3 * 1. build netdata (as normally)
6 * gcc -O3 -Wall -Wextra -I ../src/ -I ../ -o benchmark-dictionary benchmark-dictionary.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o -pthread
13 #include "dictionary.h"
22 int main(int argc, char **argv) {
25 DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_NAME_CORRUPTION_CHECK);
26 if(!dict) fatal("Cannot create dictionary.");
28 struct rusage start, end;
29 unsigned long long dt;
31 struct myvalue value, *v;
32 int i, max = 100000, max2;
34 // ------------------------------------------------------------------------
36 getrusage(RUSAGE_SELF, &start);
37 dict->inserts = dict->deletes = dict->searches = 0ULL;
38 fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
39 for(i = 0; i < max; i++) {
41 snprintf(buf, 100, "%d", i);
43 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
45 getrusage(RUSAGE_SELF, &end);
46 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
47 fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
48 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->inserts, dict->deletes, dict->searches);
50 // ------------------------------------------------------------------------
52 getrusage(RUSAGE_SELF, &start);
53 dict->inserts = dict->deletes = dict->searches = 0ULL;
54 fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
55 for(i = 0; i < max; i++) {
57 snprintf(buf, 100, "%d", i);
59 v = dictionary_get(dict, buf);
61 fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
63 fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
65 getrusage(RUSAGE_SELF, &end);
66 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
67 fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
68 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->inserts, dict->deletes, dict->searches);
70 // ------------------------------------------------------------------------
72 getrusage(RUSAGE_SELF, &start);
73 dict->inserts = dict->deletes = dict->searches = 0ULL;
74 fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
75 for(i = 0; i < max; i++) {
77 snprintf(buf, 100, "%d", i);
79 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
81 getrusage(RUSAGE_SELF, &end);
82 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
83 fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
84 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->inserts, dict->deletes, dict->searches);
86 // ------------------------------------------------------------------------
88 getrusage(RUSAGE_SELF, &start);
89 dict->inserts = dict->deletes = dict->searches = 0ULL;
90 fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max);
92 for(i = max; i < max2; i++) {
94 snprintf(buf, 100, "%d", i);
96 v = dictionary_get(dict, buf);
98 fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
100 getrusage(RUSAGE_SELF, &end);
101 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
102 fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
103 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->inserts, dict->deletes, dict->searches);
105 // ------------------------------------------------------------------------
107 getrusage(RUSAGE_SELF, &start);
108 dict->inserts = dict->deletes = dict->searches = 0ULL;
109 fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
110 for(i = 0; i < max; i++) {
112 snprintf(buf, 100, "%d", i);
114 dictionary_del(dict, buf);
116 getrusage(RUSAGE_SELF, &end);
117 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
118 fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
119 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->inserts, dict->deletes, dict->searches);
121 // ------------------------------------------------------------------------
123 getrusage(RUSAGE_SELF, &start);
124 dict->inserts = dict->deletes = dict->searches = 0ULL;
125 fprintf(stderr, "Destroying dictionary\n");
126 dictionary_destroy(dict);
127 getrusage(RUSAGE_SELF, &end);
128 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
129 fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);