@@ -40,14 +40,18 @@ typedef uint32_t xid_t;
40
40
struct thread
41
41
{
42
42
pthread_t t;
43
- size_t proceeded;
43
+ size_t transactions;
44
+ size_t updates;
45
+ size_t selects;
44
46
size_t aborts;
45
47
int id;
46
48
47
49
void start (int tid, thread_proc_t proc) {
48
50
id = tid;
49
- proceeded = 0 ;
51
+ updates = 0 ;
52
+ selects = 0 ;
50
53
aborts = 0 ;
54
+ transactions = 0 ;
51
55
pthread_create (&t, NULL , proc, this );
52
56
}
53
57
@@ -62,13 +66,15 @@ struct config
62
66
int nWriters;
63
67
int nIterations;
64
68
int nAccounts;
69
+ int updatePercent;
65
70
vector<string> connections;
66
71
67
72
config () {
68
73
nReaders = 1 ;
69
74
nWriters = 10 ;
70
75
nIterations = 1000 ;
71
76
nAccounts = 100000 ;
77
+ updatePercent = 100 ;
72
78
}
73
79
};
74
80
@@ -123,7 +129,8 @@ void* reader(void* arg)
123
129
printf (" Total=%ld\n " , sum);
124
130
prevSum = sum;
125
131
}
126
- t.proceeded += 1 ;
132
+ t.transactions += 1 ;
133
+ t.selects += 1 ;
127
134
txn.commit ();
128
135
}
129
136
return NULL ;
@@ -142,16 +149,26 @@ void* writer(void* arg)
142
149
int srcAcc = random () % cfg.nAccounts ;
143
150
int dstAcc = random () % cfg.nAccounts ;
144
151
try {
145
- exec (txn, " update t set v = v - 1 where u=%d" , srcAcc);
146
- exec (txn, " update t set v = v + 1 where u=%d" , dstAcc);
152
+ if (random () % 100 < cfg.updatePercent ) {
153
+ exec (txn, " update t set v = v - 1 where u=%d" , srcAcc);
154
+ exec (txn, " update t set v = v + 1 where u=%d" , dstAcc);
155
+ t.updates += 2 ;
156
+ } else {
157
+ int64_t sum = execQuery (txn, " select v from t where u=%d" , srcAcc)
158
+ + execQuery (txn, " select v from t where u=%d" , dstAcc);
159
+ if (sum > cfg.nIterations *cfg.nWriters || sum < -cfg.nIterations *cfg.nWriters ) {
160
+ printf (" Wrong sum=%ld\n " , sum);
161
+ }
162
+ t.selects += 2 ;
163
+ }
147
164
txn.commit ();
165
+ t.transactions += 1 ;
148
166
} catch (pqxx_exception const & x) {
149
167
txn.abort ();
150
168
t.aborts += 1 ;
151
169
i -= 1 ;
152
170
continue ;
153
171
}
154
- t.proceeded += 1 ;
155
172
}
156
173
return NULL ;
157
174
}
@@ -188,6 +205,9 @@ int main (int argc, char* argv[])
188
205
case ' n' :
189
206
cfg.nIterations = atoi (argv[++i]);
190
207
continue ;
208
+ case ' p' :
209
+ cfg.updatePercent = atoi (argv[++i]);
210
+ continue ;
191
211
case ' c' :
192
212
cfg.connections .push_back (string (argv[++i]));
193
213
continue ;
@@ -201,6 +221,7 @@ int main (int argc, char* argv[])
201
221
" \t -w N\t number of writers (10)\n "
202
222
" \t -a N\t number of accounts (100000)\n "
203
223
" \t -n N\t number of iterations (1000)\n "
224
+ " \t -p N\t update percent (100)\n "
204
225
" \t -c STR\t database connection string\n "
205
226
" \t -i\t initialize database\n " );
206
227
return 1 ;
@@ -216,10 +237,11 @@ int main (int argc, char* argv[])
216
237
217
238
vector<thread> readers (cfg.nReaders );
218
239
vector<thread> writers (cfg.nWriters );
219
- size_t nReads = 0 ;
220
- size_t nWrites = 0 ;
221
240
size_t nAborts = 0 ;
222
-
241
+ size_t nUpdates = 0 ;
242
+ size_t nSelects = 0 ;
243
+ size_t nTransactions = 0 ;
244
+
223
245
for (int i = 0 ; i < cfg.nReaders ; i++) {
224
246
readers[i].start (i, reader);
225
247
}
@@ -229,29 +251,35 @@ int main (int argc, char* argv[])
229
251
230
252
for (int i = 0 ; i < cfg.nWriters ; i++) {
231
253
writers[i].wait ();
232
- nWrites += writers[i].proceeded ;
254
+ nUpdates += writers[i].updates ;
255
+ nSelects += writers[i].selects ;
233
256
nAborts += writers[i].aborts ;
257
+ nTransactions += writers[i].transactions ;
234
258
}
235
259
236
260
running = false ;
237
261
238
262
for (int i = 0 ; i < cfg.nReaders ; i++) {
239
263
readers[i].wait ();
240
- nReads += readers[i].proceeded ;
264
+ nSelects += readers[i].selects ;
265
+ nTransactions += writers[i].transactions ;
241
266
}
242
267
243
268
time_t elapsed = getCurrentTime () - start;
244
269
245
270
printf (
246
- " {\" update_tps\" :%f, \" read_tps\" :%f,"
247
- " \" readers\" :%d, \" writers\" :%d, \" aborts\" :%ld, \" abort_percent\" : %d,"
248
- " \" accounts\" :%d, \" iterations\" :%d, \" hosts\" :%ld}\n " ,
249
- (double )(nWrites*USEC)/elapsed,
250
- (double )(nReads*USEC)/elapsed,
271
+ " {\" tps\" :%f, \" transactions\" :%ld,"
272
+ " \" selects\" :%ld, \" updates\" :%ld, \" aborts\" :%ld, \" abort_percent\" : %d,"
273
+ " \" readers\" :%d, \" writers\" :%d, \" update_percent\" :%d, \" accounts\" :%d, \" iterations\" :%d, \" hosts\" :%ld}\n " ,
274
+ (double )(nTransactions*USEC)/elapsed,
275
+ nTransactions,
276
+ nSelects,
277
+ nUpdates,
278
+ nAborts,
279
+ (int )(nAborts*100 /nTransactions),
251
280
cfg.nReaders ,
252
281
cfg.nWriters ,
253
- nAborts,
254
- (int )(nAborts*100 /nWrites),
282
+ cfg.updatePercent ,
255
283
cfg.nAccounts ,
256
284
cfg.nIterations ,
257
285
cfg.connections .size ()
0 commit comments