Skip to content

Commit fffbfea

Browse files
committed
Use separate DB connection pools for read/update in platform update benchmark
1 parent 83993ea commit fffbfea

File tree

1 file changed

+39
-6
lines changed
  • src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data

1 file changed

+39
-6
lines changed

src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs

+39-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private readonly MemoryCache _cache
2020
= new(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(60) });
2121

2222
#if NET7_0_OR_GREATER
23-
private readonly NpgsqlDataSource _dataSource;
23+
private readonly NpgsqlDataSource _dataSource, _updateDataSource, _readOnlyDataSource;
2424
#else
2525
private readonly string _connectionString;
2626
#endif
@@ -30,6 +30,15 @@ public RawDb(ConcurrentRandom random, AppSettings appSettings)
3030
_random = random;
3131
#if NET7_0_OR_GREATER
3232
_dataSource = NpgsqlDataSource.Create(appSettings.ConnectionString);
33+
34+
// For the update benchmark, we use two different connection pools for read/update, to avoid head-of-line
35+
// perf issues.
36+
var dataSourceBuilder = new NpgsqlDataSourceBuilder(appSettings.ConnectionString);
37+
38+
dataSourceBuilder.ConnectionStringBuilder.MaxPoolSize = 18;
39+
_readOnlyDataSource = dataSourceBuilder.Build();
40+
dataSourceBuilder.ConnectionStringBuilder.MaxPoolSize = 9;
41+
_updateDataSource = dataSourceBuilder.Build();
3342
#else
3443
_connectionString = appSettings.ConnectionString;
3544
#endif
@@ -170,14 +179,12 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
170179
}
171180
#endif
172181

182+
#if NET7_0_OR_GREATER
173183
public async Task<World[]> LoadMultipleUpdatesRows(int count)
174184
{
175185
var results = new World[count];
176186

177-
using var connection = CreateConnection();
178-
await connection.OpenAsync();
179-
180-
#if NET7_0_OR_GREATER
187+
using (var connection = await _readOnlyDataSource.OpenConnectionAsync())
181188
using (var batch = new NpgsqlBatch(connection))
182189
{
183190
// Inserts a PG Sync message between each statement in the batch, required for compliance with
@@ -203,7 +210,33 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
203210
await reader.NextResultAsync();
204211
}
205212
}
213+
214+
using (var connection = await _updateDataSource.OpenConnectionAsync())
215+
using (var updateCmd = new NpgsqlCommand(BatchUpdateString.Query(count), connection))
216+
{
217+
for (var i = 0; i < results.Length; i++)
218+
{
219+
var randomNumber = _random.Next(1, 10001);
220+
221+
updateCmd.Parameters.Add(new NpgsqlParameter<int> { TypedValue = results[i].Id });
222+
updateCmd.Parameters.Add(new NpgsqlParameter<int> { TypedValue = randomNumber });
223+
224+
results[i].RandomNumber = randomNumber;
225+
}
226+
227+
await updateCmd.ExecuteNonQueryAsync();
228+
}
229+
230+
return results;
231+
}
206232
#else
233+
public async Task<World[]> LoadMultipleUpdatesRows(int count)
234+
{
235+
var results = new World[count];
236+
237+
using var connection = CreateConnection();
238+
await connection.OpenAsync();
239+
207240
var (queryCmd, queryParameter) = CreateReadCommand(connection);
208241
using (queryCmd)
209242
{
@@ -213,7 +246,6 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
213246
queryParameter.TypedValue = _random.Next(1, 10001);
214247
}
215248
}
216-
#endif
217249

218250
using (var updateCmd = new NpgsqlCommand(BatchUpdateString.Query(count), connection))
219251
{
@@ -232,6 +264,7 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
232264

233265
return results;
234266
}
267+
#endif
235268

236269
public async Task<List<FortuneUtf8>> LoadFortunesRows()
237270
{

0 commit comments

Comments
 (0)