Skip to content

Commit

Permalink
allow specifying to CrdtSampleKernel that this is a performance test …
Browse files Browse the repository at this point in the history
…to enable EF Service provider caching as that makes our performance degrade enough to fail the tests
  • Loading branch information
hahn-kev committed Oct 29, 2024
1 parent 53e763f commit f0d9689
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 2 additions & 6 deletions src/SIL.Harmony.Sample/CrdtSampleKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DbContextOptionsBuilder> optionsBuilder)
Action<DbContextOptionsBuilder> optionsBuilder, bool performanceTest = false)
{
services.AddDbContext<SampleDbContext>((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();
Expand Down
7 changes: 5 additions & 2 deletions src/SIL.Harmony.Tests/DataModelPerformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataModelPerformanceBenchmarks>(
ManualConfig.CreateEmpty()
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
}
Expand Down
17 changes: 11 additions & 6 deletions src/SIL.Harmony.Tests/DataModelTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data.Common;
using Microsoft.Data.Sqlite;
using SIL.Harmony.Changes;
using SIL.Harmony.Core;
Expand All @@ -17,26 +18,30 @@ 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<IServiceCollection>? configure = null) : this(saveToDisk
Action<IServiceCollection>? 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)
{
}

public DataModelTestBase() : this(new SqliteConnection("Data Source=:memory:"))
{
}

public DataModelTestBase(SqliteConnection connection, bool alwaysValidate = true, Action<IServiceCollection>? configure = null)
public DataModelTestBase(SqliteConnection connection, bool alwaysValidate = true, Action<IServiceCollection>? 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<CrdtConfig>(config => config.AlwaysValidateCommits = alwaysValidate)
.Replace(ServiceDescriptor.Singleton<IHybridDateTimeProvider>(MockTimeProvider));
configure?.Invoke(serviceCollection);
Expand All @@ -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;
}
Expand Down

0 comments on commit f0d9689

Please sign in to comment.