]> arthur.barton.de Git - netdata.git/blob - profile/benchmark-dictionary.c
Merge remote-tracking branch 'upstream/master' into registry
[netdata.git] / profile / benchmark-dictionary.c
1
2 /*
3  * 1. build netdata (as normally)
4  * 2. cd profile/
5  * 3. compile with:
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
7  *
8  */
9
10 #include <stdio.h>
11 #include <inttypes.h>
12
13 #include "dictionary.h"
14 #include "main.h"
15 #include "log.h"
16 #include "common.h"
17
18 struct myvalue {
19         int i;
20 };
21
22 int main(int argc, char **argv) {
23         if(argc || argv) {;}
24
25         DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_NAME_CORRUPTION_CHECK);
26         if(!dict) fatal("Cannot create dictionary.");
27
28         struct rusage start, end;
29         unsigned long long dt;
30         char buf[100 + 1];
31         struct myvalue value, *v;
32         int i, max = 100000, max2;
33
34         // ------------------------------------------------------------------------
35
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++) {
40                 value.i = i;
41                 snprintf(buf, 100, "%d", i);
42
43                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
44         }
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);
49
50         // ------------------------------------------------------------------------
51
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++) {
56                 value.i = i;
57                 snprintf(buf, 100, "%d", i);
58
59                 v = dictionary_get(dict, buf);
60                 if(!v)
61                         fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
62                 else if(v->i != i)
63                         fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
64         }
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);
69
70         // ------------------------------------------------------------------------
71
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++) {
76                 value.i = i;
77                 snprintf(buf, 100, "%d", i);
78
79                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
80         }
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);
85
86         // ------------------------------------------------------------------------
87
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);
91         max2 = max * 2;
92         for(i = max; i < max2; i++) {
93                 value.i = i;
94                 snprintf(buf, 100, "%d", i);
95
96                 v = dictionary_get(dict, buf);
97                 if(v)
98                         fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
99         }
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);
104
105         // ------------------------------------------------------------------------
106
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++) {
111                 value.i = i;
112                 snprintf(buf, 100, "%d", i);
113
114                 dictionary_del(dict, buf);
115         }
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);
120
121         // ------------------------------------------------------------------------
122
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);
130
131         return 0;
132 }