@@ -52,6 +52,7 @@ struct config
52
52
int updatePercent;
53
53
int nShards;
54
54
string connection;
55
+ bool prepared;
55
56
56
57
config () {
57
58
nShards =1 ;
@@ -60,6 +61,7 @@ struct config
60
61
nIterations =1000 ;
61
62
nAccounts =10000 ;
62
63
updatePercent =100 ;
64
+ prepared =false ;
63
65
}
64
66
};
65
67
@@ -76,14 +78,14 @@ static time_t getCurrentTime()
76
78
}
77
79
78
80
79
- void exec (transaction_base& txn,char const * sql, ...)
81
+ int exec (transaction_base& txn,char const * sql, ...)
80
82
{
81
83
va_list args;
82
84
va_start (args, sql);
83
85
char buf[1024 ];
84
86
vsprintf (buf, sql, args);
85
87
va_end (args);
86
- txn.exec (buf);
88
+ return txn.exec (buf). affected_rows ( );
87
89
}
88
90
89
91
template <class T >
@@ -123,15 +125,28 @@ void* writer(void* arg)
123
125
{
124
126
thread& t = *(thread*)arg;
125
127
connectionconn (cfg.connection );
128
+ if (cfg.prepared ) {
129
+ conn.prepare (" transfer" ," update t set v = v + $1 where u=$2" );
130
+ }
126
131
for (int i =0 ; i < cfg.nIterations ; i++)
127
132
{
128
133
worktxn (conn);
129
134
int srcAcc =random () % cfg.nAccounts ;
130
135
int dstAcc =random () % cfg.nAccounts ;
131
136
try {
132
- if (random () %100 < cfg.updatePercent ) {
133
- exec (txn," update t set v = v - 1 where u=%d" , srcAcc);
134
- exec (txn," update t set v = v + 1 where u=%d" , dstAcc);
137
+ if (random () %100 < cfg.updatePercent ) {
138
+ int rc = cfg.prepared
139
+ ? txn.prepared (" transfer" )(-1 )(srcAcc).exec ().affected_rows ()
140
+ :exec (txn," update t set v = v - 1 where u=%d" , srcAcc);
141
+ if (rc !=1 ) {
142
+ printf (" Failed to withdraw from account %d\n " , srcAcc);
143
+ }
144
+ rc = cfg.prepared
145
+ ? txn.prepared (" transfer" )(1 )(dstAcc).exec ().affected_rows ()
146
+ :exec (txn," update t set v = v + 1 where u=%d" , dstAcc);
147
+ if (rc !=1 ) {
148
+ printf (" Failed to deposit to account %d\n " , dstAcc);
149
+ }
135
150
t.updates +=2 ;
136
151
}else {
137
152
int64_t sum = execQuery<int64_t >(txn," select v from t where u=%d" , srcAcc)
@@ -158,7 +173,7 @@ void initializeDatabase()
158
173
connectionconn (cfg.connection );
159
174
if (cfg.nShards ==0 ) {
160
175
worktxn (conn);
161
- exec (txn," insert into t (select generate_series(1 ,%d), 0)" , cfg.nAccounts );
176
+ exec (txn," insert into t (select generate_series(0 ,%d), 0)" , cfg.nAccounts - 1 );
162
177
txn.commit ();
163
178
}else {
164
179
int accountsPerShard = (cfg.nAccounts + cfg.nShards -1 )/cfg.nShards ;
@@ -206,6 +221,9 @@ int main (int argc, char* argv[])
206
221
initialize =true ;
207
222
cfg.nShards =atoi (argv[++i]);
208
223
continue ;
224
+ case ' P' :
225
+ cfg.prepared =true ;
226
+ continue ;
209
227
}
210
228
}
211
229
printf (" Options:\n "
@@ -215,7 +233,8 @@ int main (int argc, char* argv[])
215
233
" \t -n N\t number of iterations (1000)\n "
216
234
" \t -p N\t update percent (100)\n "
217
235
" \t -c STR\t database connection string\n "
218
- " \t -i N\t initialize N shards\n " );
236
+ " \t -i N\t initialize N shards\n "
237
+ " \t -P\t use prepared statements\n " );
219
238
return 1 ;
220
239
}
221
240
@@ -263,7 +282,7 @@ int main (int argc, char* argv[])
263
282
printf (
264
283
" {\" tps\" :%f,\" transactions\" :%ld,"
265
284
" \" selects\" :%ld,\" updates\" :%ld,\" aborts\" :%ld,\" abort_percent\" : %d,"
266
- " \" readers\" :%d,\" writers\" :%d,\" update_percent\" :%d,\" accounts\" :%d,\" iterations\" :%d ,\" shards\" :%d}\n " ,
285
+ " \" readers\" :%d,\" writers\" :%d,\" update_percent\" :%d,\" accounts\" :%d,\" iterations\" :%d ,\" shards\" :%d, \" prepared \" :%d }\n " ,
267
286
(double )(nTransactions*USEC)/elapsed,
268
287
nTransactions,
269
288
nSelects,
@@ -275,7 +294,8 @@ int main (int argc, char* argv[])
275
294
cfg.updatePercent ,
276
295
cfg.nAccounts ,
277
296
cfg.nIterations ,
278
- cfg.nShards );
297
+ cfg.nShards ,
298
+ cfg.prepared );
279
299
280
300
return 0 ;
281
301
}