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
16 void netdata_cleanup_and_exit(int ret) { exit(ret); }
18 int main(int argc, char **argv) {
21 // DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS);
22 DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
23 if(!dict) fatal("Cannot create dictionary.");
25 struct rusage start, end;
26 unsigned long long dt;
28 struct myvalue value, *v;
29 int i, max = 30000000, max2;
31 // ------------------------------------------------------------------------
33 getrusage(RUSAGE_SELF, &start);
34 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
35 fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
36 for(i = 0; i < max; i++) {
38 snprintf(buf, 100, "%d", i);
40 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
42 getrusage(RUSAGE_SELF, &end);
43 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
44 fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
45 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
47 // ------------------------------------------------------------------------
49 getrusage(RUSAGE_SELF, &start);
50 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
51 fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
52 for(i = 0; i < max; i++) {
54 snprintf(buf, 100, "%d", i);
56 v = dictionary_get(dict, buf);
58 fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
60 fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
62 getrusage(RUSAGE_SELF, &end);
63 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
64 fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
65 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
67 // ------------------------------------------------------------------------
69 getrusage(RUSAGE_SELF, &start);
70 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
71 fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
72 for(i = 0; i < max; i++) {
74 snprintf(buf, 100, "%d", i);
76 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
78 getrusage(RUSAGE_SELF, &end);
79 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
80 fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt);
81 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
83 // ------------------------------------------------------------------------
85 getrusage(RUSAGE_SELF, &start);
86 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
87 fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max);
89 for(i = max; i < max2; i++) {
91 snprintf(buf, 100, "%d", i);
93 v = dictionary_get(dict, buf);
95 fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
97 getrusage(RUSAGE_SELF, &end);
98 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
99 fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt);
100 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
102 // ------------------------------------------------------------------------
104 getrusage(RUSAGE_SELF, &start);
105 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
106 fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
107 for(i = 0; i < max; i++) {
109 snprintf(buf, 100, "%d", i);
111 dictionary_del(dict, buf);
113 getrusage(RUSAGE_SELF, &end);
114 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
115 fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
116 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
118 // ------------------------------------------------------------------------
120 getrusage(RUSAGE_SELF, &start);
121 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
122 fprintf(stderr, "Destroying dictionary\n");
123 dictionary_destroy(dict);
124 getrusage(RUSAGE_SELF, &end);
125 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
126 fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);