Skip to content

Commit cdd075f

Browse files
fix: adjust mysql and postgres benchmark settings
1 parent 2ca05be commit cdd075f

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

benchmark/BenchmarkRunner/Benchmarks/MysqlReadBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MysqlReadBenchmark
1919
private const int CustomerCount = 500;
2020
private const int QueriesToRun = 1000;
2121

22-
[Params(50, 500, 5000)]
22+
[Params(50, 500, 1000)]
2323
public int Limit { get; set; }
2424

2525
[Params(10, 50)]

benchmark/BenchmarkRunner/Benchmarks/PostgresqlReadBenchmark.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class PostgresqlReadBenchmark
1919
private const int CustomerCount = 500;
2020
private const int QueriesToRun = 1000;
2121

22-
[Params(50, 500, 5000)]
22+
[Params(50, 500, 1000)]
2323
public int Limit { get; set; }
2424

2525
[Params(10, 50)]
@@ -29,7 +29,6 @@ public class PostgresqlReadBenchmark
2929
public async Task GlobalSetup()
3030
{
3131
_sqlcImpl = new(_connectionString);
32-
3332
await PostgresqlDatabaseHelper.CleanupDatabaseAsync(_connectionString);
3433
var seeder = new PostgresqlDatabaseSeeder(_connectionString);
3534
await seeder.SeedAsync(

benchmark/BenchmarkRunner/Benchmarks/SqliteReadBenchmark.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SqliteReadBenchmark
1818
private QuerySql _sqlcImpl = null!;
1919
private const int CustomerCount = 250;
2020
private const int QueriesToRun = 500;
21-
21+
2222
[Params(50, 500)]
2323
public int Limit { get; set; }
2424

@@ -29,7 +29,6 @@ public class SqliteReadBenchmark
2929
public async Task GlobalSetup()
3030
{
3131
_sqlcImpl = new QuerySql(_connectionString);
32-
3332
SqliteDatabaseHelper.CleanupDatabase(_connectionString);
3433
await SqliteDatabaseHelper.InitializeDatabaseAsync(_connectionString);
3534
var seeder = new SqliteDatabaseSeeder(_connectionString);
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using System.Diagnostics;
12
using BenchmarkDotNet.Configs;
23
using BenchmarkRunner.Benchmarks;
4+
using BenchmarkRunner.Utils;
35
using Microsoft.Extensions.Logging;
46

57
public class MysqlRunner(string connectionString, ILogger<MysqlRunner> logger)
68
{
79
private readonly string _connectionString = connectionString;
810
private readonly ILogger<MysqlRunner> _logger = logger;
9-
1011
public string ConnectionString => _connectionString;
1112

1213
public Task RunAsync()
@@ -15,9 +16,19 @@ public Task RunAsync()
1516
var config = DefaultConfig.Instance.WithArtifactsPath(path);
1617

1718
_logger.LogInformation("Running MySQL Reads benchmarks...");
19+
var stopwatch = Stopwatch.StartNew();
1820
BenchmarkDotNet.Running.BenchmarkRunner.Run<MysqlReadBenchmark>(config);
21+
stopwatch.Stop();
22+
var readTime = stopwatch.Elapsed;
23+
1924
_logger.LogInformation("Running MySQL Writes benchmarks...");
25+
stopwatch.Restart();
2026
BenchmarkDotNet.Running.BenchmarkRunner.Run<MysqlWriteBenchmark>(config);
27+
stopwatch.Stop();
28+
var writeTime = stopwatch.Elapsed;
29+
30+
_logger.LogInformation("MySQL Reads benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(readTime));
31+
_logger.LogInformation("MySQL Writes benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(writeTime));
2132
return Task.CompletedTask;
2233
}
2334
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using System.Diagnostics;
12
using BenchmarkDotNet.Configs;
23
using BenchmarkRunner.Benchmarks;
4+
using BenchmarkRunner.Utils;
35
using Microsoft.Extensions.Logging;
46

57
public class PostgresqlRunner(string connectionString, ILogger<PostgresqlRunner> logger)
68
{
79
private readonly string _connectionString = connectionString;
810
private readonly ILogger<PostgresqlRunner> _logger = logger;
9-
1011
public string ConnectionString => _connectionString;
1112

1213
public Task RunAsync()
@@ -15,9 +16,19 @@ public Task RunAsync()
1516
var config = DefaultConfig.Instance.WithArtifactsPath(path);
1617

1718
_logger.LogInformation("Running PostgreSQL Reads benchmarks...");
19+
var stopwatch = Stopwatch.StartNew();
1820
BenchmarkDotNet.Running.BenchmarkRunner.Run<PostgresqlReadBenchmark>(config);
21+
stopwatch.Stop();
22+
var readTime = stopwatch.Elapsed;
23+
1924
_logger.LogInformation("Running PostgreSQL Writes benchmarks...");
25+
stopwatch.Restart();
2026
BenchmarkDotNet.Running.BenchmarkRunner.Run<PostgresqlWriteBenchmark>(config);
27+
stopwatch.Stop();
28+
var writeTime = stopwatch.Elapsed;
29+
30+
_logger.LogInformation("PostgreSQL Reads benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(readTime));
31+
_logger.LogInformation("PostgreSQL Writes benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(writeTime));
2132
return Task.CompletedTask;
2233
}
2334
}

benchmark/BenchmarkRunner/SqliteRunner.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System.Diagnostics;
12
using BenchmarkDotNet.Configs;
23
using BenchmarkRunner.Benchmarks;
4+
using BenchmarkRunner.Utils;
35
using Microsoft.Extensions.Logging;
46

57
public class SqliteRunner(string connectionString, ILogger<SqliteRunner> logger)
@@ -15,9 +17,19 @@ public Task RunAsync()
1517
var config = DefaultConfig.Instance.WithArtifactsPath(path);
1618

1719
_logger.LogInformation("Running SQLite Reads benchmarks...");
20+
var stopwatch = Stopwatch.StartNew();
1821
BenchmarkDotNet.Running.BenchmarkRunner.Run<SqliteReadBenchmark>(config);
22+
stopwatch.Stop();
23+
var readTime = stopwatch.Elapsed;
24+
1925
_logger.LogInformation("Running SQLite Writes benchmarks...");
26+
stopwatch.Restart();
2027
BenchmarkDotNet.Running.BenchmarkRunner.Run<SqliteWriteBenchmark>(config);
28+
stopwatch.Stop();
29+
var writeTime = stopwatch.Elapsed;;
30+
31+
_logger.LogInformation("SQLite Reads benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(readTime));
32+
_logger.LogInformation("SQLite Writes benchmarks completed in {ElapsedTime}", Helpers.FormatElapsedTime(writeTime))
2133
return Task.CompletedTask;
2234
}
2335
}

benchmark/BenchmarkRunner/Utils/Helpers.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
1-
using System.Linq;
1+
using Microsoft.Extensions.Logging;
22

33
namespace BenchmarkRunner.Utils;
44

55
public static class Helpers
66
{
7+
private static readonly ILoggerFactory LoggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
8+
{
9+
builder
10+
.AddConsole()
11+
.SetMinimumLevel(LogLevel.Information);
12+
});
13+
14+
public static ILogger<T> CreateLogger<T>() => LoggerFactory.CreateLogger<T>();
15+
716
public static void InvokeGarbageCollection()
817
{
918
GC.Collect();
1019
GC.WaitForPendingFinalizers();
1120
GC.Collect();
1221
}
1322

14-
public static async Task InsertInBatchesAsync<T>(List<T> items, int batchSize, Func<List<T>, Task> insertBatch)
23+
/// <summary>
24+
/// Formats elapsed time in a human-readable format (e.g., "1m 23.45s" or "45.67s")
25+
/// </summary>
26+
public static string FormatElapsedTime(TimeSpan elapsed)
1527
{
16-
for (int i = 0; i < items.Count; i += batchSize)
28+
if (elapsed.TotalMinutes >= 1)
1729
{
18-
var batch = items.Skip(i).Take(batchSize).ToList();
19-
await insertBatch(batch);
30+
var minutes = (int)elapsed.TotalMinutes;
31+
var seconds = elapsed.TotalSeconds % 60;
32+
return $"{minutes}m {seconds:F2}s";
2033
}
34+
return $"{elapsed.TotalSeconds:F2}s";
35+
}
36+
37+
public static async Task InsertInBatchesAsync<T>(List<T> items, int batchSize, Func<List<T>, Task> insertBatch)
38+
{
39+
var batches = items.Chunk(batchSize);
40+
foreach (var batch in batches)
41+
await insertBatch([.. batch]);
2142
}
2243

2344
private static int CalculateMaxConcurrency(int totalTasks, int maxConcurrency)
@@ -32,20 +53,20 @@ public static async Task<List<T>> ExecuteConcurrentlyAsync<T>(
3253
Func<int, Task<List<T>>> taskFactory)
3354
{
3455
maxConcurrency = CalculateMaxConcurrency(totalTasks, maxConcurrency);
35-
var semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency);
56+
using var semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency);
3657
var tasks = new List<Task<List<T>>>();
3758
for (int i = 0; i < totalTasks; i++)
38-
tasks.Add(ExecuteWithThrottleAsync(semaphore, () => taskFactory(i)));
59+
{
60+
var index = i; // Capture for closure
61+
tasks.Add(ExecuteWithThrottleAsync(semaphore, () => taskFactory(index)));
62+
}
3963

40-
var results = await Task.WhenAll(tasks);
64+
var results = await Task.WhenAll([.. tasks]);
4165
return [.. results.SelectMany(r => r)];
4266
}
4367

44-
private static async Task<T> ExecuteWithThrottleAsync<T>(SemaphoreSlim? semaphore, Func<Task<T>> taskFactory)
68+
private static async Task<List<T>> ExecuteWithThrottleAsync<T>(SemaphoreSlim semaphore, Func<Task<List<T>>> taskFactory)
4569
{
46-
if (semaphore == null)
47-
return await taskFactory();
48-
4970
await semaphore.WaitAsync();
5071
try
5172
{

0 commit comments

Comments
 (0)