5
5
using System . Collections . Generic ;
6
6
using System . Data ;
7
7
using System . Data . Common ;
8
- using System . Text ;
9
8
using System . Threading . Tasks ;
10
9
using Benchmarks . Configuration ;
11
10
using Microsoft . Extensions . Options ;
@@ -14,6 +13,8 @@ namespace Benchmarks.Data
14
13
{
15
14
public class RawDb : IDb
16
15
{
16
+ private static readonly Comparison < World > WorldSortComparison = ( a , b ) => a . Id . CompareTo ( b . Id ) ;
17
+
17
18
private readonly IRandom _random ;
18
19
private readonly DbProviderFactory _dbProviderFactory ;
19
20
private readonly string _connectionString ;
@@ -89,10 +90,6 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
89
90
90
91
public async Task < World [ ] > LoadMultipleUpdatesRows ( int count )
91
92
{
92
- var results = new World [ count ] ;
93
-
94
- var updateCommand = new StringBuilder ( count ) ;
95
-
96
93
using ( var db = _dbProviderFactory . CreateConnection ( ) )
97
94
{
98
95
db . ConnectionString = _connectionString ;
@@ -101,41 +98,41 @@ public async Task<World[]> LoadMultipleUpdatesRows(int count)
101
98
using ( var updateCmd = db . CreateCommand ( ) )
102
99
using ( var queryCmd = CreateReadCommand ( db ) )
103
100
{
101
+ var results = new World [ count ] ;
104
102
for ( int i = 0 ; i < count ; i ++ )
105
103
{
106
104
results [ i ] = await ReadSingleRow ( db , queryCmd ) ;
107
105
queryCmd . Parameters [ "@Id" ] . Value = _random . Next ( 1 , 10001 ) ;
108
106
}
109
107
110
108
// Postgres has problems with deadlocks when these aren't sorted
111
- Array . Sort < World > ( results , ( a , b ) => a . Id . CompareTo ( b . Id ) ) ;
109
+ Array . Sort < World > ( results , WorldSortComparison ) ;
112
110
113
111
for ( int i = 0 ; i < count ; i ++ )
114
112
{
113
+ var strings = BatchUpdateString . Strings [ i ] ;
115
114
var id = updateCmd . CreateParameter ( ) ;
116
- id . ParameterName = BatchUpdateString . Strings [ i ] . Id ;
115
+ id . ParameterName = strings . Id ;
117
116
id . DbType = DbType . Int32 ;
118
117
updateCmd . Parameters . Add ( id ) ;
119
118
120
119
var random = updateCmd . CreateParameter ( ) ;
121
- random . ParameterName = BatchUpdateString . Strings [ i ] . Random ;
120
+ random . ParameterName = strings . Random ;
122
121
random . DbType = DbType . Int32 ;
123
122
updateCmd . Parameters . Add ( random ) ;
124
123
125
124
var randomNumber = _random . Next ( 1 , 10001 ) ;
126
125
id . Value = results [ i ] . Id ;
127
126
random . Value = randomNumber ;
128
127
results [ i ] . RandomNumber = randomNumber ;
129
-
130
- updateCommand . Append ( BatchUpdateString . Strings [ i ] . UpdateQuery ) ;
131
128
}
132
129
133
- updateCmd . CommandText = updateCommand . ToString ( ) ;
130
+ updateCmd . CommandText = BatchUpdateString . Strings [ results . Length - 1 ] . UpdateQuery ;
131
+
134
132
await updateCmd . ExecuteNonQueryAsync ( ) ;
133
+ return results ;
135
134
}
136
135
}
137
-
138
- return results ;
139
136
}
140
137
141
138
public async Task < IEnumerable < Fortune > > LoadFortunesRows ( )
0 commit comments