expected-usage.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* We expect a timer to rarely go off, so benchmark that case:
  2. * Every 1ms a connection comes in, we set up a 30 second timer for it.
  3. * After 8192ms we finish the connection (and thus delete the timer).
  4. */
  5. #include <ccan/timer/timer.h>
  6. #include <ccan/opt/opt.h>
  7. #include <ccan/array_size/array_size.h>
  8. #include <stdio.h>
  9. #define PER_CONN_TIME 8192
  10. #define CONN_TIMEOUT_MS 30000
  11. int main(int argc, char *argv[])
  12. {
  13. struct timespec start, curr;
  14. struct timers timers;
  15. struct list_head expired;
  16. struct timer t[PER_CONN_TIME];
  17. unsigned int i, num;
  18. bool check = false;
  19. opt_register_noarg("-c|--check", opt_set_bool, &check,
  20. "Check timer structure during progress");
  21. opt_parse(&argc, argv, opt_log_stderr_exit);
  22. num = argv[1] ? atoi(argv[1]) : (check ? 100000 : 100000000);
  23. list_head_init(&expired);
  24. curr = start = time_now();
  25. timers_init(&timers, start);
  26. for (i = 0; i < num; i++) {
  27. curr = time_add(curr, time_from_msec(1));
  28. if (check)
  29. timers_check(&timers, NULL);
  30. if (timers_expire(&timers, curr))
  31. abort();
  32. if (check)
  33. timers_check(&timers, NULL);
  34. if (i >= PER_CONN_TIME) {
  35. timer_del(&timers, &t[i%PER_CONN_TIME]);
  36. if (check)
  37. timers_check(&timers, NULL);
  38. }
  39. timer_add(&timers, &t[i%PER_CONN_TIME],
  40. time_add(curr, time_from_msec(CONN_TIMEOUT_MS)));
  41. if (check)
  42. timers_check(&timers, NULL);
  43. }
  44. if (num > PER_CONN_TIME) {
  45. for (i = 0; i < PER_CONN_TIME; i++)
  46. timer_del(&timers, &t[i]);
  47. }
  48. curr = time_sub(time_now(), start);
  49. if (check)
  50. timers_check(&timers, NULL);
  51. timers_cleanup(&timers);
  52. opt_free_table();
  53. for (i = 0; i < ARRAY_SIZE(timers.level); i++)
  54. if (!timers.level[i])
  55. break;
  56. printf("%u in %lu.%09lu (%u levels / %zu)\n",
  57. num, (long)curr.tv_sec, curr.tv_nsec,
  58. i, ARRAY_SIZE(timers.level));
  59. return 0;
  60. }