switch_event.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <switch.h>
  2. #include <test/switch_test.h>
  3. // #define BENCHMARK 1
  4. FST_MINCORE_BEGIN("./conf")
  5. FST_SUITE_BEGIN(switch_event)
  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_event_t *event = NULL;
  17. switch_time_t start_ts, end_ts;
  18. int loops = 10, x = 0;
  19. switch_status_t status = SWITCH_STATUS_SUCCESS;
  20. char **index = NULL;
  21. uint64_t micro_total = 0;
  22. double micro_per = 0;
  23. double rate_per_sec = 0;
  24. #ifdef BENCHMARK
  25. switch_time_t small_start_ts, small_end_ts;
  26. #endif
  27. index = calloc(loops, sizeof(char *));
  28. for ( x = 0; x < loops; x++) {
  29. index[x] = switch_mprintf("%d", x);
  30. }
  31. /* START LOOPS */
  32. start_ts = switch_time_now();
  33. status = switch_event_create(&event, SWITCH_EVENT_MESSAGE);
  34. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Failed to create event");
  35. #ifndef BENCHMARK
  36. for ( x = 0; x < loops; x++) {
  37. status = switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, index[x], index[x]);
  38. fst_xcheck(status == SWITCH_STATUS_SUCCESS, "Failed to add header to event");
  39. }
  40. #else
  41. small_start_ts = switch_time_now();
  42. for ( x = 0; x < loops; x++) {
  43. if ( switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, index[x], index[x]) != SWITCH_STATUS_SUCCESS) {
  44. fst_fail("Failed to add header to event");
  45. }
  46. }
  47. small_end_ts = switch_time_now();
  48. micro_total = small_end_ts - small_start_ts;
  49. micro_per = micro_total / (double) loops;
  50. rate_per_sec = 1000000 / micro_per;
  51. printf("switch_event add_header: Total %" SWITCH_UINT64_T_FMT "us / %d loops, %.2f us per loop, %.0f loops per second\n",
  52. micro_total, loops, micro_per, rate_per_sec);
  53. #endif
  54. #ifndef BENCHMARK
  55. for ( x = 0; x < loops; x++) {
  56. fst_check_string_equals(switch_event_get_header(event, index[x]), index[x]);
  57. }
  58. #else
  59. small_start_ts = switch_time_now();
  60. for ( x = 0; x < loops; x++) {
  61. if ( !switch_event_get_header(event, index[x])) {
  62. fst_fail("Failed to lookup event header value");
  63. }
  64. }
  65. small_end_ts = switch_time_now();
  66. micro_total = small_end_ts - small_start_ts;
  67. micro_per = micro_total / (double) loops;
  68. rate_per_sec = 1000000 / micro_per;
  69. printf("switch_event get_header: Total %" SWITCH_UINT64_T_FMT "us / %d loops, %.2f us per loop, %.0f loops per second\n",
  70. micro_total, loops, micro_per, rate_per_sec);
  71. #endif
  72. switch_event_destroy(&event);
  73. /* END LOOPS */
  74. end_ts = switch_time_now();
  75. for ( x = 0; x < loops; x++) {
  76. free(index[x]);
  77. }
  78. free(index);
  79. micro_total = end_ts - start_ts;
  80. micro_per = micro_total / (double) loops;
  81. rate_per_sec = 1000000 / micro_per;
  82. printf("switch_event Total %" SWITCH_UINT64_T_FMT "us / %d loops, %.2f us per loop, %.0f loops per second\n",
  83. micro_total, loops, micro_per, rate_per_sec);
  84. }
  85. FST_TEST_END()
  86. FST_SUITE_END()
  87. FST_MINCORE_END()