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).
5 #include <ccan/timer/timer.h>
6 #include <ccan/opt/opt.h>
7 #include <ccan/array_size/array_size.h>
10 #define PER_CONN_TIME 8192
11 #define CONN_TIMEOUT_MS 30000
13 int main(int argc, char *argv[])
15 struct timespec start, curr;
17 struct list_head expired;
18 struct timer t[PER_CONN_TIME];
22 opt_register_noarg("-c|--check", opt_set_bool, &check,
23 "Check timer structure during progress");
25 opt_parse(&argc, argv, opt_log_stderr_exit);
27 num = argv[1] ? atoi(argv[1]) : (check ? 100000 : 100000000);
29 list_head_init(&expired);
30 curr = start = time_now();
31 timers_init(&timers, start);
33 for (i = 0; i < num; i++) {
34 curr = time_add(curr, time_from_msec(1));
36 timers_check(&timers, NULL);
37 if (timers_expire(&timers, curr))
40 timers_check(&timers, NULL);
42 if (i >= PER_CONN_TIME) {
43 timer_del(&timers, &t[i%PER_CONN_TIME]);
45 timers_check(&timers, NULL);
47 timer_add(&timers, &t[i%PER_CONN_TIME],
48 time_add(curr, time_from_msec(CONN_TIMEOUT_MS)));
50 timers_check(&timers, NULL);
52 if (num > PER_CONN_TIME) {
53 for (i = 0; i < PER_CONN_TIME; i++)
54 timer_del(&timers, &t[i]);
57 curr = time_sub(time_now(), start);
59 timers_check(&timers, NULL);
60 timers_cleanup(&timers);
63 for (i = 0; i < ARRAY_SIZE(timers.level); i++)
67 printf("%u in %lu.%09lu (%u levels / %zu)\n",
68 num, (long)curr.tv_sec, curr.tv_nsec,
69 i, ARRAY_SIZE(timers.level));