test-timers.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <event2/event.h>
  8. #include "compat.h"
  9. #include "compat_libevent.h"
  10. #include "crypto.h"
  11. #include "timers.h"
  12. #include "util.h"
  13. #define N_TIMERS 1000
  14. #define MAX_DURATION 30
  15. #define N_DISABLE 5
  16. static struct timeval fire_at[N_TIMERS] = { {0,0} };
  17. static int is_disabled[N_TIMERS] = {0};
  18. static int fired[N_TIMERS] = {0};
  19. static struct timeval difference[N_TIMERS] = { {0,0} };
  20. static tor_timer_t *timers[N_TIMERS] = {NULL};
  21. static int n_active_timers = 0;
  22. static int n_fired = 0;
  23. static monotime_t started_at;
  24. static int64_t delay_usec[N_TIMERS];
  25. static int64_t diffs_mono_usec[N_TIMERS];
  26. static void
  27. timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
  28. {
  29. struct timeval now;
  30. tor_gettimeofday(&now);
  31. tor_timer_t **t_ptr = arg;
  32. tor_assert(*t_ptr == t);
  33. int idx = (int) (t_ptr - timers);
  34. ++fired[idx];
  35. timersub(&now, &fire_at[idx], &difference[idx]);
  36. diffs_mono_usec[idx] =
  37. monotime_diff_usec(&started_at, now_mono) -
  38. delay_usec[idx];
  39. ++n_fired;
  40. // printf("%d / %d\n",n_fired, N_TIMERS);
  41. if (n_fired == n_active_timers) {
  42. event_base_loopbreak(tor_libevent_get_base());
  43. }
  44. }
  45. int
  46. main(int argc, char **argv)
  47. {
  48. (void)argc;
  49. (void)argv;
  50. tor_libevent_cfg cfg;
  51. memset(&cfg, 0, sizeof(cfg));
  52. tor_libevent_initialize(&cfg);
  53. timers_initialize();
  54. int i;
  55. int ret;
  56. struct timeval now;
  57. tor_gettimeofday(&now);
  58. monotime_get(&started_at);
  59. for (i = 0; i < N_TIMERS; ++i) {
  60. struct timeval delay;
  61. delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
  62. delay.tv_usec = crypto_rand_int_range(0,1000000);
  63. delay_usec[i] = delay.tv_sec * 1000000 + delay.tv_usec;
  64. timeradd(&now, &delay, &fire_at[i]);
  65. timers[i] = timer_new(timer_cb, &timers[i]);
  66. timer_schedule(timers[i], &delay);
  67. ++n_active_timers;
  68. }
  69. /* Disable some; we'll make sure they don't trigger. */
  70. for (i = 0; i < N_DISABLE; ++i) {
  71. int idx = crypto_rand_int_range(0, N_TIMERS);
  72. if (is_disabled[idx])
  73. continue;
  74. is_disabled[idx] = 1;
  75. timer_disable(timers[idx]);
  76. --n_active_timers;
  77. }
  78. event_base_loop(tor_libevent_get_base(), 0);
  79. int64_t total_difference = 0;
  80. uint64_t total_square_difference = 0;
  81. tor_assert(n_fired == n_active_timers);
  82. for (i = 0; i < N_TIMERS; ++i) {
  83. if (is_disabled[i]) {
  84. tor_assert(fired[i] == 0);
  85. continue;
  86. }
  87. tor_assert(fired[i] == 1);
  88. //int64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
  89. int64_t diff = diffs_mono_usec[i];
  90. total_difference += diff;
  91. total_square_difference += diff*diff;
  92. }
  93. const int64_t mean_diff = total_difference / n_active_timers;
  94. printf("mean difference: "I64_FORMAT" usec\n",
  95. I64_PRINTF_ARG(mean_diff));
  96. const double mean_sq = ((double)total_square_difference)/ n_active_timers;
  97. const double sq_mean = mean_diff * mean_diff;
  98. const double stddev = sqrt(mean_sq - sq_mean);
  99. printf("standard deviation: %lf usec\n", stddev);
  100. #define MAX_DIFF_USEC (500*1000)
  101. #define MAX_STDDEV_USEC (500*1000)
  102. #define ODD_DIFF_USEC (2000)
  103. #define ODD_STDDEV_USEC (2000)
  104. if (mean_diff < 0 || mean_diff > MAX_DIFF_USEC || stddev > MAX_STDDEV_USEC) {
  105. printf("Either your system is under ridiculous load, or the "
  106. "timer backend is broken.\n");
  107. ret = 1;
  108. } else if (mean_diff > ODD_DIFF_USEC || stddev > ODD_STDDEV_USEC) {
  109. printf("Either your system is a bit slow or the "
  110. "timer backend is odd.\n");
  111. ret = 0;
  112. } else {
  113. printf("Looks good enough.\n");
  114. ret = 0;
  115. }
  116. timer_free_(NULL);
  117. for (i = 0; i < N_TIMERS; ++i) {
  118. timer_free(timers[i]);
  119. }
  120. timers_shutdown();
  121. return ret;
  122. }