-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.c
460 lines (416 loc) · 13.2 KB
/
benchmark.c
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright 2022-2023 CrossDB.ORG. All rights reserved.
*
* https://crossdb.org
* https://github.com/crossdb-org/crossdb
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
typedef struct Random {
uint32_t seed_;
} Random;
void rand_init(Random* rand_, uint32_t s)
{
rand_->seed_ = s & 0x7fffffffu;
// Avoid bad seeds.
if (rand_->seed_ == 0 || rand_->seed_ == 2147483647L) {
rand_->seed_ = 1;
}
}
uint32_t rand_next(Random* rand_)
{
static const uint32_t M = 2147483647L;
static const uint64_t A = 16807;
uint64_t product = rand_->seed_ * A;
rand_->seed_ = (uint32_t)((product >> 31) + (product & M));
if (rand_->seed_ > M) {
rand_->seed_ -= M;
}
return rand_->seed_;
}
static uint64_t timestamp_us ()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000LL + tv.tv_usec;
}
static inline uint32_t get_next_rand (Random *pRand, uint8_t *pMark, uint32_t max)
{
uint32_t id;
while (pMark[(id = (rand_next(pRand) % max))])
;
pMark[id] = 1;
return id;
}
static int int_cmp (const void *pL, const void *pR)
{
int left = *(int*)pL, right = *(int*)pR;
return (left == right) ? 0 : ((left > right) ? -1 : 1);
}
#ifndef DFT_ROW_COUNT
#define DFT_ROW_COUNT 1000000
#endif
bool s_quiet = false;
int row_count = 0;
int static inline count_convert(const char *arg)
{
int count = atoi (optarg);
switch (toupper(arg[strlen(arg)-1])) {
case 'K': return count * 1000;
case 'M': return count * 1000000;
}
return count;
}
int main (int argc, char **argv)
{
int i;
char ch;
uint64_t ts;
route_t route;
dbtype_e type = DB_RAMDISK;
keytype_e key = KEY_DEFAULT;
int load_count = 0;
int insert_count = DFT_ROW_COUNT;
int query_count = DFT_ROW_COUNT;
int update_count = DFT_ROW_COUNT;
int delete_count = DFT_ROW_COUNT;
int cpuid = -1;
bool sequential = false, verify = false, header = false;
uint32_t insert_tps = 0, load_tps = 0, query_qps = 0, update_tps = 0, delete_tps = 0;
while ((ch = getopt(argc, argv, "s:k:l:i:q:u:d:r:c:SVQHh")) != -1) {
switch (ch) {
case 'h':
printf ("Usage: {count unit can be k or K (*1000) m or M (*1000000)}\n");
printf (" -h show this help\n");
printf (" -s <db storage> ramdisk | inmem | ondisk, default is ramdisk\n");
printf (" shortcut r: ramdisk m: inmem d: ondisk\n");
printf (" ondisk store db in current folder crossbench\n");
printf (" -i <insert count> default %d, do insert test, conflict with -l load\n", insert_count);
printf (" -r <row count> open db which already has these rows\n");
printf (" -q <query count> default %d, do query test\n", query_count);
printf (" -u <update count> default %d, do update test\n", update_count);
printf (" -d <delete count> default %d, do delete test\n", delete_count);
printf (" -c <cpu core> bind cpu core\n");
printf (" -l <load count> quick load data, conflict with -i insert\n");
printf (" -k <key type> default | hash | tree\n");
printf (" shortcut h: hash t: tree d: default\n");
printf (" -S sequential, defaut is random\n");
printf (" -V verify driver basic CRUD operations\n");
printf (" -Q quiet mode, only show last result\n");
printf (" -H show header, if -Q is set, then don't show header\n");
printf ("\nExample:\n");
printf (" default: do ramdisk random mode test, insert 1M rows, query 1M times, update 1M times, delete 1M rows\n");
printf (" -s m: do inmem random mode test, insert 1M rows, query 1M times, update 1M times, delete 1M rows\n");
printf (" -s d: do ondisk random mode test, insert 1M rows, query 1M times, update 1M times, delete 1M rows\n");
printf (" -i 500k -q 5m: do ramdisk random mode test, insert 500K rows, query 5M times, updat 1M times, delete 500k rows\n");
printf (" -i 10k -q 0 -u 0 -d 0: do ramdisk random mode test, insert 10K rows\n");
printf (" -r 10k -q 100k -u 0 -d 0: do ramdisk random mode test, query 100K times\n");
printf (" -r 10k -q 0 -u 100k -d 0: do ramdisk random mode test, update 100K times\n");
printf (" -r 10k -q 0 -u 0 -d 10k: do ramdisk random mode test, delete 10K rows\n");
printf (" -V: verify db driver is ok\n");
return -1;
case 's':
if (('m' == optarg[0]) || !strcmp (optarg, "inmem")) {
type = DB_INMEM;
} else if (('d' == optarg[0]) || !strcmp (optarg, "ondisk")) {
type = DB_ONDISK;
}
break;
case 'k':
if (('h' == optarg[0]) || !strcmp (optarg, "hash")) {
key = KEY_HASH;
} else if (('t' == optarg[0]) || !strcmp (optarg, "tree")) {
key = KEY_TREE;
}
break;
case 'r':
row_count = count_convert (optarg);
load_count = 0;
insert_count = 0;
delete_count = 0;
break;
case 'l':
load_count = count_convert (optarg);
row_count = 0;
insert_count = 0;
delete_count = 0;
break;
case 'i':
insert_count = count_convert (optarg);
delete_count = insert_count;
load_count = 0;
row_count = 0;
break;
case 'q':
query_count = count_convert (optarg);
break;
case 'u':
update_count = count_convert (optarg);
break;
case 'd':
delete_count = count_convert (optarg);
break;
case 'c':
cpuid = atoi (optarg);
break;
case 'S':
sequential = true;
break;
case 'V':
verify = true;
case 'Q':
s_quiet = true;
break;
case 'H':
header = true;
break;
}
}
if (load_count > 0 && insert_count > 0) {
printf ("Both load data and insert test are set, only one can be set\n");
return -1;
}
// dbname
char name[256], cmd[512];
if (DB_ONDISK == type) {
sprintf (name, "_benchmarkdb/%s", db_name);
if (0 == row_count) {
sprintf (cmd, "rm -rf %s", name);
int rc = system (cmd);
(void)rc;
}
sprintf (cmd, "mkdir -p %s", name);
int rc = system (cmd);
(void)rc;
} else {
sprintf (name, "/tmp/_benchmarkdb/%s", db_name);
if (0 == row_count) {
sprintf (cmd, "rm -rf %s", name);
int rc = system (cmd);
(void)rc;
}
sprintf (cmd, "mkdir -p %s", name);
int rc = system (cmd);
(void)rc;
}
if (0 == row_count) {
row_count = load_count + insert_count;
}
if (0 == row_count) {
if (header) {
printf ("%16s\t%8s\t%8s\t%8s\t%8s\t%8s\n", "DB", "Rows", "Insert", "Query", "Update", "Delete");
return 0;
}
printf ("Both load data and insert test are not set, only one can be set\n");
return -1;
}
if (delete_count > row_count) {
delete_count = row_count;
}
srand (time(NULL));
// rand init
uint8_t *pRandMark = (uint8_t*)malloc (row_count);
if (NULL == pRandMark) {
return -1;
}
Random rand;
rand_init (&rand, time(NULL));
const char *type_str[] = { "ONDISK", "INMEM", "RAMDISK", };
if (!s_quiet) {
printf ("%s Database Benchmark\n\n", db_name);
printf ("Location: %s\nType: %s\nKey: %s\nAccess: %s\n", name,
type_str[type], keyTypeStr(key), sequential?"Sequential":"Random");
}
const char *dbname = db_create (name, type, key, 0);
if (NULL == dbname) {
dbname = db_name;
}
// init route
uint32_t prefix = IP4ADDR(10,1,1,0);
route.prefix = prefix;
route.mask = 24;
route.nexthop = IP4ADDR(10,1,1,254);
route.metric = 1;
route.flags = 0;
strcpy (route.intf, "eth1");
route.birth = time (NULL);
if (verify) {
printf ("------ Verify %s ------\n", dbname);
printf ("Insert route\n");
db_insert (&route);
printf ("Query route\n");
memset (&route, 0, sizeof(route));
route.prefix = prefix;
route.mask = 24;
if (!db_query (&route)) {
printf ("Didn't get route\n");
return -1;
}
DUMP_ROUTE (route);
printf ("Update route\n");
route.birth += 10;
time_t birth = route.birth;
db_update (&route);
printf ("Query route\n");
memset (&route, 0, sizeof(route));
route.prefix = prefix;
route.mask = 24;
if (!db_query (&route)) {
printf ("Didn't get route\n");
return -1;
}
DUMP_ROUTE (route);
if (route.birth != birth) {
printf ("db.birth %u != %u\n", (uint32_t)route.birth, (uint32_t)birth);
return -1;
}
printf ("Delete route\n");
db_delete (&route);
printf ("Query route\n");
if (db_query (&route)) {
printf ("Route was not deleted\n");
DUMP_ROUTE (route);
}
printf ("------ Verify Done ------\n");
db_close ();
return 0;
}
if (cpuid >= 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpuid, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
perror ("sched_setaffinity");
return -1;
}
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
perror("pthread_setaffinity_np");
}
if (!s_quiet) {
printf ("\nRun test on CPU %d\n", cpuid);
}
}
if (load_count > 0) {
ts = timestamp_us();
db_begin ();
for (i = 0; i < row_count; ++i) {
db_insert (&route);
route.prefix++;
}
db_commit ();
ts = timestamp_us() - ts;
load_tps = row_count*1000000LL/ts;
if (!s_quiet) {
printf ("\nLoad rows: %d Use time: %uus TPS: %d\n\n", row_count, (uint32_t)ts, load_tps);
}
}
if (insert_count > 0) {
memset (pRandMark, 0, row_count);
ts = timestamp_us();
for (i = 0; i < row_count; ++i) {
if (!sequential) {
route.prefix = prefix + get_next_rand(&rand, pRandMark, row_count);
} else {
route.prefix = prefix + i;
}
db_insert (&route);
}
ts = timestamp_us() - ts;
insert_tps = row_count*1000000LL/ts;
if (!s_quiet) {
printf ("\nInsert rows: %d Use time: %uus TPS: %d\n\n", row_count, (uint32_t)ts, insert_tps);
}
}
if (query_count > 0) {
int qps[10];
if (!s_quiet) {
printf ("Query Test: %d\n", query_count);
}
for (int r = 0; r < 10; ++r) {
ts = timestamp_us();
route.mask = 24;
for (i = 0; i < query_count; ++i) {
if (!sequential) {
route.prefix = prefix + (rand_next(&rand) % row_count);
} else {
route.prefix = prefix + (i % row_count);
}
db_query (&route);
}
ts = timestamp_us() - ts;
qps[r] = (int)((int64_t)i*1000000/ts);
if (!s_quiet) {
printf (" Round: %d\tUse Time: %uus\tQPS: %d\n", r+1, (uint32_t)ts, qps[r]);
}
}
qsort (qps, 10, sizeof(qps[0]), int_cmp);
query_qps = (qps[0] + qps[1] + qps[2]) / 3;
}
if (update_count > 0) {
int tps[10];
if (!s_quiet) {
printf ("Update Test: %d\n", update_count);
}
uint32_t id = time(NULL);
for (int r = 0; r < 10; ++r) {
memset (pRandMark, 0, row_count);
ts = timestamp_us();
route.mask = 24;
for (i = 0; i < update_count; ++i) {
route.birth = id++;
if (!sequential) {
route.prefix = prefix + (rand_next(&rand) % row_count);
} else {
route.prefix = prefix + (i % row_count);
}
db_update (&route);
}
ts = timestamp_us() - ts;
tps[r] = update_count*1000000LL/ts;
if (!s_quiet) {
printf (" Round: %d\tUse Time: %uus\tTPS: %d\n", r+1, (uint32_t)ts, tps[r]);
}
}
qsort (tps, 10, sizeof(tps[0]), int_cmp);
update_tps = (tps[0] + tps[1] + tps[2] + tps[3] + tps[4]) / 5;
}
if (delete_count > 0) {
memset (pRandMark, 0, row_count);
ts = timestamp_us();
route.mask = 24;
for (i = 0; i < delete_count; ++i) {
if (!sequential) {
route.prefix = prefix + get_next_rand(&rand, pRandMark, row_count);
} else {
route.prefix = prefix + (i % row_count);
}
db_delete (&route);
}
ts = timestamp_us() - ts;
delete_tps = delete_count*1000000LL/ts;
if (!s_quiet) {
printf ("\nDelete: %d\tUse Time: %uus\tTPS: %d\n", i, (uint32_t)ts, delete_tps);
}
}
if (!s_quiet) {
printf ("\n");
printf ("%s Benchmark Result: %s Access=%s Key=%s Rows=%d\n",
db_name, type_str[type], sequential?"Sequential":"Random", keyTypeStr(key), row_count);
}
if (!s_quiet || !header) {
printf ("%16s\t%8s\t%8s\t%8s\t%8s\t%8s\n", "DB", "Rows", insert_tps?"Insert":"Load", "Query", "Update", "Delete");
}
printf ("%16s\t%8d\t%8d\t%8d\t%8d\t%8d\n", dbname, row_count, insert_tps+load_tps, query_qps, update_tps, delete_tps);
db_close ();
return 0;
}