From 08ec7f00cee439873d95e0b5dc3ca4cfa6d9b63b Mon Sep 17 00:00:00 2001 From: Thorsten Thiel Date: Wed, 18 Sep 2024 23:53:13 +0200 Subject: [PATCH] WIP [skip-ci] --- src/Benchmark/Bench.cs | 40 +++++++++++++++---- src/Benchmark/Benchmark.csproj | 1 + src/Benchmark/Program.cs | 9 ++++- src/Benchmark/docker-compose.yaml | 9 +++++ .../PostgreSQLEventRepository.cs | 4 ++ 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/Benchmark/docker-compose.yaml diff --git a/src/Benchmark/Bench.cs b/src/Benchmark/Bench.cs index 079c1d2..bb1336c 100644 --- a/src/Benchmark/Bench.cs +++ b/src/Benchmark/Bench.cs @@ -4,25 +4,42 @@ using Fluss; using Fluss.Authentication; using Fluss.Events; +using Fluss.PostgreSQL; using Fluss.ReadModel; using Microsoft.Extensions.DependencyInjection; +using Npgsql; namespace Benchmark; -[GitJob("879573d", baseline: true, id: "0_before")] -[SimpleJob(id: "1_after")] +// [GitJob("a4d91117", baseline: true, id: "0_before")] +[SimpleJob(id: "current")] [RPlotExporter] [MemoryDiagnoser] public class Bench { + [Params("in-memory", "postgres")] + public string StorageType { get; set; } + [IterationSetup] public void Setup() { var sc = new ServiceCollection(); sc.AddEventSourcing(); + + if (StorageType == "postgres") + { + sc.AddPostgresEventSourcingRepository("Host=localhost;Port=5432;Database=fluss;Username=fluss;Password=fluss"); + } + sc.AddPolicy(); _sp = sc.BuildServiceProvider(); + + if (StorageType == "postgres") { + var migrator = _sp.GetRequiredService(); + migrator.StartAsync(default).Wait(); + migrator.WaitForFinish().Wait(); + } } ServiceProvider _sp = null!; @@ -51,14 +68,23 @@ await _sp.GetSystemUserUnitOfWorkFactory().Commit(async unitOfWork => return sum; } + [IterationCleanup] + public void Cleanup() + { + if (StorageType == "postgres") { + var conn = new NpgsqlConnection("Host=localhost;Port=5432;Database=fluss;Username=fluss;Password=fluss"); + conn.Open(); + using var cmd = conn.CreateCommand(); + cmd.CommandText = @"DELETE FROM ""Events"""; + cmd.ExecuteNonQuery(); + conn.Close(); + } + } + [IterationSetup(Targets = [nameof(PublishEventsAndReadReadHeavySingleReadModel), nameof(PublishEventsAndReadReadHeavyMultipleReadModel)])] public void SetupHeavyRead() { - var sc = new ServiceCollection(); - sc.AddEventSourcing(); - sc.AddPolicy(); - - _sp = sc.BuildServiceProvider(); + Setup(); _sp.GetSystemUserUnitOfWorkFactory().Commit(async unitOfWork => { diff --git a/src/Benchmark/Benchmark.csproj b/src/Benchmark/Benchmark.csproj index 83fb0ee..9be7344 100644 --- a/src/Benchmark/Benchmark.csproj +++ b/src/Benchmark/Benchmark.csproj @@ -14,6 +14,7 @@ + diff --git a/src/Benchmark/Program.cs b/src/Benchmark/Program.cs index d7bd483..f19de90 100644 --- a/src/Benchmark/Program.cs +++ b/src/Benchmark/Program.cs @@ -1,4 +1,11 @@ using Benchmark; using BenchmarkDotNet.Running; -BenchmarkRunner.Run(); \ No newline at end of file +BenchmarkRunner.Run(); + +// var bench = new Bench(); +// bench.StorageType = "postgres"; +// bench.SetupHeavyRead(); +// await bench.PublishEventsAndReadReadHeavySingleReadModel(); +// bench.SetupHeavyRead(); +// bench.Cleanup(); \ No newline at end of file diff --git a/src/Benchmark/docker-compose.yaml b/src/Benchmark/docker-compose.yaml new file mode 100644 index 0000000..5ad2b98 --- /dev/null +++ b/src/Benchmark/docker-compose.yaml @@ -0,0 +1,9 @@ +services: + db: + image: postgres:15 + environment: + POSTGRES_USER: fluss + POSTGRES_PASSWORD: fluss + POSTGRES_DB: fluss + ports: + - "5432:5432" \ No newline at end of file diff --git a/src/Fluss.PostgreSQL/PostgreSQLEventRepository.cs b/src/Fluss.PostgreSQL/PostgreSQLEventRepository.cs index ae760ef..23086e9 100644 --- a/src/Fluss.PostgreSQL/PostgreSQLEventRepository.cs +++ b/src/Fluss.PostgreSQL/PostgreSQLEventRepository.cs @@ -17,6 +17,10 @@ public partial class PostgreSQLEventRepository : IBaseEventRepository public PostgreSQLEventRepository(PostgreSQLConfig config) { var dataSourceBuilder = new NpgsqlDataSourceBuilder(config.ConnectionString); + dataSourceBuilder.ConnectionStringBuilder.Pooling = true; + dataSourceBuilder.ConnectionStringBuilder.MaxPoolSize = 100; + dataSourceBuilder.ConnectionStringBuilder.MinPoolSize = 1; + dataSourceBuilder.ConnectionStringBuilder.Enlist = false; dataSourceBuilder.UseJsonNet(settings: new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All,