From f0d9689ae8e8eed9a27f825c7aa33bcd45d9d72f Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Tue, 29 Oct 2024 10:42:12 +0700 Subject: [PATCH] allow specifying to CrdtSampleKernel that this is a performance test to enable EF Service provider caching as that makes our performance degrade enough to fail the tests --- src/SIL.Harmony.Sample/CrdtSampleKernel.cs | 8 ++------ .../DataModelPerformanceTests.cs | 7 +++++-- src/SIL.Harmony.Tests/DataModelTestBase.cs | 17 +++++++++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/SIL.Harmony.Sample/CrdtSampleKernel.cs b/src/SIL.Harmony.Sample/CrdtSampleKernel.cs index 527afd0..5bf8061 100644 --- a/src/SIL.Harmony.Sample/CrdtSampleKernel.cs +++ b/src/SIL.Harmony.Sample/CrdtSampleKernel.cs @@ -15,20 +15,16 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi { return services.AddCrdtDataSample(builder => builder.UseSqlite($"Data Source={dbPath}")); } - public static IServiceCollection AddCrdtDataSample(this IServiceCollection services, DbConnection connection) - { - return services.AddCrdtDataSample(builder => builder.UseSqlite(connection, true)); - } public static IServiceCollection AddCrdtDataSample(this IServiceCollection services, - Action optionsBuilder) + Action optionsBuilder, bool performanceTest = false) { services.AddDbContext((provider, builder) => { //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 - builder.EnableServiceProviderCaching(false); + builder.EnableServiceProviderCaching(performanceTest); builder.UseLinqToDbCrdt(provider); optionsBuilder(builder); builder.EnableDetailedErrors(); diff --git a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs index d009691..48073d5 100644 --- a/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs +++ b/src/SIL.Harmony.Tests/DataModelPerformanceTests.cs @@ -23,6 +23,9 @@ public class DataModelPerformanceTests(ITestOutputHelper output) [Fact] public void AddingChangePerformance() { + #if DEBUG + Assert.Fail("This test is disabled in debug builds, not reliable"); + #endif var summary = BenchmarkRunner.Run( ManualConfig.CreateEmpty() @@ -188,7 +191,7 @@ public class DataModelPerformanceBenchmarks [GlobalSetup] public void GlobalSetup() { - _templateModel = new DataModelTestBase(alwaysValidate: false); + _templateModel = new DataModelTestBase(alwaysValidate: false, performanceTest: true); DataModelPerformanceTests.BulkInsertChanges(_templateModel, StartingSnapshots).GetAwaiter().GetResult(); } @@ -198,7 +201,7 @@ public void GlobalSetup() [IterationSetup] public void IterationSetup() { - _emptyDataModel = new(alwaysValidate: false); + _emptyDataModel = new(alwaysValidate: false, performanceTest: true); _ = _emptyDataModel.WriteNextChange(_emptyDataModel.SetWord(Guid.NewGuid(), "entity1")).Result; _dataModelTestBase = _templateModel.ForkDatabase(false); } diff --git a/src/SIL.Harmony.Tests/DataModelTestBase.cs b/src/SIL.Harmony.Tests/DataModelTestBase.cs index 81db26a..0b2515c 100644 --- a/src/SIL.Harmony.Tests/DataModelTestBase.cs +++ b/src/SIL.Harmony.Tests/DataModelTestBase.cs @@ -1,3 +1,4 @@ +using System.Data.Common; using Microsoft.Data.Sqlite; using SIL.Harmony.Changes; using SIL.Harmony.Core; @@ -17,15 +18,16 @@ public class DataModelTestBase : IAsyncLifetime { protected readonly ServiceProvider _services; protected readonly Guid _localClientId = Guid.NewGuid(); + private readonly bool _performanceTest; public readonly DataModel DataModel; public readonly SampleDbContext DbContext; internal readonly CrdtRepository CrdtRepository; protected readonly MockTimeProvider MockTimeProvider = new(); public DataModelTestBase(bool saveToDisk = false, bool alwaysValidate = true, - Action? configure = null) : this(saveToDisk + Action? configure = null, bool performanceTest = false) : this(saveToDisk ? new SqliteConnection("Data Source=test.db") - : new SqliteConnection("Data Source=:memory:"), alwaysValidate, configure) + : new SqliteConnection("Data Source=:memory:"), alwaysValidate, configure, performanceTest) { } @@ -33,10 +35,13 @@ public DataModelTestBase() : this(new SqliteConnection("Data Source=:memory:")) { } - public DataModelTestBase(SqliteConnection connection, bool alwaysValidate = true, Action? configure = null) + public DataModelTestBase(SqliteConnection connection, bool alwaysValidate = true, Action? configure = null, bool performanceTest = false) { - var serviceCollection = new ServiceCollection() - .AddCrdtDataSample(connection) + _performanceTest = performanceTest; + var serviceCollection = new ServiceCollection().AddCrdtDataSample(builder => + { + builder.UseSqlite(connection, true); + }, performanceTest) .Configure(config => config.AlwaysValidateCommits = alwaysValidate) .Replace(ServiceDescriptor.Singleton(MockTimeProvider)); configure?.Invoke(serviceCollection); @@ -55,7 +60,7 @@ public DataModelTestBase ForkDatabase(bool alwaysValidate = true) var existingConnection = DbContext.Database.GetDbConnection() as SqliteConnection; if (existingConnection is null) throw new InvalidOperationException("Database is not SQLite"); existingConnection.BackupDatabase(connection); - var newTestBase = new DataModelTestBase(connection, alwaysValidate); + var newTestBase = new DataModelTestBase(connection, alwaysValidate, performanceTest: _performanceTest); newTestBase.SetCurrentDate(currentDate.DateTime); return newTestBase; }