2
0

switch_hash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <switch.h>
  2. #include <test/switch_test.h>
  3. // #define BENCHMARK 1
  4. FST_MINCORE_BEGIN("./conf")
  5. FST_SUITE_BEGIN(switch_hash)
  6. FST_SETUP_BEGIN()
  7. {
  8. }
  9. FST_SETUP_END()
  10. FST_TEARDOWN_BEGIN()
  11. {
  12. }
  13. FST_TEARDOWN_END()
  14. FST_TEST_BEGIN(benchmark)
  15. {
  16. switch_time_t start_ts, end_ts;
  17. uint64_t micro_total = 0;
  18. double micro_per = 0;
  19. double rate_per_sec = 0;
  20. int x = 0;
  21. #ifdef BENCHMARK
  22. switch_time_t small_start_ts, small_end_ts;
  23. #endif
  24. int loops = 10;
  25. switch_status_t status = SWITCH_STATUS_SUCCESS;
  26. char **index = NULL;
  27. switch_hash_t *hash = NULL;
  28. fst_requires(switch_core_hash_init(&hash) == SWITCH_STATUS_SUCCESS);
  29. index = calloc(loops, sizeof(char *));
  30. for ( x = 0; x < loops; x++) {
  31. index[x] = switch_mprintf("%d", x);
  32. }
  33. /* START LOOPS */
  34. start_ts = switch_time_now();
  35. /* Insertion */
  36. #ifndef BENCHMARK
  37. for ( x = 0; x < loops; x++) {
  38. status = switch_core_hash_insert(hash, index[x], (void *) index[x]);
  39. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Failed to insert into the hash");
  40. }
  41. #else
  42. small_start_ts = switch_time_now();
  43. for ( x = 0; x < loops; x++) {
  44. switch_core_hash_insert(hash, index[x], (void *) index[x]);
  45. }
  46. small_end_ts = switch_time_now();
  47. micro_total = small_end_ts - small_start_ts;
  48. micro_per = micro_total / (double) loops;
  49. rate_per_sec = 1000000 / micro_per;
  50. printf("switch_hash insert: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
  51. micro_total, loops, micro_per, rate_per_sec);
  52. #endif
  53. /* Lookup */
  54. #ifndef BENCHMARK
  55. for ( x = 0; x < loops; x++) {
  56. char *data = NULL;
  57. data = switch_core_hash_find(hash, index[x]);
  58. fst_xcheck(data != NULL, "Lookup failed");
  59. fst_check_string_equals( index[x], data);
  60. }
  61. #else
  62. small_start_ts = switch_time_now();
  63. for ( x = 0; x < loops; x++) {
  64. if ( ! switch_core_hash_find(hash, index[x])) {
  65. fst_fail("Failed to properly locate one of the values");
  66. }
  67. }
  68. small_end_ts = switch_time_now();
  69. micro_total = small_end_ts - small_start_ts;
  70. micro_per = micro_total / (double) loops;
  71. rate_per_sec = 1000000 / micro_per;
  72. printf("switch_hash find: Total %ldus / %ld loops, %.2f us per loop, %.0f loops per second\n",
  73. micro_total, loops, micro_per, rate_per_sec);
  74. #endif
  75. /* Delete */
  76. #ifndef BENCHMARK
  77. for ( x = 0; x < loops; x++) {
  78. char *data = NULL;
  79. data = switch_core_hash_delete(hash, index[x]);
  80. fst_xcheck(data != NULL, "Delete from the hash");
  81. fst_check_string_equals( index[x], data );
  82. }
  83. #else
  84. small_start_ts = switch_time_now();
  85. for ( x = 0; x < loops; x++) {
  86. if ( !switch_core_hash_delete(hash, index[x])) {
  87. fst_fail("Failed to delete and return the value");
  88. }
  89. }
  90. small_end_ts = switch_time_now();
  91. micro_total = small_end_ts - small_start_ts;
  92. micro_per = micro_total / (double) loops;
  93. rate_per_sec = 1000000 / micro_per;
  94. printf("switch_hash delete: Total %ldus / %d loops, %.2f us per loop, %.0f loops per second\n",
  95. micro_total, loops, micro_per, rate_per_sec);
  96. #endif
  97. end_ts = switch_time_now();
  98. /* END LOOPS */
  99. switch_core_hash_destroy(&hash);
  100. for ( x = 0; x < loops; x++) {
  101. free(index[x]);
  102. }
  103. free(index);
  104. micro_total = end_ts - start_ts;
  105. micro_per = micro_total / (double) loops;
  106. rate_per_sec = 1000000 / micro_per;
  107. printf("switch_hash Total %" SWITCH_UINT64_T_FMT "us / %d loops, %.2f us per loop, %.0f loops per second\n",
  108. micro_total, loops, micro_per, rate_per_sec);
  109. }
  110. FST_TEST_END()
  111. FST_SUITE_END()
  112. FST_MINCORE_END()