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_WITH_STATISTICS);
26 DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
27 if(!dict) fatal("Cannot create dictionary.");
29 struct rusage start, end;
30 unsigned long long dt;
32 struct myvalue value, *v;
33 int i, max = 100000, max2;
35 // ------------------------------------------------------------------------
37 getrusage(RUSAGE_SELF, &start);
38 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
39 fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
40 for(i = 0; i < max; i++) {
42 snprintf(buf, 100, "%d", i);
44 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
46 getrusage(RUSAGE_SELF, &end);
47 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
48 fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
49 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
51 // ------------------------------------------------------------------------
53 getrusage(RUSAGE_SELF, &start);
54 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
55 fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
56 for(i = 0; i < max; i++) {
58 snprintf(buf, 100, "%d", i);
60 v = dictionary_get(dict, buf);
62 fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
64 fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
66 getrusage(RUSAGE_SELF, &end);
67 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
68 fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
69 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
71 // ------------------------------------------------------------------------
73 getrusage(RUSAGE_SELF, &start);
74 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
75 fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
76 for(i = 0; i < max; i++) {
78 snprintf(buf, 100, "%d", i);
80 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
82 getrusage(RUSAGE_SELF, &end);
83 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
84 fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt);
85 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
87 // ------------------------------------------------------------------------
89 getrusage(RUSAGE_SELF, &start);
90 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
91 fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max);
93 for(i = max; i < max2; i++) {
95 snprintf(buf, 100, "%d", i);
97 v = dictionary_get(dict, buf);
99 fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
101 getrusage(RUSAGE_SELF, &end);
102 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
103 fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt);
104 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
106 // ------------------------------------------------------------------------
108 getrusage(RUSAGE_SELF, &start);
109 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
110 fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
111 for(i = 0; i < max; i++) {
113 snprintf(buf, 100, "%d", i);
115 dictionary_del(dict, buf);
117 getrusage(RUSAGE_SELF, &end);
118 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
119 fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
120 fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
122 // ------------------------------------------------------------------------
124 getrusage(RUSAGE_SELF, &start);
125 dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
126 fprintf(stderr, "Destroying dictionary\n");
127 dictionary_destroy(dict);
128 getrusage(RUSAGE_SELF, &end);
129 dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
130 fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);