forked from liangjk/YCSB-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ycsbc.cc
345 lines (300 loc) · 11.8 KB
/
ycsbc.cc
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
//
// ycsbc.cc
// YCSB-C
//
// Created by Jinglei Ren on 12/19/14.
// Copyright (c) 2014 Jinglei Ren <[email protected]>.
//
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <future>
#include <functional>
#include <algorithm>
#include <unistd.h>
#include <iomanip>
#include <thread>
#include "core/utils.h"
#include "core/timer.h"
#include "core/client.h"
#include "core/core_workload.h"
#include "db/db_factory.h"
using namespace std;
void UsageMessage(const char *command);
bool StrStartWith(const char *str, const char *pre);
string ParseCommandLine(int argc, const char *argv[], utils::Properties &props);
// Execute the workload by one client (thread)
int DelegateClient(ycsbc::DB *db, ycsbc::CoreWorkload *wl, int thread_idx, const int load_ops,
vector<double> *tail_latency) {
db->Init(thread_idx);
ycsbc::Client client(*db, *wl);
int ops_total = 0;
if (load_ops) // loading stage
{
for (int i = 0; i < load_ops; ++i) {
ops_total += client.DoInsert();
}
} else { // running stage
utils::Timer<double> timer;
double last_time = 0;
// calculate the latency of several transactions in 1 second
while (last_time < 1000) {
timer.Start();
int ok = client.DoTransaction();
double t = timer.End(); // span time(ms)
ops_total += ok;
if (tail_latency && ok) {
tail_latency->push_back(t);
}
last_time += t;
}
// sort the tail_latency array in descending order
if (tail_latency)
sort(tail_latency->begin(), tail_latency->end(), greater<double>());
}
db->Close();
return ops_total;
}
int main(const int argc, const char *argv[]) {
utils::Properties props;
string file_name = ParseCommandLine(argc, argv, props);
ycsbc::DB *db = ycsbc::DBFactory::CreateDB(props);
if (!db) {
cout << "Unknown database name " << props["dbname"] << endl;
exit(0);
}
ycsbc::CoreWorkload wl;
wl.Init(props);
const int num_threads = stoi(props.GetProperty("threadcount", "1"));
db->SetDbType(props.GetProperty("dbname"));
if (db->DbType() == "cruisedb") {
db->Begin(1); // just for prepare rocksdb tokenBucket
// sleep 目的:等待后台compaction完成,尽可能清空内存的情况下在开始流量控制
// 防止冷启动问题的发生
// const int SLEEP_TIME = 150;
// sleep(SLEEP_TIME);
db->Begin(2);
}
// Loading Stage
vector<future<int>> worker_threads;
int total_ops = stoi(props[ycsbc::CoreWorkload::RECORD_COUNT_PROPERTY]);
cout << "[YCSB load] Loading records..." << endl;
int load_ops_per_thread = total_ops / num_threads;
for (int i = 0; i < num_threads; ++i) {
if (i == num_threads - 1) {
load_ops_per_thread = total_ops - load_ops_per_thread * i;
}
worker_threads.emplace_back(std::async(launch::async,
DelegateClient, db, &wl, i, load_ops_per_thread, nullptr));
}
assert((int) worker_threads.size() == num_threads);
int sum = 0;
for (auto &n: worker_threads) {
assert(n.valid());
sum += n.get();
}
cout << "[YCSB load] Records finish loaded:\t" << sum << endl;
/////////////////////////////////////////////////////////////////////
if (db->DbType() == "cruisedb") {
db->Begin(1); // just for prepare rocksdb tokenBucket
//
// // sleep 目的:等待后台compaction完成,尽可能清空内存的情况下在开始流量控制
// // 防止冷启动问题的发生
// const int SLEEP_TIME = 150;
// sleep(SLEEP_TIME);
//
db->Begin(2);
}
/////////////////////////////////////////////////////////////////////////
// Running Stage
int current_time = 0;
total_ops = stoi(props[ycsbc::CoreWorkload::OPERATION_COUNT_PROPERTY]);
// latency array in total time
vector<double> total_latency;
total_latency.reserve(total_ops);
// latency array in one second: Descending order
vector<double> sec_latency;
// each thread tail_latency array in one second
vector<vector<double>> tail_latency(num_threads);
// the pointer to each thread tail_latency array
vector<int> latency_ptr(num_threads);
// the heap to store the tail_latency
priority_queue<pair<double, int>> latency_pq;
sum = 0;
// A background thread for debug
//std::cout << "start a background thread for debug.." << std::endl;
//std::atomic<int> finish{0};
//std::future<void> f1;
//if (db->DbType() == "cruisedb") {
// f1 = std::async(std::launch::async, [&]() {
// while (true) {
// std::this_thread::sleep_for(std::chrono::seconds(5));
// db->End();
// if (finish == 1) {
// break;
// }
// }
// });
//}
while (sum < total_ops) {
worker_threads.clear();
for (int i = 0; i < num_threads; ++i) {
worker_threads.emplace_back(std::async(launch::async,
DelegateClient, db, &wl, i, 0, &tail_latency[i]));
}
assert((int) worker_threads.size() == num_threads);
int ops = 0;
for (int i = 0; i < num_threads; ++i) {
assert(worker_threads[i].valid());
ops += worker_threads[i].get();
assert(tail_latency[i].size());
latency_ptr[i] = 0;
latency_pq.emplace(tail_latency[i][0], i);
}
sum += ops;
// the same as multi-way merge, all the latency in one second will send to sec_latency
// sorted in descending order
while (!latency_pq.empty()) {
auto current = latency_pq.top();
latency_pq.pop();
double curr_latency = current.first;
int thread_idx = current.second;
sec_latency.push_back(curr_latency);
total_latency.push_back(curr_latency);
++latency_ptr[thread_idx];
if (latency_ptr[thread_idx] < (int) tail_latency[thread_idx].size()) {
latency_pq.emplace(tail_latency[thread_idx][latency_ptr[thread_idx]], thread_idx);
} else {
tail_latency[thread_idx].clear();
}
}
cout << fixed << setprecision(2)
<< "[YCSB run] " << current_time++ << " sec, total: " << sum << " , ops: " << ops << ", tail_latency(ms): "
<< "Max=" << sec_latency[0]
<< ", Min=" << sec_latency[sec_latency.size() - 1]
<< ", Avg=" << sec_latency[sec_latency.size() / 2]
<< ", 90%=" << sec_latency[sec_latency.size() * 0.1]
<< ", 99%=" << sec_latency[sec_latency.size() * 0.01]
<< ", 99.9%=" << sec_latency[sec_latency.size() * 0.001]
<< ", 99.99%=" << sec_latency[sec_latency.size() * 0.0001] << endl;
// stream to cerr: ops, 90% latency, 99% latency, 99.9% latency, 99.99% latency
cerr << fixed << setprecision(2)
<< ops << ", " << sec_latency[sec_latency.size() * 0.1] << ", " << sec_latency[sec_latency.size() * 0.01]
<< ", " << sec_latency[sec_latency.size() * 0.001] << ", " << sec_latency[sec_latency.size() * 0.0001]
<< endl;
sec_latency.clear();
}
sort(total_latency.begin(), total_latency.end(), greater<double>());
// total latency: Max, Min, Avg, 90%, 99%, 99.9%, 99.99%
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(Max): " << total_latency[0] << "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(Min): " << total_latency[total_latency.size() - 1]
<< "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(Avg): " << total_latency[total_latency.size() / 2]
<< "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(90%): " << total_latency[total_latency.size() * 0.1]
<< "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(99%): " << total_latency[total_latency.size() * 0.01]
<< "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(99.9%): "
<< total_latency[total_latency.size() * 0.001] << "ms" << endl;
cout << fixed << setprecision(2) << "[YCSB] Total tail latency(99.99%): "
<< total_latency[total_latency.size() * 0.0001] << "ms" << endl;
// A background thread for debug
//if (db->DbType() == "cruisedb") {
// finish = 1;
// f1.get();
//}
delete db;
return 0;
}
string ParseCommandLine(int argc, const char *argv[], utils::Properties &props) {
int argindex = 1;
string filename;
while (argindex < argc && StrStartWith(argv[argindex], "-")) {
if (strcmp(argv[argindex], "-h") == 0) {
UsageMessage(argv[0]);
exit(0);
} else if (strcmp(argv[argindex], "-threads") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("threadcount", argv[argindex]);
argindex++;
} else if (strcmp(argv[argindex], "-db") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("dbname", argv[argindex]);
argindex++;
} else if (strcmp(argv[argindex], "-host") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("host", argv[argindex]);
argindex++;
} else if (strcmp(argv[argindex], "-port") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("port", argv[argindex]);
argindex++;
} else if (strcmp(argv[argindex], "-slaves") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("slaves", argv[argindex]);
argindex++;
} else if (strcmp(argv[argindex], "-P") == 0) {
argindex++;
if (argindex >= argc) {
UsageMessage(argv[0]);
exit(0);
}
filename.assign(argv[argindex]);
ifstream input(argv[argindex]);
try {
props.Load(input);
}
catch (const string &message) {
cout << message << endl;
exit(0);
}
input.close();
argindex++;
} else {
cout << "Unknown option '" << argv[argindex] << "'" << endl;
UsageMessage(argv[0]);
exit(0);
}
}
if (argindex == 1 || argindex != argc) {
UsageMessage(argv[0]);
exit(0);
}
return filename;
}
void UsageMessage(const char *command) {
cout << "Usage: " << command << " [options]" << endl;
cout << "Options:" << endl;
cout << " -threads n: execute using n threads (default: 1) [-threads 1]" << endl;
cout << " -db dbname: specify the name of the DB to use (default: basic) [-db rocksdb]" << endl;
cout << " -P propertyFile: load properties from the given file or multiple files. [-P workloads/workloada.spec]"
<< endl;
cout << " 1 > res/w1.out: result log by cout" << endl;
cout << " 2 > res/w1.txt: result(tps,latency) by cerr" << endl;
}
inline bool StrStartWith(const char *str, const char *pre) {
return strncmp(str, pre, strlen(pre)) == 0;
}