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
14 #include "dictionary.h"
23 void netdata_cleanup_and_exit(int ret) { exit(ret); }
25 int main(int argc, char **argv) {
28 // DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS);
29 DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
30 if(!dict) fatal("Cannot create dictionary.");
32 struct rusage start, end;
33 unsigned long long dt;
35 struct myvalue value, *v;
36 int i, max = 30000000, max2;
38 // ------------------------------------------------------------------------
40 getrusage(RUSAGE_SELF, &start);
41 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
42 fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
43 for(i = 0; i < max; i++) {
45 snprintf(buf, 100, "%d", i);
47 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
49 getrusage(RUSAGE_SELF, &end);
50 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
51 fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
52 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
54 // ------------------------------------------------------------------------
56 getrusage(RUSAGE_SELF, &start);
57 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
58 fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
59 for(i = 0; i < max; i++) {
61 snprintf(buf, 100, "%d", i);
63 v = dictionary_get(dict, buf);
65 fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
67 fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
69 getrusage(RUSAGE_SELF, &end);
70 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
71 fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
72 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
74 // ------------------------------------------------------------------------
76 getrusage(RUSAGE_SELF, &start);
77 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
78 fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
79 for(i = 0; i < max; i++) {
81 snprintf(buf, 100, "%d", i);
83 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
85 getrusage(RUSAGE_SELF, &end);
86 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
87 fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt);
88 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
90 // ------------------------------------------------------------------------
92 getrusage(RUSAGE_SELF, &start);
93 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
94 fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max);
96 for(i = max; i < max2; i++) {
98 snprintf(buf, 100, "%d", i);
100 v = dictionary_get(dict, buf);
102 fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
104 getrusage(RUSAGE_SELF, &end);
105 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
106 fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt);
107 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
109 // ------------------------------------------------------------------------
111 getrusage(RUSAGE_SELF, &start);
112 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
113 fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
114 for(i = 0; i < max; i++) {
116 snprintf(buf, 100, "%d", i);
118 dictionary_del(dict, buf);
120 getrusage(RUSAGE_SELF, &end);
121 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
122 fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
123 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
125 // ------------------------------------------------------------------------
127 getrusage(RUSAGE_SELF, &start);
128 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
129 fprintf(stderr, "Destroying dictionary\n");
130 dictionary_destroy(dict);
131 getrusage(RUSAGE_SELF, &end);
132 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
133 fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);