switch_event.c 2.9 KB

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