Skip to content

Commit e204946

Browse files
fix: read benchmarks logic
1 parent 98040fa commit e204946

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ env:
1111
DOTNET_VERSION: '8.0.x'
1212

1313
on:
14+
push:
15+
branches: [ "add-performance-benchmark-with-efcore" ]
1416
release:
1517
types: [ published ]
1618
workflow_dispatch:

benchmark/BenchmarkRunner/Benchmarks/MysqlReadBenchmark.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ namespace BenchmarkRunner.Benchmarks;
1414
[CategoriesColumn]
1515
public class MysqlReadBenchmark
1616
{
17-
private const int CustomerCount = 500;
1817
private readonly string _connectionString = Config.GetMysqlConnectionString();
1918
private QuerySql _sqlcImpl = null!;
19+
private const int CustomerCount = 500;
20+
private const int QueriesToRun = 10000;
2021

2122
[Params(5000, 10000, 20000)]
2223
public int Limit { get; set; }
@@ -46,7 +47,7 @@ await seeder.SeedAsync(
4647
[Benchmark(Baseline = true, Description = "SQLC - GetCustomerOrders")]
4748
public async Task<List<QuerySql.GetCustomerOrdersRow>> Sqlc_GetCustomerOrders()
4849
{
49-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, _ =>
50+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, _ =>
5051
{
5152
return _sqlcImpl.GetCustomerOrdersAsync(new QuerySql.GetCustomerOrdersArgs(
5253
CustomerId: Random.Shared.Next(1, CustomerCount),
@@ -60,7 +61,7 @@ await seeder.SeedAsync(
6061
[Benchmark(Description = "EFCore (NoTracking) - GetCustomerOrders")]
6162
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_NoTracking_GetCustomerOrders()
6263
{
63-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
64+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
6465
{
6566
await using var dbContext = new SalesDbContext(_connectionString);
6667
var queries = new Queries(dbContext, useTracking: false);
@@ -76,7 +77,7 @@ await seeder.SeedAsync(
7677
[Benchmark(Description = "EFCore (WithTracking) - GetCustomerOrders")]
7778
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_WithTracking_GetCustomerOrders()
7879
{
79-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
80+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
8081
{
8182
await using var dbContext = new SalesDbContext(_connectionString);
8283
var queries = new Queries(dbContext, useTracking: true);

benchmark/BenchmarkRunner/Benchmarks/PostgresqlReadBenchmark.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ namespace BenchmarkRunner.Benchmarks;
1414
[CategoriesColumn]
1515
public class PostgresqlReadBenchmark
1616
{
17-
private const int CustomerCount = 500;
1817
private readonly string _connectionString = Config.GetPostgresConnectionString();
1918
private QuerySql _sqlcImpl = null!;
19+
private const int CustomerCount = 500;
20+
private const int QueriesToRun = 10000;
2021

2122
[Params(5000, 10000, 20000)]
2223
public int Limit { get; set; }
@@ -48,7 +49,7 @@ await seeder.SeedAsync(
4849
[Benchmark(Baseline = true, Description = "SQLC - GetCustomerOrders")]
4950
public async Task<List<QuerySql.GetCustomerOrdersRow>> Sqlc_GetCustomerOrders()
5051
{
51-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, _ =>
52+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, _ =>
5253
{
5354
return _sqlcImpl.GetCustomerOrdersAsync(new QuerySql.GetCustomerOrdersArgs(
5455
CustomerId: Random.Shared.Next(1, CustomerCount),
@@ -62,7 +63,7 @@ await seeder.SeedAsync(
6263
[Benchmark(Description = "EFCore (NoTracking) - GetCustomerOrders")]
6364
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_NoTracking_GetCustomerOrders()
6465
{
65-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
66+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
6667
{
6768
await using var dbContext = new SalesDbContext(_connectionString);
6869
var queries = new Queries(dbContext, useTracking: false);
@@ -78,7 +79,7 @@ await seeder.SeedAsync(
7879
[Benchmark(Description = "EFCore (WithTracking) - GetCustomerOrders")]
7980
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_WithTracking_GetCustomerOrders()
8081
{
81-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
82+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
8283
{
8384
await using var dbContext = new SalesDbContext(_connectionString);
8485
var queries = new Queries(dbContext, useTracking: true);

benchmark/BenchmarkRunner/Benchmarks/SqliteReadBenchmark.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ namespace BenchmarkRunner.Benchmarks;
1414
[CategoriesColumn]
1515
public class SqliteReadBenchmark
1616
{
17-
private const int CustomerCount = 500;
1817
private readonly string _connectionString = Config.GetSqliteConnectionString();
1918
private QuerySql _sqlcImpl = null!;
19+
private const int CustomerCount = 500;
20+
private const int QueriesToRun = 1000;
2021

2122
[Params(5000, 10000, 20000)]
2223
public int Limit { get; set; }
2324

24-
[Params(1, 10)]
25+
[Params(5, 50)]
2526
public int ConcurrentQueries { get; set; }
2627

2728
[GlobalSetup]
@@ -48,7 +49,7 @@ await seeder.SeedAsync(
4849
[Benchmark(Baseline = true, Description = "SQLC - GetCustomerOrders")]
4950
public async Task<List<QuerySql.GetCustomerOrdersRow>> Sqlc_GetCustomerOrders()
5051
{
51-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, _ =>
52+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, _ =>
5253
{
5354
return _sqlcImpl.GetCustomerOrdersAsync(new QuerySql.GetCustomerOrdersArgs(
5455
CustomerId: Random.Shared.Next(1, CustomerCount),
@@ -62,7 +63,7 @@ await seeder.SeedAsync(
6263
[Benchmark(Description = "EFCore (NoTracking) - GetCustomerOrders")]
6364
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_NoTracking_GetCustomerOrders()
6465
{
65-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
66+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
6667
{
6768
await using var dbContext = new SalesDbContext(_connectionString);
6869
var queries = new Queries(dbContext, useTracking: false);
@@ -78,7 +79,7 @@ await seeder.SeedAsync(
7879
[Benchmark(Description = "EFCore (WithTracking) - GetCustomerOrders")]
7980
public async Task<List<Queries.GetCustomerOrdersRow>> EFCore_WithTracking_GetCustomerOrders()
8081
{
81-
return await Helpers.ExecuteConcurrentlyAsync(ConcurrentQueries, async _ =>
82+
return await Helpers.ExecuteConcurrentlyAsync(QueriesToRun, ConcurrentQueries, async _ =>
8283
{
8384
await using var dbContext = new SalesDbContext(_connectionString);
8485
var queries = new Queries(dbContext, useTracking: true);

benchmark/BenchmarkRunner/Utils/Helpers.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,33 @@ public static async Task InsertInBatchesAsync<T>(List<T> items, int batchSize, F
2020
}
2121
}
2222

23-
public static async Task<List<T>> ExecuteConcurrentlyAsync<T>(int concurrency, Func<int, Task<List<T>>> taskFactory)
23+
public static async Task<List<T>> ExecuteConcurrentlyAsync<T>(
24+
int totalTasks,
25+
int maxConcurrency,
26+
Func<int, Task<List<T>>> taskFactory)
2427
{
28+
var semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency);
2529
var tasks = new List<Task<List<T>>>();
26-
for (int i = 0; i < concurrency; i++)
27-
{
28-
tasks.Add(taskFactory(i));
29-
}
30+
for (int i = 0; i < totalTasks; i++)
31+
tasks.Add(ExecuteWithThrottleAsync(semaphore, () => taskFactory(i)));
3032

3133
var results = await Task.WhenAll(tasks);
3234
return [.. results.SelectMany(r => r)];
3335
}
36+
37+
private static async Task<T> ExecuteWithThrottleAsync<T>(SemaphoreSlim? semaphore, Func<Task<T>> taskFactory)
38+
{
39+
if (semaphore == null)
40+
return await taskFactory();
41+
42+
await semaphore.WaitAsync();
43+
try
44+
{
45+
return await taskFactory();
46+
}
47+
finally
48+
{
49+
semaphore.Release();
50+
}
51+
}
3452
}

0 commit comments

Comments
 (0)