]> arthur.barton.de Git - netdata.git/blob - profile/benchmark-dictionary.c
Merge pull request #1968 from ktsaou/master
[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 "common.h"
11
12 struct myvalue {
13         int i;
14 };
15
16 void netdata_cleanup_and_exit(int ret) { exit(ret); }
17
18 int main(int argc, char **argv) {
19         if(argc || argv) {;}
20
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.");
24
25         struct rusage start, end;
26         unsigned long long dt;
27         char buf[100 + 1];
28         struct myvalue value, *v;
29         int i, max = 30000000, max2;
30
31         // ------------------------------------------------------------------------
32
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++) {
37                 value.i = i;
38                 snprintf(buf, 100, "%d", i);
39
40                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
41         }
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);
46
47         // ------------------------------------------------------------------------
48
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++) {
53                 value.i = i;
54                 snprintf(buf, 100, "%d", i);
55
56                 v = dictionary_get(dict, buf);
57                 if(!v)
58                         fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
59                 else if(v->i != i)
60                         fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
61         }
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);
66
67         // ------------------------------------------------------------------------
68
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++) {
73                 value.i = i;
74                 snprintf(buf, 100, "%d", i);
75
76                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
77         }
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);
82
83         // ------------------------------------------------------------------------
84
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);
88         max2 = max * 2;
89         for(i = max; i < max2; i++) {
90                 value.i = i;
91                 snprintf(buf, 100, "%d", i);
92
93                 v = dictionary_get(dict, buf);
94                 if(v)
95                         fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
96         }
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);
101
102         // ------------------------------------------------------------------------
103
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++) {
108                 value.i = i;
109                 snprintf(buf, 100, "%d", i);
110
111                 dictionary_del(dict, buf);
112         }
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);
117
118         // ------------------------------------------------------------------------
119
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);
127
128         return 0;
129 }