benchmark-registry.c 7.6 KB

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