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