-
Notifications
You must be signed in to change notification settings - Fork 1
/
ycsbc.cc
236 lines (216 loc) · 6.93 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
//
// ycsbc.cc
// YCSB-C
//
// Created by Jinglei Ren on 12/19/14.
// Copyright (c) 2014 Jinglei Ren <[email protected]>.
//
#include <algorithm>
#include <chrono>
#include <cstring>
#include <memory>
#include <ratio>
#include <string>
#include <iostream>
#include <vector>
#include <future>
#include "core/utils.h"
#include "core/timer.h"
#include "core/client.h"
#include "core/core_workload.h"
#include "db.h"
#include "db/db_factory.h"
#include "histogram.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);
int DelegateClient(shared_ptr<ycsbc::DB> db, ycsbc::CoreWorkload *wl, const int num_ops,
bool is_loading,shared_ptr<utils::Histogram> histogram) {
db->Init();
utils::Timer<utils::t_microseconds> timer;
ycsbc::Client client(db, *wl);
int oks = 0;
for (int i = 0; i < num_ops; ++i) {
timer.Start();
if (is_loading) {
oks += client.DoInsert();
} else {
oks += client.DoTransaction();
}
double duration = timer.End();
histogram->Add_Fast(duration);
}
db->Close();
return oks;
}
int main(const int argc, const char *argv[]) {
utils::Properties props;
string file_name = ParseCommandLine(argc, argv, props);
shared_ptr<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"));
vector<future<int>> actual_ops;
vector<shared_ptr<utils::Histogram>> histogram_list;
utils::Timer<utils::t_microseconds> timer;
int total_ops = 0;
int sum = 0;
if (props.GetProperty("skipload") != "true"){
// Loads data
timer.Start();
total_ops = stoi(props[ycsbc::CoreWorkload::RECORD_COUNT_PROPERTY]);
for (int i = 0; i < num_threads; ++i) {
auto histogram_tmp = make_shared<utils::Histogram>(utils::RecordUnit::h_microseconds);
histogram_list.push_back(histogram_tmp);
actual_ops.emplace_back(async(launch::async,
DelegateClient, db, &wl, total_ops / num_threads, true, histogram_tmp));
}
assert((int)actual_ops.size() == num_threads);
for (auto &n : actual_ops) {
assert(n.valid());
sum += n.get();
}
double duration = timer.End();
utils::Histogram histogram(utils::RecordUnit::h_microseconds);
for (auto &h : histogram_list){
histogram.Merge(*h);
}
cerr << "# Loading records:\t" << sum << endl;
cerr << props["dbname"] << '\t' << file_name << '\t' << num_threads << '\t';
cerr << total_ops / (duration/ histogram.GetRecordUnit()) << " OPS" << endl;
cerr << histogram.ToString() << endl;
}else {
cerr << "# Skipped load records!" << endl;
}
// Peforms transactions
actual_ops.clear();
histogram_list.clear();
total_ops = stoi(props[ycsbc::CoreWorkload::OPERATION_COUNT_PROPERTY]);
timer.Start();
for (int i = 0; i < num_threads; ++i) {
auto histogram_tmp = make_shared<utils::Histogram>(utils::RecordUnit::h_microseconds);
histogram_list.push_back(histogram_tmp);
actual_ops.emplace_back(async(launch::async,
DelegateClient, db, &wl, total_ops / num_threads, false, histogram_tmp));
}
assert((int)actual_ops.size() == num_threads);
sum = 0;
for (auto &n : actual_ops) {
assert(n.valid());
sum += n.get();
}
utils::Histogram histogram(utils::RecordUnit::h_microseconds);
for (auto &h : histogram_list){
histogram.Merge( *h );
}
auto duration = timer.End();
cerr << "# Transaction throughput (KTPS)" << endl;
cerr << props["dbname"] << '\t' << file_name << '\t' << num_threads << '\t';
cerr << total_ops / (duration / histogram.GetRecordUnit()) << " OPS" << endl;
cerr << histogram.ToString() << endl;
}
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], "-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], "-dbpath") == 0){
argindex++;
if(argindex >= argc){
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("dbpath", argv[argindex]);
argindex++;
}else if (strcmp(argv[argindex], "-skipload") == 0){
argindex++;
if(argindex >= argc){
UsageMessage(argv[0]);
exit(0);
}
props.SetProperty("skipload", 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;
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)" << endl;
cout << " -db dbname: specify the name of the DB to use (default: basic)" << endl;
cout << " -dbpath: specify the path of DB location" << endl;
cout << " -skipload: whether to run the load phase, sometime you can run on an existing database" << endl;
cout << " -P propertyfile: load properties from the given file. Multiple files can" << endl;
cout << " be specified, and will be processed in the order specified" << endl;
}
inline bool StrStartWith(const char *str, const char *pre) {
return strncmp(str, pre, strlen(pre)) == 0;
}