benchmark-dictionary.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* SPDX-License-Identifier: GPL-3.0-or-later */
  2. /*
  3. * 1. build netdata (as normally)
  4. * 2. cd tests/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. #include "config.h"
  10. #include "libnetdata/libnetdata.h"
  11. struct myvalue {
  12. int i;
  13. };
  14. void netdata_cleanup_and_exit(int ret) { exit(ret); }
  15. int main(int argc, char **argv) {
  16. if(argc || argv) {;}
  17. // DICTIONARY *dict = dictionary_create(DICT_OPTION_SINGLE_THREADED|DICT_OPTION_WITH_STATISTICS);
  18. DICTIONARY *dict = dictionary_create(DICT_OPTION_STATS);
  19. if(!dict) fatal("Cannot create dictionary.");
  20. struct rusage start, end;
  21. unsigned long long dt;
  22. char buf[100 + 1];
  23. struct myvalue value, *v;
  24. int i, max = 30000000, max2;
  25. // ------------------------------------------------------------------------
  26. getrusage(RUSAGE_SELF, &start);
  27. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  28. fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
  29. for(i = 0; i < max; i++) {
  30. value.i = i;
  31. snprintf(buf, 100, "%d", i);
  32. dictionary_set(dict, buf, &value, sizeof(struct myvalue));
  33. }
  34. getrusage(RUSAGE_SELF, &end);
  35. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  36. fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
  37. fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
  38. // ------------------------------------------------------------------------
  39. getrusage(RUSAGE_SELF, &start);
  40. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  41. fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
  42. for(i = 0; i < max; i++) {
  43. value.i = i;
  44. snprintf(buf, 100, "%d", i);
  45. v = dictionary_get(dict, buf);
  46. if(!v)
  47. fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
  48. else if(v->i != i)
  49. fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
  50. }
  51. getrusage(RUSAGE_SELF, &end);
  52. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  53. fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
  54. fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
  55. // ------------------------------------------------------------------------
  56. getrusage(RUSAGE_SELF, &start);
  57. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  58. fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
  59. for(i = 0; i < max; i++) {
  60. value.i = i;
  61. snprintf(buf, 100, "%d", i);
  62. dictionary_set(dict, buf, &value, sizeof(struct myvalue));
  63. }
  64. getrusage(RUSAGE_SELF, &end);
  65. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  66. fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt);
  67. fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
  68. // ------------------------------------------------------------------------
  69. getrusage(RUSAGE_SELF, &start);
  70. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  71. fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max);
  72. max2 = max * 2;
  73. for(i = max; i < max2; i++) {
  74. value.i = i;
  75. snprintf(buf, 100, "%d", i);
  76. v = dictionary_get(dict, buf);
  77. if(v)
  78. fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
  79. }
  80. getrusage(RUSAGE_SELF, &end);
  81. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  82. fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt);
  83. fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
  84. // ------------------------------------------------------------------------
  85. getrusage(RUSAGE_SELF, &start);
  86. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  87. fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
  88. for(i = 0; i < max; i++) {
  89. value.i = i;
  90. snprintf(buf, 100, "%d", i);
  91. dictionary_del(dict, buf);
  92. }
  93. getrusage(RUSAGE_SELF, &end);
  94. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  95. fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
  96. fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);
  97. // ------------------------------------------------------------------------
  98. getrusage(RUSAGE_SELF, &start);
  99. dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
  100. fprintf(stderr, "Destroying dictionary\n");
  101. dictionary_destroy(dict);
  102. getrusage(RUSAGE_SELF, &end);
  103. dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
  104. fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);
  105. return 0;
  106. }