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