-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestbench.cpp
More file actions
388 lines (302 loc) · 10.5 KB
/
testbench.cpp
File metadata and controls
388 lines (302 loc) · 10.5 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
#include "genId.hpp"
#include "lits/lits.hpp"
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <sys/time.h>
#include <vector>
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
typedef enum {
idcards = 0,
randstr = 1,
} strType;
// Randomly generated keys (idcard)
std::vector<std::string> keys;
const int default_key_cnt = 2e6;
const int default_search_cnt = 1e6;
const int default_scan_cnt = 1e5;
const int default_scan_range = 100;
// std::string will store strings with a length less than 16 locally. To ensure
// unified memory access for the data, we store all the data in a buffer.
// For bulk load
char *bulk_data;
char **bulk_keys;
uint64_t *bulk_vals;
int num_of_bulk;
// For search
char *search_data;
char **search_keys;
int num_of_search;
// For insert
char *insert_data;
char **insert_keys;
uint64_t dummy_value = 982;
int num_of_insert;
void generateKeys(strType t) {
keys = IdGenerator::getKeys(default_key_cnt, t);
}
void _Myfree(void *&addr) {
if (addr) {
free(addr);
addr = NULL;
}
}
void freeData() {
_Myfree((void *&)bulk_data);
_Myfree((void *&)bulk_keys);
_Myfree((void *&)bulk_vals);
_Myfree((void *&)search_data);
_Myfree((void *&)search_keys);
_Myfree((void *&)insert_data);
_Myfree((void *&)insert_keys);
}
void prepareSearchQuerys() {
std::cout << "[Info]: Preparing search queries ..." << std::endl;
// Prepare 20M keys (100% bulk load)
int key_cnt = default_key_cnt;
uint64_t bulk_byte_size = 0, bulk_ofs = 0;
uint64_t search_byte_size = 0, search_ofs = 0;
// Prepare the bulk load key number
num_of_bulk = key_cnt;
for (int i = 0; i < keys.size(); i++) {
bulk_byte_size += keys[i].length() + 1;
}
// Prepare the bulk load data
bulk_data = new char[bulk_byte_size];
bulk_keys = new char *[num_of_bulk];
bulk_vals = new uint64_t[num_of_bulk];
// Copy the data
for (int i = 0; i < keys.size(); ++i) {
bulk_keys[i] = bulk_data + bulk_ofs;
bulk_vals[i] = i + 1;
memcpy(bulk_data + bulk_ofs, keys[i].c_str(), keys[i].length() + 1);
bulk_ofs += keys[i].length() + 1;
}
assert(bulk_ofs == bulk_byte_size);
// 10M keys for possitive search
std::random_shuffle(keys.begin(), keys.end());
// Prepare the search key number
num_of_search = default_search_cnt;
for (int i = 0; i < num_of_search; i++) {
search_byte_size += keys[i].length() + 1;
}
// Prepare the search data
search_data = new char[search_byte_size];
search_keys = new char *[num_of_search];
// Copy the data
for (int i = 0; i < num_of_search; ++i) {
search_keys[i] = search_data + search_ofs;
memcpy(search_data + search_ofs, keys[i].c_str(), keys[i].length() + 1);
search_ofs += keys[i].length() + 1;
}
assert(search_ofs == search_byte_size);
}
void prepareInsertQuerys() {
std::cout << "[Info]: Preparing insert queries ..." << std::endl;
// Prepare 20M keys (50% bulk load, 50% insert)
int key_cnt = default_key_cnt;
uint64_t bulk_byte_size = 0, bulk_ofs = 0;
uint64_t insert_byte_size = 0, insert_ofs = 0;
// Prepare the bulk load key number
num_of_bulk = key_cnt / 2;
num_of_insert = key_cnt / 2;
// Randomly shuffle the keys
std::random_shuffle(keys.begin(), keys.end());
// Sort the 50% keys for bulk load
std::partial_sort(keys.begin(), keys.begin() + num_of_bulk, keys.end());
for (int i = 0; i < num_of_bulk; i++) {
bulk_byte_size += keys[i].length() + 1;
}
for (int i = num_of_bulk; i < num_of_bulk + num_of_insert; i++) {
insert_byte_size += keys[i].length() + 1;
}
// Prepare the bulk load data
bulk_data = new char[bulk_byte_size];
bulk_keys = new char *[num_of_bulk];
bulk_vals = new uint64_t[num_of_bulk];
// Prepare the insert data
insert_data = new char[insert_byte_size];
insert_keys = new char *[num_of_insert];
// Copy the data
for (int i = 0; i < num_of_bulk; ++i) {
// Copy the bulk load data
bulk_keys[i] = bulk_data + bulk_ofs;
bulk_vals[i] = i + 1;
memcpy(bulk_data + bulk_ofs, keys[i].c_str(), keys[i].length() + 1);
bulk_ofs += keys[i].length() + 1;
}
for (int i = 0; i < num_of_insert; ++i) {
// Copy the insert data
insert_keys[i] = insert_data + insert_ofs;
memcpy(insert_data + insert_ofs, keys[i + num_of_bulk].c_str(),
keys[i + num_of_bulk].length() + 1);
insert_ofs += keys[i + num_of_bulk].length() + 1;
}
}
void prepareScanQuerys() {
std::cout << "[Info]: Preparing scan queries ..." << std::endl;
// Prepare 20M keys (100% bulk load)
int key_cnt = default_key_cnt;
uint64_t bulk_byte_size = 0, bulk_ofs = 0;
uint64_t search_byte_size = 0, search_ofs = 0;
// Prepare the bulk load key number
num_of_bulk = key_cnt;
for (int i = 0; i < keys.size(); i++) {
bulk_byte_size += keys[i].length() + 1;
}
// Prepare the bulk load data
bulk_data = new char[bulk_byte_size];
bulk_keys = new char *[num_of_bulk];
bulk_vals = new uint64_t[num_of_bulk];
// Copy the data
for (int i = 0; i < keys.size(); ++i) {
bulk_keys[i] = bulk_data + bulk_ofs;
bulk_vals[i] = i + 1;
memcpy(bulk_data + bulk_ofs, keys[i].c_str(), keys[i].length() + 1);
bulk_ofs += keys[i].length() + 1;
}
assert(bulk_ofs == bulk_byte_size);
// 1M keys for scan
std::random_shuffle(keys.begin(), keys.end());
// Prepare the search key number
num_of_search = default_scan_cnt;
for (int i = 0; i < num_of_search; i++) {
search_byte_size += keys[i].length() + 1;
}
// Prepare the search data
search_data = new char[search_byte_size];
search_keys = new char *[num_of_search];
// Copy the data
for (int i = 0; i < num_of_search; ++i) {
search_keys[i] = search_data + search_ofs;
memcpy(search_data + search_ofs, keys[i].c_str(), keys[i].length() + 1);
search_ofs += keys[i].length() + 1;
}
assert(search_ofs == search_byte_size);
}
void OutputResult(uint64_t checkSum, int numQuery, double second) {
std::cout << "[Info]: Checksum:\t" << checkSum << std::endl;
std::cout << "[Info]: Query Count:\t" << numQuery << std::endl;
std::cout << "[Info]: Throughput:\t\033[32m" << numQuery / (1e6 * second)
<< " Mops\033[0m" << std::endl;
}
void LITS_Search_test() {
lits::LITS index;
uint64_t checkSum = 0;
struct timeval tv1, tv2;
double second;
std::cout << "[Info]: Index bulk loading ... " << std::endl;
// Bulk load the keys
index.bulkload((const char **)(bulk_keys), (const uint64_t *)(bulk_vals),
num_of_bulk);
std::cout << "[Info]: Index bulk loaded." << std::endl;
gettimeofday(&tv1, NULL);
for (int i = 0; i < num_of_search; ++i) {
checkSum += index.lookup((const char *)(search_keys[i])) ? 1 : 0;
}
gettimeofday(&tv2, NULL);
second = tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec) / 1000000.0;
OutputResult(checkSum, num_of_search, second);
index.destroy();
}
void LITS_Insert_test() {
lits::LITS index;
uint64_t checkSum = 0;
struct timeval tv1, tv2;
double second;
std::cout << "[Info]: Index bulk loading ... " << std::endl;
// Bulk load the keys
index.bulkload((const char **)(bulk_keys), (const uint64_t *)(bulk_vals),
num_of_bulk);
std::cout << "[Info]: Index bulk loaded." << std::endl;
gettimeofday(&tv1, NULL);
for (int i = 0; i < num_of_insert; ++i) {
checkSum +=
index.insert((const char *)(insert_keys[i]), dummy_value) ? 1 : 0;
}
gettimeofday(&tv2, NULL);
second = tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec) / 1000000.0;
OutputResult(checkSum, num_of_insert, second);
index.destroy();
}
void LITS_Scan_test() {
lits::LITS index;
uint64_t checkSum = 0;
struct timeval tv1, tv2;
double second;
std::cout << "[Info]: Index bulk loading ... " << std::endl;
// Bulk load the keys
index.bulkload((const char **)(bulk_keys), (const uint64_t *)(bulk_vals),
num_of_bulk);
std::cout << "[Info]: Index bulk loaded." << std::endl;
gettimeofday(&tv1, NULL);
for (int i = 0; i < num_of_search; ++i) {
int scan_range = rand() % default_scan_range + 1;
auto iter = index.find((const char *)(search_keys[i]));
for (int i = 0; i < scan_range && iter.not_finish(); ++i) {
checkSum += iter.getKV()->v;
iter.next();
}
}
gettimeofday(&tv2, NULL);
second = tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec) / 1000000.0;
OutputResult(checkSum, num_of_search, second);
index.destroy();
}
int main(int argc, char *argv[]) {
srand(time(NULL));
if (argc != 3) {
std::cout << "Usage: " << std::endl;
std::cout << argv[0] << " idcards/randstr 1/2/3" << std::endl;
return 0;
}
int testMode = atoi(argv[2]);
if (testMode < 1 || testMode > 3) {
std::cout << "1: Search-Only Test" << std::endl;
std::cout << "2: Insert-Only Test" << std::endl;
std::cout << "3: Scan-Only Test" << std::endl;
return 0;
}
if (strcmp(argv[1], "idcards") == 0)
generateKeys(idcards);
else if (strcmp(argv[1], "randstr") == 0)
generateKeys(randstr);
else {
std::cout << "Invalid argument" << std::endl;
return 0;
}
// Do Search Test
if (testMode == 1) {
std::cout << std::endl;
std::cout << "\033[33m" << "[Search-Only Test] (100% bulk load, "
<< default_search_cnt << " random search)"
<< "\033[0m" << std::endl;
prepareSearchQuerys();
LITS_Search_test();
}
// Do Insert Test
if (testMode == 2) {
std::cout << std::endl;
std::cout << "\033[33m"
<< "[Insert-Only Test] (50% bulk load, 50% random insert)"
<< "\033[0m" << std::endl;
prepareInsertQuerys();
LITS_Insert_test();
}
// Do Scan Test
if (testMode == 3) {
std::cout << std::endl;
std::cout << "\033[33m" << "[Short Scan Test] (100% bulk load, "
<< default_scan_cnt << " random scan)"
<< "\033[0m" << std::endl;
prepareScanQuerys();
LITS_Scan_test();
}
// Free the data
freeData();
}