]> arthur.barton.de Git - netdata.git/blob - profile/benchmark-dictionary.c
846e3c61ac7427c333aabb32f061b5db273dd176
[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 <stdlib.h>
12 #include <inttypes.h>
13
14 #include "dictionary.h"
15 #include "main.h"
16 #include "log.h"
17 #include "common.h"
18
19 struct myvalue {
20         int i;
21 };
22
23 void netdata_cleanup_and_exit(int ret) { exit(ret); }
24
25 int main(int argc, char **argv) {
26         if(argc || argv) {;}
27
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.");
31
32         struct rusage start, end;
33         unsigned long long dt;
34         char buf[100 + 1];
35         struct myvalue value, *v;
36         int i, max = 30000000, max2;
37
38         // ------------------------------------------------------------------------
39
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++) {
44                 value.i = i;
45                 snprintf(buf, 100, "%d", i);
46
47                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
48         }
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);
53
54         // ------------------------------------------------------------------------
55
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++) {
60                 value.i = i;
61                 snprintf(buf, 100, "%d", i);
62
63                 v = dictionary_get(dict, buf);
64                 if(!v)
65                         fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
66                 else if(v->i != i)
67                         fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
68         }
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);
73
74         // ------------------------------------------------------------------------
75
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++) {
80                 value.i = i;
81                 snprintf(buf, 100, "%d", i);
82
83                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
84         }
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);
89
90         // ------------------------------------------------------------------------
91
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);
95         max2 = max * 2;
96         for(i = max; i < max2; i++) {
97                 value.i = i;
98                 snprintf(buf, 100, "%d", i);
99
100                 v = dictionary_get(dict, buf);
101                 if(v)
102                         fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
103         }
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);
108
109         // ------------------------------------------------------------------------
110
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++) {
115                 value.i = i;
116                 snprintf(buf, 100, "%d", i);
117
118                 dictionary_del(dict, buf);
119         }
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);
124
125         // ------------------------------------------------------------------------
126
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);
134
135         return 0;
136 }