test-timers.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright 2016, 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. #ifdef HAVE_EVENT2_EVENT_H
  8. #include <event2/event.h>
  9. #else
  10. #include <event.h>
  11. #endif
  12. #include "compat.h"
  13. #include "compat_libevent.h"
  14. #include "crypto.h"
  15. #include "timers.h"
  16. #include "util.h"
  17. #define N_TIMERS 1000
  18. #define MAX_DURATION 30
  19. static struct timeval fire_at[N_TIMERS] = {{0,0}};
  20. static int fired[N_TIMERS] = {0};
  21. static struct timeval difference[N_TIMERS] = {{0,0}};
  22. static tor_timer_t *timers[N_TIMERS] = {NULL};
  23. static int n_fired = 0;
  24. static void
  25. timer_cb(tor_timer_t *t, void *arg, const struct timeval *now)
  26. {
  27. tor_timer_t **t_ptr = arg;
  28. tor_assert(*t_ptr == t);
  29. int idx = (int) (t_ptr - timers);
  30. ++fired[idx];
  31. timersub(now, &fire_at[idx], &difference[idx]);
  32. ++n_fired;
  33. // printf("%d / %d\n",n_fired, N_TIMERS);
  34. if (n_fired == N_TIMERS) {
  35. event_base_loopbreak(tor_libevent_get_base());
  36. }
  37. }
  38. int
  39. main(int argc, char **argv)
  40. {
  41. (void)argc;
  42. (void)argv;
  43. tor_libevent_cfg cfg;
  44. memset(&cfg, 0, sizeof(cfg));
  45. tor_libevent_initialize(&cfg);
  46. timers_initialize();
  47. int i;
  48. struct timeval now;
  49. tor_gettimeofday(&now);
  50. for (i = 0; i < N_TIMERS; ++i) {
  51. struct timeval delay;
  52. delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION);
  53. delay.tv_usec = crypto_rand_int_range(0,1000000);
  54. timeradd(&now, &delay, &fire_at[i]);
  55. timers[i] = timer_new(timer_cb, &timers[i], 0);
  56. timer_schedule(timers[i], &delay);
  57. }
  58. event_base_loop(tor_libevent_get_base(), 0);
  59. uint64_t total_difference = 0;
  60. uint64_t total_square_difference = 0;
  61. tor_assert(n_fired == N_TIMERS);
  62. for (i = 0; i < N_TIMERS; ++i) {
  63. tor_assert(fired[i] == 1);
  64. uint64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000;
  65. total_difference += diff;
  66. total_square_difference += diff*diff;
  67. }
  68. const uint64_t mean_diff = total_difference / N_TIMERS;
  69. printf("mean difference: "U64_FORMAT" usec\n",
  70. U64_PRINTF_ARG(mean_diff));
  71. const double mean_sq = ((double)total_square_difference) / N_TIMERS;
  72. const double sq_mean = mean_diff * mean_diff;
  73. const double stddev = sqrt(mean_sq - sq_mean);
  74. printf("standard deviation: %lf usec\n", stddev);
  75. if (mean_diff > 500*1000 || stddev > 500*1000) {
  76. printf("Either your system is under ridiculous load, or the "
  77. "timer backend is broken.\n");
  78. return 1;
  79. } else if (mean_diff > 2000 || stddev > 2000) {
  80. printf("Either your system is a bit slow or the "
  81. "timer backend is odd.\n");
  82. return 0;
  83. } else {
  84. printf("Looks good enough.\n");
  85. }
  86. return 0;
  87. }