@@ -20,7 +20,7 @@ private readonly MemoryCache _cache
20
20
= new ( new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan . FromMinutes ( 60 ) } ) ;
21
21
22
22
#if NET7_0_OR_GREATER
23
- private readonly NpgsqlDataSource _dataSource ;
23
+ private readonly NpgsqlDataSource _dataSource , _updateDataSource , _readOnlyDataSource ;
24
24
#else
25
25
private readonly string _connectionString ;
26
26
#endif
@@ -30,6 +30,15 @@ public RawDb(ConcurrentRandom random, AppSettings appSettings)
30
30
_random = random ;
31
31
#if NET7_0_OR_GREATER
32
32
_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 ( ) ;
33
42
#else
34
43
_connectionString = appSettings . ConnectionString ;
35
44
#endif
@@ -170,14 +179,12 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
170
179
}
171
180
#endif
172
181
182
+ #if NET7_0_OR_GREATER
173
183
public async Task < World [ ] > LoadMultipleUpdatesRows ( int count )
174
184
{
175
185
var results = new World [ count ] ;
176
186
177
- using var connection = CreateConnection ( ) ;
178
- await connection . OpenAsync ( ) ;
179
-
180
- #if NET7_0_OR_GREATER
187
+ using ( var connection = await _readOnlyDataSource . OpenConnectionAsync ( ) )
181
188
using ( var batch = new NpgsqlBatch ( connection ) )
182
189
{
183
190
// 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)
203
210
await reader . NextResultAsync ( ) ;
204
211
}
205
212
}
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
+ }
206
232
#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
+
207
240
var ( queryCmd , queryParameter ) = CreateReadCommand ( connection ) ;
208
241
using ( queryCmd )
209
242
{
@@ -213,7 +246,6 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
213
246
queryParameter . TypedValue = _random . Next ( 1 , 10001 ) ;
214
247
}
215
248
}
216
- #endif
217
249
218
250
using ( var updateCmd = new NpgsqlCommand ( BatchUpdateString . Query ( count ) , connection ) )
219
251
{
@@ -232,6 +264,7 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
232
264
233
265
return results ;
234
266
}
267
+ #endif
235
268
236
269
public async Task < List < FortuneUtf8 > > LoadFortunesRows ( )
237
270
{
0 commit comments