Skip to content

Commit 930b61a

Browse files
authored
Fix failing performance test (#65)
1 parent fab2754 commit 930b61a

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/SIL.Harmony.Sample/CrdtSampleKernel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi
2222
{
2323
//this ensures that Ef Conversion methods will not be cached across different IoC containers
2424
//this can show up as the second instance using the JsonSerializerOptions from the first container
25-
//only needed for testing scenarios
25+
//But, for testing scenarios (performanceTest: true) this essentially restores EF Core's default service-provider caching
26+
//(which is otherwise disabled for test isolation) and thus matches real ASP.NET Core app config
2627
builder.EnableServiceProviderCaching(performanceTest);
2728
builder.UseLinqToDbCrdt(provider);
2829
optionsBuilder(builder);

src/SIL.Harmony.Tests/DataModelPerformanceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static async Task<TimeSpan> MeasureTime(Func<Task> action, int iteration
7777
public async Task SimpleAddChangePerformanceTest()
7878
{
7979
//disable validation because it's slow
80-
var dataModelTest = new DataModelTestBase(alwaysValidate: false);
80+
var dataModelTest = new DataModelTestBase(alwaysValidate: false, performanceTest: true);
8181
// warmup the code, this causes jit to run and keeps our actual test below consistent
8282
await dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 0"));
8383
var runtimeAddChange1Snapshot = await MeasureTime(() => dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity 1")).AsTask());
@@ -86,6 +86,9 @@ public async Task SimpleAddChangePerformanceTest()
8686
//fork the database, this creates a new DbContext which does not have a cache of all the snapshots created above
8787
//that cache causes DetectChanges (used by SaveChanges) to be slower than it should be
8888
dataModelTest = dataModelTest.ForkDatabase(false);
89+
//warmup the forked context too — it has a fresh ServiceProvider/DbContext/DataModel,
90+
//so the first WriteNextChange pays EF Core query-compilation cost that the measurement shouldn't include
91+
await dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity warmup"));
8992

9093
await StartTrace();
9194
var runtimeAddChange10000Snapshots = await MeasureTime(() => dataModelTest.WriteNextChange(dataModelTest.SetWord(Guid.NewGuid(), "entity1")).AsTask());

0 commit comments

Comments
 (0)