Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate DB connection pools for read/update in platform update benchmark #1816

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ public class RawDb
= 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;
roji marked this conversation as resolved.
Show resolved Hide resolved
_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))
roji marked this conversation as resolved.
Show resolved Hide resolved
{
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