]> arthur.barton.de Git - netdata.git/blob - profile/benchmark-dictionary.c
lower dictionary memory requirements by keeping pointers to optional features
[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_WITH_STATISTICS);
26         DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
27         if(!dict) fatal("Cannot create dictionary.");
28
29         struct rusage start, end;
30         unsigned long long dt;
31         char buf[100 + 1];
32         struct myvalue value, *v;
33         int i, max = 100000, max2;
34
35         // ------------------------------------------------------------------------
36
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++) {
41                 value.i = i;
42                 snprintf(buf, 100, "%d", i);
43
44                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
45         }
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);
50
51         // ------------------------------------------------------------------------
52
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++) {
57                 value.i = i;
58                 snprintf(buf, 100, "%d", i);
59
60                 v = dictionary_get(dict, buf);
61                 if(!v)
62                         fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
63                 else if(v->i != i)
64                         fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
65         }
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);
70
71         // ------------------------------------------------------------------------
72
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++) {
77                 value.i = i;
78                 snprintf(buf, 100, "%d", i);
79
80                 dictionary_set(dict, buf, &value, sizeof(struct myvalue));
81         }
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);
86
87         // ------------------------------------------------------------------------
88
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);
92         max2 = max * 2;
93         for(i = max; i < max2; i++) {
94                 value.i = i;
95                 snprintf(buf, 100, "%d", i);
96
97                 v = dictionary_get(dict, buf);
98                 if(v)
99                         fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
100         }
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);
105
106         // ------------------------------------------------------------------------
107
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++) {
112                 value.i = i;
113                 snprintf(buf, 100, "%d", i);
114
115                 dictionary_del(dict, buf);
116         }
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);
121
122         // ------------------------------------------------------------------------
123
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);
131
132         return 0;
133 }