Skip to content

Commit

Permalink
Use separate DB connection pools for read/update in platform update b…
Browse files Browse the repository at this point in the history
…enchmark
  • Loading branch information
roji committed Mar 16, 2023
1 parent 34796da commit b702ae6
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/BenchmarksApps/TechEmpower/PlatformBenchmarks/Data/RawDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private readonly MemoryCache _cache
= new(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(60) });

#if NET7_0_OR_GREATER
private readonly NpgsqlDataSource _dataSource;
private readonly NpgsqlDataSource _dataSource, _updateDataSource, _readOnlyDataSource;
#else
private readonly string _connectionString;
#endif
Expand All @@ -30,6 +30,15 @@ public RawDb(ConcurrentRandom random, AppSettings appSettings)
_random = random;
#if NET7_0_OR_GREATER
_dataSource = NpgsqlDataSource.Create(appSettings.ConnectionString);

// For the update benchmark, we use two different connection pools for read/update, to avoid head-of-line
// perf issues.
var dataSourceBuilder = new NpgsqlDataSourceBuilder(appSettings.ConnectionString);

dataSourceBuilder.ConnectionStringBuilder.MaxPoolSize = 18;
_readOnlyDataSource = dataSourceBuilder.Build();
dataSourceBuilder.ConnectionStringBuilder.MaxPoolSize = 9;
_updateDataSource = dataSourceBuilder.Build();
#else
_connectionString = appSettings.ConnectionString;
#endif
Expand Down Expand Up @@ -170,14 +179,12 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
}
#endif

#if NET7_0_OR_GREATER
public async Task<World[]> LoadMultipleUpdatesRows(int count)
{
var results = new World[count];

using var connection = CreateConnection();
await connection.OpenAsync();

#if NET7_0_OR_GREATER
using (var connection = await _readOnlyDataSource.OpenConnectionAsync())
using (var batch = new NpgsqlBatch(connection))
{
// Inserts a PG Sync message between each statement in the batch, required for compliance with
Expand All @@ -203,7 +210,33 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
await reader.NextResultAsync();
}
}

using (var connection = await _updateDataSource.OpenConnectionAsync())
using (var updateCmd = new NpgsqlCommand(BatchUpdateString.Query(count), connection))
{
for (var i = 0; i < results.Length; i++)
{
var randomNumber = _random.Next(1, 10001);

updateCmd.Parameters.Add(new NpgsqlParameter<int> { TypedValue = results[i].Id });
updateCmd.Parameters.Add(new NpgsqlParameter<int> { TypedValue = randomNumber });

results[i].RandomNumber = randomNumber;
}

await updateCmd.ExecuteNonQueryAsync();
}

return results;
}
#else
public async Task<World[]> LoadMultipleUpdatesRows(int count)
{
var results = new World[count];

using var connection = CreateConnection();
await connection.OpenAsync();

var (queryCmd, queryParameter) = CreateReadCommand(connection);
using (queryCmd)
{
Expand All @@ -213,7 +246,6 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
queryParameter.TypedValue = _random.Next(1, 10001);
}
}
#endif

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

return results;
}
#endif

public async Task<List<Fortune>> LoadFortunesRows()
{
Expand Down

0 comments on commit b702ae6

Please sign in to comment.