-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench.cc
324 lines (283 loc) · 9.34 KB
/
bench.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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2015-Present Couchbase, Inc.
*
* Use of this software is governed by the Business Source License included
* in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
* in that file, in accordance with the Business Source License, use of this
* software will be governed by the Apache License, Version 2.0, included in
* the file licenses/APL2.txt.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <stdexcept>
#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <chrono>
#define CLIOPTS_ENABLE_CXX
#define INCLUDE_SUBDOC_NTOHLL
#include "subdoc/subdoc-api.h"
#include "subdoc/path.h"
#include "subdoc/match.h"
#include "subdoc/operations.h"
#include "contrib/cliopts/cliopts.h"
using std::string;
using std::vector;
using std::map;
using namespace cliopts;
using Subdoc::Path;
using Subdoc::Operation;
using Subdoc::Command;
using Subdoc::Error;
using Subdoc::Result;
struct OpEntry {
uint8_t opcode;
const char *description;
OpEntry(uint8_t opcode = 0, const char *description = NULL) :
opcode(opcode),
description(description) {}
operator uint8_t() const { return opcode; }
};
class Options {
public:
Options() :
o_iter('i', "iterations", 1000),
o_path('p', "docpath"),
o_value('v', "value"),
o_jsfile('f', "json"),
o_cmd('c', "command"),
o_mkdirp('M', "create-intermediate"),
o_txtscan('T', "text-scan"),
parser("subdoc-bench")
{
o_iter.description("Number of iterations to run");
o_path.description("Document path to manipulate");
o_value.description("Document value to insert");
o_jsfile.description("JSON files to operate on. If passing multiple files, each file should be delimited by a comma");
o_cmd.description("Command to use. Use -c help to show all the commands").mandatory();
o_mkdirp.description("Create intermediate paths for mutation operations");
o_txtscan.description("Simply scan the text using a naive approach. Used to see how much actual overhead jsonsl induces");
parser.addOption(o_iter);
parser.addOption(o_path);
parser.addOption(o_value);
parser.addOption(o_jsfile);
parser.addOption(o_cmd);
parser.addOption(o_mkdirp);
parser.addOption(o_txtscan);
totalBytes = 0;
// Set the opmap
initOpmap();
}
void initOpmap() {
// generic ops:
opmap["replace"] = OpEntry(Command::REPLACE, "Replace a value");
opmap["delete"] = OpEntry(Command::REMOVE, "Delete a value");
opmap["get"] = OpEntry(Command::GET, "Retrieve a value");
opmap["exists"] = OpEntry(Command::EXISTS, "Check if a value exists");
// dict ops
opmap["add"] = OpEntry(Command::DICT_ADD, "Create a new dictionary value");
opmap["upsert"] = OpEntry(Command::DICT_UPSERT, "Create or replace a dictionary value");
// list ops
opmap["append"] = OpEntry(Command::ARRAY_APPEND, "Insert values to the end of an array");
opmap["prepend"] = OpEntry(Command::ARRAY_PREPEND, "Insert values to the beginning of an array");
opmap["addunique"] = OpEntry(Command::ARRAY_ADD_UNIQUE, "Add a unique value to an array");
opmap["insert"] = OpEntry(Command::ARRAY_INSERT, "Insert value at given array index");
opmap["size"] = OpEntry(Command::GET_COUNT, "Count the number of items in an array or dict");
// arithmetic ops
opmap["counter"] = OpEntry(Command::COUNTER);
// Generic ops
opmap["path"] = OpEntry(0xff, "Check the validity of a path");
}
UIntOption o_iter;
StringOption o_path;
StringOption o_value;
StringOption o_jsfile;
StringOption o_cmd;
BoolOption o_mkdirp;
BoolOption o_txtscan;
map<string,OpEntry> opmap;
Parser parser;
size_t totalBytes;
};
static void
readJsonFile(string& name, vector<string>& out)
{
std::ifstream input(name.c_str());
if (input.fail()) {
throw name + ": " + strerror(errno);
}
fprintf(stderr, "Reading %s\n", name.c_str());
std::stringstream ss;
ss << input.rdbuf();
out.push_back(ss.str());
input.close();
}
static void
execOperation(Options& o)
{
vector<string> fileNames;
vector<string> inputStrs;
string flist = o.o_jsfile.const_result();
if (flist.find(',') == string::npos) {
fileNames.push_back(flist);
} else {
while (true) {
size_t pos = flist.find_first_of(',');
if (pos == string::npos) {
fileNames.push_back(flist);
break;
} else {
string curName = flist.substr(0, pos);
fileNames.push_back(curName);
flist = flist.substr(pos+1);
}
}
}
if (fileNames.empty()) {
throw string("At least one file must be passed!");
}
for (size_t ii = 0; ii < fileNames.size(); ii++) {
readJsonFile(fileNames[ii], inputStrs);
o.totalBytes += inputStrs.back().length();
}
uint8_t opcode = o.opmap[o.o_cmd.result()];
if (o.o_mkdirp.passed()) {
opcode |= 0x80;
}
string value = o.o_value.const_result();
string path = o.o_path.const_result();
Operation op;
Result res;
size_t nquotes = 0;
bool is_txtscan = o.o_txtscan.result();
size_t itermax = o.o_iter.result();
int char_table[256] = { 0 };
char_table[static_cast<uint8_t >('"')] = 1;
char_table[static_cast<uint8_t >('\\')] = 1;
char_table[static_cast<uint8_t >('!')] = 1;
for (size_t ii = 0; ii < itermax; ii++) {
const string& curInput = inputStrs[ii % inputStrs.size()];
if (is_txtscan) {
const char *buf = curInput.c_str();
size_t nbytes = curInput.size();
for (; nbytes; buf++, nbytes--) {
if (char_table[static_cast<unsigned char>(*buf)]) {
nquotes++;
}
}
continue;
}
op.clear();
res.clear();
op.set_value(value);
op.set_code(opcode);
op.set_doc(curInput);
op.set_result_buf(&res);
Error rv = op.op_exec(path);
if (!rv.success()) {
throw rv;
}
}
if (nquotes) {
printf("Found %lu quotes!\n", nquotes);
}
// Print the result.
if (opcode == Command::GET ||
opcode == Command::EXISTS ||
opcode == Command::GET_COUNT) {
string match = res.matchloc().to_string();
printf("%s\n", match.c_str());
} else {
string newdoc;
for (auto ii : res.newdoc()) {
newdoc.append(ii.at, ii.length);
}
printf("%s\n", newdoc.c_str());
}
}
static void
execPathParse(Options& o)
{
size_t itermax = o.o_iter.result();
string path = o.o_path.const_result();
Path pth;
for (size_t ii = 0; ii < itermax; ii++) {
pth.clear();
int rv = pth.parse(path);
if (rv != 0) {
throw string("Failed to parse path!");
}
}
}
void runMain(int argc, char **argv)
{
using namespace std::chrono;
Options o;
if (!o.parser.parse(argc, argv)) {
throw string("Bad options!");
}
// Determine the command
string cmdStr = o.o_cmd.const_result();
auto t_begin = steady_clock::now();
if (cmdStr == "help") {
map<string,OpEntry>::const_iterator iter = o.opmap.begin();
for (; iter != o.opmap.end(); ++iter) {
const OpEntry& ent = iter->second;
fprintf(stderr, "%s (0x%x): ", iter->first.c_str(), ent.opcode);
fprintf(stderr, "%s\n", ent.description);
}
exit(EXIT_SUCCESS);
}
if (!o.o_path.passed()) {
fprintf(stderr, "Path (-p) required\n");
exit(EXIT_FAILURE);
}
if (o.opmap.find(cmdStr) != o.opmap.end()) {
if (!o.o_jsfile.passed()) {
throw string("Operation must contain file!");
}
execOperation(o);
} else if (cmdStr == "path") {
execPathParse(o);
} else {
throw string("Unknown command!");
}
auto total = duration_cast<duration<double>>(steady_clock::now() - t_begin);
// Get the number of seconds:
double n_seconds = total.count();
double ops_per_sec = static_cast<double>(o.o_iter.result()) / n_seconds;
double mb_per_sec = (
static_cast<double>(o.totalBytes) *
static_cast<double>(o.o_iter.result())) /
n_seconds;
mb_per_sec /= (1024 * 1024);
fprintf(stderr, "DURATION=%.2fs. OPS=%u\n", n_seconds, o.o_iter.result());
fprintf(stderr, "%.2f OPS/s\n", ops_per_sec);
fprintf(stderr, "%.2f MB/s\n", mb_per_sec);
}
int main(int argc, char **argv)
{
try {
runMain(argc, argv);
return EXIT_SUCCESS;
} catch (string& exc) {
fprintf(stderr, "%s\n", exc.c_str());
return EXIT_FAILURE;
} catch (Error& rc) {
fprintf(stderr, "Command failed: %s\n", rc.description());
return EXIT_FAILURE;
} catch (std::exception& ex) {
fprintf(stderr, "Command failed: %s\n", ex.what());
return EXIT_FAILURE;
}
}