]> arthur.barton.de Git - netdata.git/blob - profile/benchmark-registry.c
Merge pull request #2022 from l2isbad/dns_query_time_fixes
[netdata.git] / profile / benchmark-registry.c
1
2 /*
3  * compile with
4  *  gcc -O1 -ggdb -Wall -Wextra -I ../src/ -I ../ -o benchmark-registry benchmark-registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o ../src/appconfig.o ../src/web_buffer.o ../src/storage_number.o ../src/rrd.o ../src/health.o -pthread -luuid -lm -DHAVE_CONFIG_H -DVARLIB_DIR="\"/tmp\""
5  */
6
7 char *hostname = "me";
8
9 #include "../src/registry.c"
10
11 void netdata_cleanup_and_exit(int ret) { exit(ret); }
12
13 // ----------------------------------------------------------------------------
14 // TESTS
15
16 int test1(int argc, char **argv) {
17
18         void print_stats(uint32_t requests, unsigned long long start, unsigned long long end) {
19                 fprintf(stderr, " > SPEED: %u requests served in %0.2f seconds ( >>> %llu per second <<< )\n",
20                                 requests, (end-start) / 1000000.0, (unsigned long long)requests * 1000000ULL / (end-start));
21
22                 fprintf(stderr, " > DB   : persons %llu, machines %llu, unique URLs %llu, accesses %llu, URLs: for persons %llu, for machines %llu\n",
23                                 registry.persons_count, registry.machines_count, registry.urls_count, registry.usages_count,
24                                 registry.persons_urls_count, registry.machines_urls_count);
25         }
26
27         (void) argc;
28         (void) argv;
29
30         uint32_t u, users = 1000000;
31         uint32_t m, machines = 200000;
32         uint32_t machines2 = machines * 2;
33
34         char **users_guids = malloc(users * sizeof(char *));
35         char **machines_guids = malloc(machines2 * sizeof(char *));
36         char **machines_urls = malloc(machines2 * sizeof(char *));
37         unsigned long long start;
38
39         registry_init();
40
41         fprintf(stderr, "Generating %u machine guids\n", machines2);
42         for(m = 0; m < machines2 ;m++) {
43                 uuid_t uuid;
44                 machines_guids[m] = malloc(36+1);
45                 uuid_generate(uuid);
46                 uuid_unparse(uuid, machines_guids[m]);
47
48                 char buf[FILENAME_MAX + 1];
49                 snprintfz(buf, FILENAME_MAX, "http://%u.netdata.rocks/", m+1);
50                 machines_urls[m] = strdup(buf);
51
52                 // fprintf(stderr, "\tmachine %u: '%s', url: '%s'\n", m + 1, machines_guids[m], machines_urls[m]);
53         }
54
55         start = timems();
56         fprintf(stderr, "\nGenerating %u users accessing %u machines\n", users, machines);
57         m = 0;
58         time_t now = time(NULL);
59         for(u = 0; u < users ; u++) {
60                 if(++m == machines) m = 0;
61
62                 PERSON *p = registry_request_access(NULL, machines_guids[m], machines_urls[m], "test", now);
63                 users_guids[u] = p->guid;
64         }
65         print_stats(u, start, timems());
66
67         start = timems();
68         fprintf(stderr, "\nAll %u users accessing again the same %u servers\n", users, machines);
69         m = 0;
70         now = time(NULL);
71         for(u = 0; u < users ; u++) {
72                 if(++m == machines) m = 0;
73
74                 PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
75
76                 if(p->guid != users_guids[u])
77                         fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
78         }
79         print_stats(u, start, timems());
80
81         start = timems();
82         fprintf(stderr, "\nAll %u users accessing a new server, out of the %u servers\n", users, machines);
83         m = 1;
84         now = time(NULL);
85         for(u = 0; u < users ; u++) {
86                 if(++m == machines) m = 0;
87
88                 PERSON *p = registry_request_access(users_guids[u], machines_guids[m], machines_urls[m], "test", now);
89
90                 if(p->guid != users_guids[u])
91                         fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[u], p->guid);
92         }
93         print_stats(u, start, timems());
94
95         start = timems();
96         fprintf(stderr, "\n%u random users accessing a random server, out of the %u servers\n", users, machines);
97         now = time(NULL);
98         for(u = 0; u < users ; u++) {
99                 uint32_t tu = random() * users / RAND_MAX;
100                 uint32_t tm = random() * machines / RAND_MAX;
101
102                 PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
103
104                 if(p->guid != users_guids[tu])
105                         fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
106         }
107         print_stats(u, start, timems());
108
109         start = timems();
110         fprintf(stderr, "\n%u random users accessing a random server, out of %u servers\n", users, machines2);
111         now = time(NULL);
112         for(u = 0; u < users ; u++) {
113                 uint32_t tu = random() * users / RAND_MAX;
114                 uint32_t tm = random() * machines2 / RAND_MAX;
115
116                 PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], machines_urls[tm], "test", now);
117
118                 if(p->guid != users_guids[tu])
119                         fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
120         }
121         print_stats(u, start, timems());
122
123         for(m = 0; m < 10; m++) {
124                 start = timems();
125                 fprintf(stderr,
126                                 "\n%u random user accesses to a random server, out of %u servers,\n > using 1/10000 with a random url, 1/1000 with a mismatched url\n",
127                                 users * 2, machines2);
128                 now = time(NULL);
129                 for (u = 0; u < users * 2; u++) {
130                         uint32_t tu = random() * users / RAND_MAX;
131                         uint32_t tm = random() * machines2 / RAND_MAX;
132
133                         char *url = machines_urls[tm];
134                         char buf[FILENAME_MAX + 1];
135                         if (random() % 10000 == 1234) {
136                                 snprintfz(buf, FILENAME_MAX, "http://random.%ld.netdata.rocks/", random());
137                                 url = buf;
138                         }
139                         else if (random() % 1000 == 123)
140                                 url = machines_urls[random() * machines2 / RAND_MAX];
141
142                         PERSON *p = registry_request_access(users_guids[tu], machines_guids[tm], url, "test", now);
143
144                         if (p->guid != users_guids[tu])
145                                 fprintf(stderr, "ERROR: expected to get user guid '%s' but git '%s'", users_guids[tu], p->guid);
146                 }
147                 print_stats(u, start, timems());
148         }
149
150         fprintf(stderr, "\n\nSAVE\n");
151         start = timems();
152         registry_save();
153         print_stats(registry.persons_count, start, timems());
154
155         fprintf(stderr, "\n\nCLEANUP\n");
156         start = timems();
157         registry_free();
158         print_stats(registry.persons_count, start, timems());
159         return 0;
160 }
161
162 // ----------------------------------------------------------------------------
163 // TESTING
164
165 int main(int argc, char **argv) {
166         config_set_boolean("registry", "enabled", 1);
167
168         //debug_flags = 0xFFFFFFFF;
169         test1(argc, argv);
170         exit(0);
171
172         (void)argc;
173         (void)argv;
174
175
176         PERSON *p1, *p2;
177
178         fprintf(stderr, "\n\nINITIALIZATION\n");
179
180         registry_init();
181
182         int i = 2;
183
184         fprintf(stderr, "\n\nADDING ENTRY\n");
185         p1 = registry_request_access("2c95abd0-1542-11e6-8c66-00508db7e9c9", "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
186
187         if(0)
188         while(i--) {
189 #ifdef REGISTRY_STDOUT_DUMP
190                 fprintf(stderr, "\n\nADDING ENTRY\n");
191 #endif /* REGISTRY_STDOUT_DUMP */
192                 p1 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://localhost:19999/", "test", time(NULL));
193
194 #ifdef REGISTRY_STDOUT_DUMP
195                 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
196 #endif /* REGISTRY_STDOUT_DUMP */
197                 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://127.0.0.1:19999/", "test", time(NULL));
198
199 #ifdef REGISTRY_STDOUT_DUMP
200                 fprintf(stderr, "\n\nADDING ANOTHER URL\n");
201 #endif /* REGISTRY_STDOUT_DUMP */
202                 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
203
204 #ifdef REGISTRY_STDOUT_DUMP
205                 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
206 #endif /* REGISTRY_STDOUT_DUMP */
207                 p1 = registry_request_access(p1->guid, "7c173980-145c-11e6-b86f-00508db7e9c1", "http://my.server:19999/", "test", time(NULL));
208
209 #ifdef REGISTRY_STDOUT_DUMP
210                 fprintf(stderr, "\n\nADDING ANOTHER PERSON\n");
211 #endif /* REGISTRY_STDOUT_DUMP */
212                 p2 = registry_request_access(NULL, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
213
214 #ifdef REGISTRY_STDOUT_DUMP
215                 fprintf(stderr, "\n\nADDING ANOTHER MACHINE\n");
216 #endif /* REGISTRY_STDOUT_DUMP */
217                 p2 = registry_request_access(p2->guid, "7c173980-145c-11e6-b86f-00508db7e9c3", "http://localhost:19999/", "test", time(NULL));
218         }
219
220         fprintf(stderr, "\n\nSAVE\n");
221         registry_save();
222
223         fprintf(stderr, "\n\nCLEANUP\n");
224         registry_free();
225         return 0;
226 }