From b3cb4daff78590380e19ff06b9ae6a9538bcddd2 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Wed, 13 May 2026 12:32:08 +0200 Subject: [PATCH] Fix failing performance test --- src/SIL.Harmony.Sample/CrdtSampleKernel.cs | 3 ++- src/SIL.Harmony.Tests/DataModelPerformanceTests.cs | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/SIL.Harmony.Sample/CrdtSampleKernel.cs b/src/SIL.Harmony.Sample/CrdtSampleKernel.cs index 761af81..a89454e 100644 --- a/src/SIL.Harmony.Sample/CrdtSampleKernel.cs +++ b/src/SIL.Harmony.Sample/CrdtSampleKernel.cs @@ -22,7 +22,8 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi { //this ensures that Ef Conversion methods will not be cached across different IoC containers //this can show up as the second instance using the JsonSerializerOptions from the first container - //only needed for testing scenarios + //But, for testing scenarios (performanceTest: true) this essentially restores EF Core's default service-provider caching + //(which is otherwise disabled for test isolation) and thus matches real ASP.NET Core app config builder.EnableServiceProviderCaching(performanceTest); builder.UseLinqToDbCrdt(provider); optionsBuilder(builder); diff --git a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs index 84398e5..fabd799 100644 --- a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs +++ b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs @@ -78,7 +78,7 @@ private static async Task MeasureTime(Func action, int iteration public async Task SimpleAddChangePerformanceTest() { //disable validation because it's slow - var dataModelTest = new DataModelTestBase(alwaysValidate: false); + var dataModelTest = new DataModelTestBase(alwaysValidate: false, performanceTest: true); // warmup the code, this causes jit to run and keeps our actual test below consistent await dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 0")); var runtimeAddChange1Snapshot = await MeasureTime(() => dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 1")).AsTask()); @@ -87,6 +87,9 @@ public async Task SimpleAddChangePerformanceTest() //fork the database, this creates a new DbContext which does not have a cache of all the snapshots created above //that cache causes DetectChanges (used by SaveChanges) to be slower than it should be dataModelTest = dataModelTest.ForkDatabase(false); + //warmup the forked context too — it has a fresh ServiceProvider/DbContext/DataModel, + //so the first WriteNextChange pays EF Core query-compilation cost that the measurement shouldn't include + await dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity warmup")); await StartTrace(); var runtimeAddChange10000Snapshots = await MeasureTime(() => dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity1")).AsTask());