Skip to content

Commit 0e1ff43

Browse files
committed
Use parepared statements in dtmbench test
1 parent bea7ac9 commit 0e1ff43

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

contrib/postgres_fdw/tests/dtmbench.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct config
5252
int updatePercent;
5353
int nShards;
5454
string connection;
55+
bool prepared;
5556

5657
config() {
5758
nShards = 1;
@@ -60,6 +61,7 @@ struct config
6061
nIterations = 1000;
6162
nAccounts = 10000;
6263
updatePercent = 100;
64+
prepared = false;
6365
}
6466
};
6567

@@ -76,14 +78,14 @@ static time_t getCurrentTime()
7678
}
7779

7880

79-
void exec(transaction_base& txn, char const* sql, ...)
81+
int exec(transaction_base& txn, char const* sql, ...)
8082
{
8183
va_list args;
8284
va_start(args, sql);
8385
char buf[1024];
8486
vsprintf(buf, sql, args);
8587
va_end(args);
86-
txn.exec(buf);
88+
return txn.exec(buf).affected_rows();
8789
}
8890

8991
template<class T>
@@ -123,15 +125,28 @@ void* writer(void* arg)
123125
{
124126
thread& t = *(thread*)arg;
125127
connection conn(cfg.connection);
128+
if (cfg.prepared) {
129+
conn.prepare("transfer", "update t set v = v + $1 where u=$2");
130+
}
126131
for (int i = 0; i < cfg.nIterations; i++)
127132
{
128133
work txn(conn);
129134
int srcAcc = random() % cfg.nAccounts;
130135
int dstAcc = random() % cfg.nAccounts;
131136
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+
}
135150
t.updates += 2;
136151
} else {
137152
int64_t sum = execQuery<int64_t>(txn, "select v from t where u=%d", srcAcc)
@@ -158,7 +173,7 @@ void initializeDatabase()
158173
connection conn(cfg.connection);
159174
if (cfg.nShards == 0) {
160175
work txn(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);
162177
txn.commit();
163178
} else {
164179
int accountsPerShard = (cfg.nAccounts + cfg.nShards - 1)/cfg.nShards;
@@ -206,6 +221,9 @@ int main (int argc, char* argv[])
206221
initialize = true;
207222
cfg.nShards = atoi(argv[++i]);
208223
continue;
224+
case 'P':
225+
cfg.prepared = true;
226+
continue;
209227
}
210228
}
211229
printf("Options:\n"
@@ -215,7 +233,8 @@ int main (int argc, char* argv[])
215233
"\t-n N\tnumber of iterations (1000)\n"
216234
"\t-p N\tupdate percent (100)\n"
217235
"\t-c STR\tdatabase connection string\n"
218-
"\t-i N\tinitialize N shards\n");
236+
"\t-i N\tinitialize N shards\n"
237+
"\t-P\tuse prepared statements\n");
219238
return 1;
220239
}
221240

@@ -263,7 +282,7 @@ int main (int argc, char* argv[])
263282
printf(
264283
"{\"tps\":%f, \"transactions\":%ld,"
265284
" \"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",
267286
(double)(nTransactions*USEC)/elapsed,
268287
nTransactions,
269288
nSelects,
@@ -275,7 +294,8 @@ int main (int argc, char* argv[])
275294
cfg.updatePercent,
276295
cfg.nAccounts,
277296
cfg.nIterations,
278-
cfg.nShards);
297+
cfg.nShards,
298+
cfg.prepared);
279299

280300
return 0;
281301
}

0 commit comments

Comments
 (0)