-
Notifications
You must be signed in to change notification settings - Fork 80
/
ycsbc.cc
181 lines (163 loc) · 4.89 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
//
// ycsbc.cc
// YCSB-C
//
// Created by Jinglei Ren on 12/19/14.
// Copyright (c) 2014 Jinglei Ren <jinglei@ren.systems>.
//
#include <cstring>
#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/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);
int DelegateClient(ycsbc::DB *db, ycsbc::CoreWorkload *wl, const int num_ops,
bool is_loading) {
db->Init();
ycsbc::Client client(*db, *wl);
int oks = 0;
for (int i = 0; i < num_ops; ++i) {
if (is_loading) {
oks += client.DoInsert();
} else {
oks += client.DoTransaction();
}
}
db->Close();
return oks;
}
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"));
// Loads data
vector<future<int>> actual_ops;
int total_ops = stoi(props[ycsbc::CoreWorkload::RECORD_COUNT_PROPERTY]);
for (int i = 0; i < num_threads; ++i) {
actual_ops.emplace_back(async(launch::async,
DelegateClient, db, &wl, total_ops / num_threads, true));
}
assert((int)actual_ops.size() == num_threads);
int sum = 0;
for (auto &n : actual_ops) {
assert(n.valid());
sum += n.get();
}
cerr << "# Loading records:\t" << sum << endl;
// Peforms transactions
actual_ops.clear();
total_ops = stoi(props[ycsbc::CoreWorkload::OPERATION_COUNT_PROPERTY]);
utils::Timer<double> timer;
timer.Start();
for (int i = 0; i < num_threads; ++i) {
actual_ops.emplace_back(async(launch::async,
DelegateClient, db, &wl, total_ops / num_threads, false));
}
assert((int)actual_ops.size() == num_threads);
sum = 0;
for (auto &n : actual_ops) {
assert(n.valid());
sum += n.get();
}
double duration = timer.End();
cerr << "# Transaction throughput (KTPS)" << endl;
cerr << props["dbname"] << '\t' << file_name << '\t' << num_threads << '\t';
cerr << total_ops / duration / 1000 << 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], "-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 << " -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;
}