-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchbench.cpp
More file actions
443 lines (395 loc) · 18.6 KB
/
Copy pathchbench.cpp
File metadata and controls
443 lines (395 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Standalone CH-benchmark driver against a Postgres/CedarDB connection string.
// Assumes the TPC-C + CH schema (see ddl-postgres-*.sql) is already populated.
// Runs TPC-C client threads plus optional CH analytical threads.
// Two modes: stored-procedure calls (--mode sproc, default) and
// individual-statement execution matching benchbase behaviour (--mode benchbase).
#include "chbench_base.h"
#include "tpcc_benchbase.h"
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
namespace chbench {
//---------------------------------------------------------------------------
// Argument parsing
//---------------------------------------------------------------------------
static void printHelp(const char* argv0) {
std::cout
<< "Usage: " << argv0 << " [options]\n"
<< " --conn <str> Postgres connection string (required)\n"
<< " --scale <n> Scale factor = warehouse count = client thread count (required)\n"
<< " --olap-threads <n> Number of analytical clients (default 0)\n"
<< " --pipeline-depth <n> Transactions per libpq pipeline batch (default 1, sproc mode only)\n"
<< " --warmup <sec> Warmup duration (default 5)\n"
<< " --measure <sec> Measurement duration (default 30)\n"
<< " --target-rate <n> Global TPC-C transaction rate cap, tx/s (default 0 = uncapped)\n"
<< " --csv <path> Latency stats output path (default chbench-stats.csv)\n"
<< " --target-system <s> cedar | postgres (default cedar) — picks functions_<s>.sql\n"
<< " --script-dir <path> SQL script dir (default: <binary-dir>/sql)\n"
<< " --mode <s> sproc | benchbase (default sproc)\n"
<< " -h, --help Show help\n";
}
static bool parseArgs(int argc, char** argv, Options& opt) {
auto need = [&](int& i) -> const char* {
if (i + 1 >= argc) { std::cerr << "missing value for " << argv[i] << "\n"; return nullptr; }
return argv[++i];
};
for (int i = 1; i < argc; ++i) {
std::string_view a(argv[i]);
if (a == "-h" || a == "--help") { printHelp(argv[0]); std::exit(0); }
else if (a == "--conn") { auto v = need(i); if (!v) return false; opt.connString = v; }
else if (a == "--scale") { auto v = need(i); if (!v) return false; opt.scaleFactor = std::stoul(v); }
else if (a == "--olap-threads") { auto v = need(i); if (!v) return false; opt.olapThreads = std::stoul(v); }
else if (a == "--pipeline-depth"){ auto v = need(i); if (!v) return false; opt.pipelineDepth = std::stoul(v); }
else if (a == "--warmup") { auto v = need(i); if (!v) return false; opt.warmupTime = std::stoul(v); }
else if (a == "--measure") { auto v = need(i); if (!v) return false; opt.measureTime = std::stoul(v); }
else if (a == "--target-rate") { auto v = need(i); if (!v) return false; opt.targetRate = std::stoul(v); }
else if (a == "--csv") { auto v = need(i); if (!v) return false; opt.csvPath = v; }
else if (a == "--target-system") { auto v = need(i); if (!v) return false; opt.targetSystem = v; }
else if (a == "--script-dir") { auto v = need(i); if (!v) return false; opt.scriptDir = v; }
else if (a == "--mode") { auto v = need(i); if (!v) return false; opt.mode = v; }
else { std::cerr << "unknown option: " << a << "\n"; return false; }
}
if (opt.connString.empty()) { std::cerr << "--conn is required\n"; return false; }
if (!opt.scaleFactor) { std::cerr << "--scale is required and must be > 0\n"; return false; }
if (opt.targetSystem != "cedar" && opt.targetSystem != "postgres") {
std::cerr << "--target-system must be 'cedar' or 'postgres'\n";
return false;
}
if (opt.mode != "sproc" && opt.mode != "benchbase") {
std::cerr << "--mode must be 'sproc' or 'benchbase'\n";
return false;
}
return true;
}
//---------------------------------------------------------------------------
// Helpers used only in this translation unit
//---------------------------------------------------------------------------
[[noreturn]] static void die(const std::string& what) {
std::cerr << what << "\n";
std::exit(1);
}
static std::string defaultScriptDir(const char* argv0) {
try {
auto p = std::filesystem::canonical(argv0).parent_path() / "sql";
if (std::filesystem::exists(p)) return p.string();
} catch (...) {}
return "sql";
}
//---------------------------------------------------------------------------
// TPC-C stored-procedure client
//---------------------------------------------------------------------------
class TpccClient final : public Client {
public:
enum Kind : unsigned { KPayment = 0, KOrderStatus, KDelivery, KStockLevel, KNewOrder, KCount };
private:
unsigned homeWarehouseId;
unsigned warehouseCount = 0;
std::mt19937 rng;
BoundStmt delivery, newOrder, orderStatus, payment, stockLevel;
std::pair<const BoundStmt*, unsigned> pickTransaction() {
std::uniform_int_distribution<int> dist(1, 100);
int dice = dist(rng);
if (dice <= 43) return {&payment, KPayment};
dice -= 43;
if (dice <= 4) return {&orderStatus, KOrderStatus};
dice -= 4;
if (dice <= 4) return {&delivery, KDelivery};
dice -= 4;
if (dice <= 4) return {&stockLevel, KStockLevel};
return {&newOrder, KNewOrder};
}
protected:
void prepareImpl() override {
stats.assign(KCount, TxStats{});
connection.exec(scripts.session);
warehouseCount = opt.scaleFactor; // client count == warehouse count
connection.prepare("delivery", "CALL delivery($1::INTEGER)", 1);
connection.prepare("newOrder", "CALL newOrder($1::INTEGER, $2::INTEGER)", 2);
connection.prepare("orderStatus", "CALL orderStatus($1::INTEGER)", 1);
connection.prepare("payment", "CALL payment($1::INTEGER, $2::INTEGER)", 2);
connection.prepare("stockLevel", "CALL stockLevel($1::INTEGER)", 1);
auto w = std::to_string(homeWarehouseId);
auto wc = std::to_string(warehouseCount);
delivery.name = "delivery"; delivery.setParams({w});
newOrder.name = "newOrder"; newOrder.setParams({w, wc});
orderStatus.name = "orderStatus"; orderStatus.setParams({w});
payment.name = "payment"; payment.setParams({w, wc});
stockLevel.name = "stockLevel"; stockLevel.setParams({w});
}
void runImpl() override {
// Global TPC-C rate cap split evenly across client threads. Convert to
// per-thread inter-batch interval in nanoseconds: a thread sending
// pipelineDepth tx per batch needs one batch every
// (pipelineDepth * scaleFactor / targetRate) seconds.
uint64_t intervalNs = 0;
if (opt.targetRate) {
intervalNs = static_cast<uint64_t>(
1e9 * opt.pipelineDepth * opt.scaleFactor / opt.targetRate);
}
runPipeline(connection, processed, done, opt.pipelineDepth, intervalNs,
[&]() { return pickTransaction(); },
[&](unsigned kind, uint64_t ns) { maybeRecord(kind, ns); });
}
public:
TpccClient(const Options& o, const Scripts& s, unsigned index)
: Client(o, s), homeWarehouseId(index + 1), rng(42u * (index + 1)) {}
std::vector<std::string> labels() const override {
return {"payment", "orderStatus", "delivery", "stockLevel", "newOrder"};
}
const char* category() const override { return "tpcc"; }
};
//---------------------------------------------------------------------------
// CH analytical client
//---------------------------------------------------------------------------
class ChClient final : public Client {
static constexpr unsigned queries = 22;
std::mt19937 rng;
std::vector<BoundStmt> statements;
std::pair<const BoundStmt*, unsigned> pickTransaction() {
std::uniform_int_distribution<unsigned> dist(0, queries - 1);
unsigned k = dist(rng);
return {&statements[k], k};
}
protected:
void prepareImpl() override {
stats.assign(queries, TxStats{});
statements.reserve(queries);
for (unsigned i = 1; i <= queries; ++i) {
auto name = "q" + std::to_string(i);
connection.prepare(name, scripts.queries[i - 1], 0);
BoundStmt s;
s.name = name;
statements.push_back(std::move(s));
}
}
void runImpl() override {
// OLAP queries use pipeline depth 1 (matches tools/oltp ChClient). OLAP
// is not affected by --target-rate; that flag caps TPC-C only.
runPipeline(connection, processed, done, 1, /*targetIntervalNs=*/0,
[&]() { return pickTransaction(); },
[&](unsigned kind, uint64_t ns) { maybeRecord(kind, ns); },
/*tolerateQueryErrors=*/true);
}
public:
ChClient(const Options& o, const Scripts& s, unsigned index)
: Client(o, s), rng(42u * (index + 1)) {}
std::vector<std::string> labels() const override {
std::vector<std::string> r;
r.reserve(queries);
for (unsigned i = 1; i <= queries; ++i) r.push_back("q" + std::to_string(i));
return r;
}
const char* category() const override { return "olap"; }
};
//---------------------------------------------------------------------------
// Driver
//---------------------------------------------------------------------------
static std::string formatTime(unsigned seconds) {
std::stringstream out;
out << std::setfill('0') << std::setw(2) << (seconds / 60) << ":"
<< std::setfill('0') << std::setw(2) << (seconds % 60);
return out.str();
}
static void preparePopulationIndependent(const Options& opt, const Scripts& scripts) {
Connection conn;
conn.open(opt.connString);
// Use the *last* procedure defined in functions.sql as the sentinel so we
// only skip the install when a prior run completed the whole script. If a
// previous attempt was interrupted mid-install, stockLevel won't exist and
// we'll re-run (which will surface the duplicate-object error from the
// earlier procedures, prompting the user to clean up).
PGresult* res = PQexec(conn.raw(),
"SELECT 1 FROM pg_proc WHERE lower(proname) = 'stocklevel' LIMIT 1");
bool installed = (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) > 0);
PQclear(res);
if (installed) {
std::cout << "functions already installed, skipping " << opt.scriptDir
<< "/functions_" << opt.targetSystem << ".sql\n";
return;
}
std::cout << "installing functions from " << opt.scriptDir
<< "/functions_" << opt.targetSystem << ".sql...\n";
conn.exec(scripts.functions);
}
static uint64_t countProcessed(const std::vector<std::unique_ptr<Client>>& clients) {
uint64_t r = 0;
for (auto& c : clients) r += c->countProcessed();
return r;
}
// Merge per-kind stats across a homogeneous group of clients (same labels).
static std::vector<std::pair<std::string, TxStats>> mergeStats(
const std::vector<std::unique_ptr<Client>>& clients) {
std::vector<std::pair<std::string, TxStats>> out;
if (clients.empty()) return out;
auto labels = clients.front()->labels();
out.resize(labels.size());
for (size_t i = 0; i < labels.size(); ++i) out[i].first = labels[i];
for (auto& c : clients) {
const auto& s = c->getStats();
for (size_t i = 0; i < s.size() && i < out.size(); ++i)
out[i].second.merge(s[i]);
}
return out;
}
static void writeCsv(const std::string& path,
const std::vector<std::unique_ptr<Client>>& tpcc,
const std::vector<std::unique_ptr<Client>>& olap) {
std::ofstream ofs(path);
if (!ofs) {
std::cerr << "warning: cannot open " << path << " for writing\n";
return;
}
ofs << "category,name,count,min_ms,median_ms,p95_ms,p99_ms\n";
constexpr double NS_PER_MS = 1'000'000.0;
auto dump = [&](const char* category,
const std::vector<std::pair<std::string, TxStats>>& rows) {
for (auto& [name, s] : rows) {
double minMs = s.count ? s.minNs / NS_PER_MS : 0.0;
double medianMs = s.count ? s.percentile(0.50) / NS_PER_MS : 0.0;
double p95Ms = s.count ? s.percentile(0.95) / NS_PER_MS : 0.0;
double p99Ms = s.count ? s.percentile(0.99) / NS_PER_MS : 0.0;
ofs << category << ',' << name << ',' << s.count << ','
<< std::fixed << std::setprecision(3)
<< minMs << ',' << medianMs << ',' << p95Ms << ',' << p99Ms << '\n';
ofs.unsetf(std::ios::floatfield);
}
};
if (!tpcc.empty()) dump(tpcc.front()->category(), mergeStats(tpcc));
if (!olap.empty()) dump(olap.front()->category(), mergeStats(olap));
std::cout << "wrote latency stats to " << path << "\n";
// Also print a short per-line summary to stdout.
auto printGroup = [](const char* cat,
const std::vector<std::pair<std::string, TxStats>>& rows) {
if (rows.empty()) return;
std::cout << "\n" << cat << " latencies (ms):\n";
std::cout << std::left << std::setw(14) << " name"
<< std::right << std::setw(10) << "count"
<< std::setw(12) << "min"
<< std::setw(12) << "median"
<< std::setw(12) << "p95"
<< std::setw(12) << "p99" << "\n";
constexpr double NS_PER_MS = 1'000'000.0;
for (auto& [name, s] : rows) {
double minMs = s.count ? s.minNs / NS_PER_MS : 0.0;
double medianMs = s.count ? s.percentile(0.50) / NS_PER_MS : 0.0;
double p95Ms = s.count ? s.percentile(0.95) / NS_PER_MS : 0.0;
double p99Ms = s.count ? s.percentile(0.99) / NS_PER_MS : 0.0;
std::cout << " " << std::left << std::setw(12) << name
<< std::right << std::setw(10) << s.count
<< std::fixed << std::setprecision(3)
<< std::setw(12) << minMs
<< std::setw(12) << medianMs
<< std::setw(12) << p95Ms
<< std::setw(12) << p99Ms << "\n";
std::cout.unsetf(std::ios::floatfield);
}
};
if (!tpcc.empty()) printGroup(tpcc.front()->category(), mergeStats(tpcc));
if (!olap.empty()) printGroup(olap.front()->category(), mergeStats(olap));
}
static void runBenchmark(const Options& opt, const Scripts& scripts) {
unsigned clientCount = opt.scaleFactor;
unsigned olapCount = opt.olapThreads;
std::cout << "preparing " << clientCount << " TPC-C clients ("
<< opt.mode << ") + " << olapCount << " OLAP clients...\n\n";
std::vector<std::unique_ptr<Client>> clients;
clients.reserve(clientCount);
for (unsigned i = 0; i < clientCount; ++i) {
if (opt.mode == "benchbase")
clients.emplace_back(std::make_unique<TpccBenchbaseClient>(opt, scripts, i));
else
clients.emplace_back(std::make_unique<TpccClient>(opt, scripts, i));
clients.back()->prepare();
}
std::vector<std::unique_ptr<Client>> olapClients;
olapClients.reserve(olapCount);
for (unsigned i = 0; i < olapCount; ++i) {
olapClients.emplace_back(std::make_unique<ChClient>(opt, scripts, i));
olapClients.back()->prepare();
}
auto checkFailure = [&](const char* msg) {
bool anyFailed = false;
for (auto& c : clients) if (c->isFailed()) anyFailed = true;
for (auto& c : olapClients) if (c->isFailed()) anyFailed = true;
if (anyFailed) {
for (auto& c : clients) c->stop();
for (auto& c : olapClients) c->stop();
die(msg);
}
};
for (auto& c : clients) c->waitUntilPrepared();
for (auto& c : olapClients) c->waitUntilPrepared();
checkFailure("error while preparing benchmark run");
for (auto& c : clients) c->run();
for (auto& c : olapClients) c->run();
auto runPhase = [&](const char* label, unsigned duration) {
if (!duration) return std::pair<uint64_t, uint64_t>{0, 0};
auto begin = std::chrono::steady_clock::now();
auto processed = countProcessed(clients);
auto olap = countProcessed(olapClients);
auto beginProcessed = processed;
auto beginOlap = olap;
for (unsigned i = 1; i <= duration; ++i) {
std::this_thread::sleep_until(begin + std::chrono::seconds(i));
auto next = countProcessed(clients);
auto nextOlap = countProcessed(olapClients);
std::cout << label << " [ " << formatTime(i) << " / " << formatTime(duration) << " ]: "
<< (next - processed) << " tx/s (" << next << " processed)";
if (olapCount)
std::cout << ", " << (nextOlap - olap) << " queries/s (" << nextOlap << " queries)";
std::cout << "\n";
processed = next;
olap = nextOlap;
}
return std::pair<uint64_t, uint64_t>{processed - beginProcessed, olap - beginOlap};
};
runPhase("WARMUP ", opt.warmupTime);
// Enable latency recording only during the measure phase so warmup latencies
// don't pollute min/max/avg.
for (auto& c : clients) c->setRecording(true);
for (auto& c : olapClients) c->setRecording(true);
auto [txs, qs] = runPhase("MEASURE", opt.measureTime);
for (auto& c : clients) c->setRecording(false);
for (auto& c : olapClients) c->setRecording(false);
if (opt.measureTime) {
std::cout << "\nMEASURE THROUGHPUT: "
<< (static_cast<double>(txs) / opt.measureTime) << " tx/s ("
<< txs << " processed)";
if (olapCount)
std::cout << ", " << (static_cast<double>(qs) / opt.measureTime) << " queries/s ("
<< qs << " queries)";
std::cout << "\n";
}
// Signal stop to every client first (including query cancellation) so their
// threads wake concurrently; then join. Otherwise total shutdown would be
// the SUM of per-client in-flight query durations instead of the MAX.
std::cout << "\nGathering statistics...\n" << std::flush;
for (auto& c : clients) c->requestStop();
for (auto& c : olapClients) c->requestStop();
for (auto& c : clients) c->join();
for (auto& c : olapClients) c->join();
checkFailure("error while executing benchmark run");
writeCsv(opt.csvPath, clients, olapClients);
}
} // namespace chbench
int main(int argc, char** argv) {
chbench::Options opt;
if (!chbench::parseArgs(argc, argv, opt)) return 1;
if (opt.scriptDir.empty()) opt.scriptDir = chbench::defaultScriptDir(argv[0]);
try {
chbench::Scripts scripts;
scripts.load(opt.scriptDir, opt.targetSystem, opt.mode == "benchbase");
if (opt.mode == "sproc")
chbench::preparePopulationIndependent(opt, scripts);
chbench::runBenchmark(opt, scripts);
} catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}