-
Notifications
You must be signed in to change notification settings - Fork 73
/
benchmark.c
456 lines (385 loc) · 12.1 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
/* benchmark.c
* Some timing tests for libdict
* Copyright (C) 2001-2011 Farooq Mela */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "dict.h"
#include "dict_private.h"
#include "tree_common.h"
#include "util.h"
static const char appname[] = "benchmark";
char *xstrdup(const char *str);
#ifdef __GNUC__
# define NORETURN __attribute__((__noreturn__))
#else
# define NORETURN
#endif
void quit(const char *, ...) NORETURN;
void warn(const char *fmt, ...);
void *xmalloc(size_t size);
static size_t hash_count = 0, comp_count = 0;
unsigned str_hash(const void *p);
int my_strcmp(const void *k1, const void *k2);
unsigned ptr_hash(const void *p);
int my_ptrcmp(const void *k1, const void *k2);
void key_str_free(void *key, void *datum);
void timer_start(struct rusage* start);
void timer_end(const struct rusage* start, struct rusage *end,
struct timeval *total);
dict *create_dictionary(char type, const char **container_name);
#define HASHTABLE_SIZE 97
/* #define HASHTABLE_SIZE 997 */
/* #define HASHTABLE_SIZE 9973 */
static size_t malloced = 0;
int
main(int argc, char **argv)
{
bool shuffle_keys = true;
if (argc != 3) {
fprintf(stderr, "usage: %s [type] [input]\n", appname);
fprintf(stderr, "type: specifies the dictionary type:\n");
fprintf(stderr, " h: height-balanced tree\n");
fprintf(stderr, " p: path-reduction tree\n");
fprintf(stderr, " r: red-black tree\n");
fprintf(stderr, " t: treap\n");
fprintf(stderr, " s: splay tree\n");
fprintf(stderr, " w: weight-balanced tree\n");
fprintf(stderr, " S: skiplist\n");
fprintf(stderr, " H: hashtable\n");
fprintf(stderr, " 2: hashtable 2\n");
fprintf(stderr, "input: text file consisting of newline-separated keys\n");
exit(EXIT_FAILURE);
}
srand(0xdeadbeef);
dict_malloc_func = xmalloc;
const char type = argv[1][0];
const char *container_name = NULL;
dict *dct = create_dictionary(type, &container_name);
if (!dct)
quit("can't create container");
ASSERT(dict_verify(dct));
ASSERT(comp_count == 0);
ASSERT(hash_count == 0);
const size_t malloced_save = malloced;
FILE *fp = fopen(argv[2], "r");
if (fp == NULL)
quit("cant open file '%s': %s", argv[2], strerror(errno));
size_t nwords = 0;
char buf[512];
while (fgets(buf, sizeof(buf), fp))
++nwords;
if (!nwords)
quit("nothing read from file");
char **words = xmalloc(sizeof(*words) * nwords);
rewind(fp);
size_t words_read = 0;
while (words_read < nwords && fgets(buf, sizeof(buf), fp)) {
strtok(buf, "\n");
words[words_read++] = xstrdup(buf);
}
fclose(fp);
if (words_read < nwords)
quit("Only read %zu/%zu words!", words_read, nwords);
printf("Loaded %zu keys from %s.\n", nwords, argv[2]);
malloced = malloced_save;
size_t total_comp = 0, total_hash = 0, total_rotations = 0;
struct rusage start, end;
struct timeval total = { 0, 0 };
timer_start(&start);
for (unsigned i = 0; i < nwords; i++) {
dict_insert_result result = dict_insert(dct, words[i]);
if (!result.inserted)
quit("insert #%d failed for '%s'", i, words[i]);
ASSERT(result.datum_ptr != NULL);
ASSERT(*result.datum_ptr == NULL);
*result.datum_ptr = words[i];
}
timer_end(&start, &end, &total);
printf(" %s container: %.02fkB\n", container_name, malloced_save * 1e-3);
printf(" %s memory: %.02fkB\n", container_name, malloced * 1e-3);
printf(" %s insert: %6.03fs %9zu cmp (%.02f/insert)",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
comp_count, comp_count / (double) nwords);
if (hash_count)
printf(" %9zu hash", hash_count);
printf("\n");
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (dict_is_sorted(dct) && type != 'S') {
tree_base *tree = dict_private(dct);
printf(" min path length: %zu\n", tree_min_path_length(tree));
printf(" max path length: %zu\n", tree_max_path_length(tree));
printf(" tot path length: %zu\n", tree_total_path_length(tree));
printf("insert rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
tree->rotation_count = 0;
} else if (type == 'S') {
size_t counts[16] = { 0 };
size_t num_counts = skiplist_link_count_histogram(dict_private(dct), counts, sizeof(counts) / sizeof(counts[0]));
size_t count_sum = 0;
for (size_t i = 0; i <= num_counts; ++i) {
printf("skiplist %zu-node(s): %zu\n", i, counts[i]);
count_sum += counts[i];
}
ASSERT(count_sum == nwords);
}
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
size_t n = dict_count(dct);
if (n != nwords)
quit("bad count (%u - should be %u)!", n, nwords);
dict_itor *itor = dict_itor_new(dct);
timer_start(&start);
n = 0;
ASSERT(dict_itor_first(itor));
do {
ASSERT(dict_itor_valid(itor));
ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor));
++n;
} while (dict_itor_next(itor));
timer_end(&start, &end, &total);
printf(" %s fwd iterate: %6.03fs\n",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
if (n != nwords)
warn("Fwd iteration returned %u items - should be %u", n, nwords);
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
timer_start(&start);
n = 0;
ASSERT(dict_itor_last(itor));
do {
ASSERT(dict_itor_valid(itor));
ASSERT(dict_itor_key(itor) == *dict_itor_datum(itor));
++n;
} while (dict_itor_prev(itor));
timer_end(&start, &end, &total);
printf(" %s rev iterate: %6.03fs\n",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6);
if (n != nwords)
warn("Rev iteration returned %u items - should be %u", n, nwords);
dict_itor_free(itor);
if (shuffle_keys) shuffle(words, nwords);
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
timer_start(&start);
for (unsigned i = 0; i < nwords; i++) {
void **p = dict_search(dct, words[i]);
if (!p)
quit("lookup failed for '%s'", buf);
if (*p != words[i])
quit("bad data for '%s', got '%s' instead", words[i], *(char **)p);
}
timer_end(&start, &end, &total);
printf(" %s good search: %6.03fs %9zu cmp (%.02f/search)",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
comp_count, comp_count / (double) nwords);
if (hash_count)
printf(" %9zu hash", hash_count);
printf("\n");
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (type != 'H' && type != '2' && type != 'S') {
tree_base *tree = dict_private(dct);
printf("search rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
tree->rotation_count = 0;
}
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
timer_start(&start);
for (unsigned i = 0; i < nwords; i++) {
unsigned rv = dict_rand() % strlen(words[i]);
words[i][rv]++;
dict_search(dct, words[i]);
words[i][rv]--;
}
timer_end(&start, &end, &total);
printf(" %s bad search: %6.03fs %9zu cmp (%.02f/search)",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
comp_count, comp_count / (double) nwords);
if (hash_count)
printf(" %9zu hash", hash_count);
printf("\n");
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
if (shuffle_keys) shuffle(words, nwords);
timer_start(&start);
for (unsigned i = 0; i < nwords; i++) {
dict_remove_result result = dict_remove(dct, words[i]);
if (!result.removed)
quit("removing #%d '%s' failed!\n", i, words[i]);
ASSERT(result.key == words[i]);
ASSERT(result.datum == words[i]);
}
timer_end(&start, &end, &total);
printf(" %s remove: %6.03fs %9zu cmp (%.2f/remove)",
container_name,
(end.ru_utime.tv_sec * 1000000 + end.ru_utime.tv_usec) * 1e-6,
comp_count, comp_count / (double)nwords);
if (hash_count)
printf(" %9zu hash", hash_count);
printf("\n");
total_comp += comp_count; comp_count = 0;
total_hash += hash_count; hash_count = 0;
if (type != 'H' && type != '2' && type != 'S') {
tree_base *tree = dict_private(dct);
printf("remove rotations: %zu\n", tree->rotation_count);
total_rotations += tree->rotation_count;
tree->rotation_count = 0;
}
ASSERT(dict_verify(dct));
comp_count = hash_count = 0; /* Ignore comparisons/hashes incurred by dict_verify() */
if ((n = dict_count(dct)) != 0)
quit("error - count not zero (%u)!", n);
dict_free(dct, key_str_free);
printf(" %s total: %6.03fs %9zu cmp",
container_name,
(total.tv_sec * 1000000 + total.tv_usec) * 1e-6,
total_comp);
if (total_hash)
printf(" %9zu hash", total_hash);
printf("\n");
if (type != 'H' && type != '2' && type != 'S') {
printf(" total rotations: %zu\n", total_rotations);
}
FREE(words);
exit(EXIT_SUCCESS);
}
dict *
create_dictionary(char type, const char **container_name)
{
dict_compare_func cmp_func = my_strcmp;
dict_hash_func hash_func = str_hash;
switch (type) {
case 'h':
*container_name = "hb";
return hb_dict_new(cmp_func);
case 'p':
*container_name = "pr";
return pr_dict_new(cmp_func);
case 'r':
*container_name = "rb";
return rb_dict_new(cmp_func);
case 't':
*container_name = "tr";
return tr_dict_new(cmp_func, NULL);
case 's':
*container_name = "sp";
return sp_dict_new(cmp_func);
case 'S':
*container_name = "sk";
return skiplist_dict_new(cmp_func, 12);
case 'w':
*container_name = "wb";
return wb_dict_new(cmp_func);
case 'H':
*container_name = "ht";
return hashtable_dict_new(cmp_func, hash_func, HASHTABLE_SIZE);
case '2':
*container_name = "h2";
return hashtable2_dict_new(cmp_func, hash_func, HASHTABLE_SIZE);
default:
quit("type must be one of h, p, r, t, s, w or H");
}
}
char *
xstrdup(const char *str)
{
size_t len = strlen(str) + 1;
return memcpy(xmalloc(len), str, len);
}
void
quit(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "%s: ", appname);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(EXIT_FAILURE);
}
void
warn(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "warning: %s: ", appname);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (!p)
quit("out of memory");
malloced += size;
return p;
}
unsigned
str_hash(const void *p)
{
++hash_count;
return dict_str_hash(p);
}
int
my_strcmp(const void *k1, const void *k2)
{
++comp_count;
return strcmp(k1, k2);
}
unsigned
ptr_hash(const void *p)
{
++hash_count;
return (unsigned) ((2166136261U ^ (uintptr_t)p) * 16777619U);
}
int
my_ptrcmp(const void *k1, const void *k2)
{
++comp_count;
return (k1 < k2) ? -1 : (k1 > k2);
}
void
key_str_free(void *key, void *datum)
{
ASSERT(key == datum);
free(key);
}
void
timer_start(struct rusage *start)
{
getrusage(RUSAGE_SELF, start);
}
void
timer_end(const struct rusage *start, struct rusage *end,
struct timeval *total) {
getrusage(RUSAGE_SELF, end);
if (end->ru_utime.tv_usec < start->ru_utime.tv_usec) {
end->ru_utime.tv_usec += 1000000; end->ru_utime.tv_sec--;
}
end->ru_utime.tv_usec -= start->ru_utime.tv_usec;
end->ru_utime.tv_sec -= start->ru_utime.tv_sec;
total->tv_sec += end->ru_utime.tv_sec;
if ((total->tv_usec += end->ru_utime.tv_usec) > 1000000) {
total->tv_usec -= 1000000; total->tv_sec++;
}
}