]> git.ozlabs.org Git - ccan/blob - ccan/timer/benchmarks/expected-usage.c
timer: cache the minimal value.
[ccan] / ccan / timer / benchmarks / expected-usage.c
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
10 #define PER_CONN_TIME 8192
11 #define CONN_TIMEOUT_MS 30000
12
13 int main(int argc, char *argv[])
14 {
15         struct timespec start, curr;
16         struct timers timers;
17         struct list_head expired;
18         struct timer t[PER_CONN_TIME];
19         unsigned int i, num;
20         bool check = false;
21
22         opt_register_noarg("-c|--check", opt_set_bool, &check,
23                            "Check timer structure during progress");
24
25         opt_parse(&argc, argv, opt_log_stderr_exit);
26
27         num = argv[1] ? atoi(argv[1]) : (check ? 100000 : 100000000);
28
29         list_head_init(&expired);
30         curr = start = time_now();
31         timers_init(&timers, start);
32
33         for (i = 0; i < num; i++) {
34                 curr = time_add(curr, time_from_msec(1));
35                 if (check)
36                         timers_check(&timers, NULL);
37                 timers_expire(&timers, curr, &expired);
38                 if (check)
39                         timers_check(&timers, NULL);
40                 assert(list_empty(&expired));
41
42                 if (i >= PER_CONN_TIME) {
43                         timer_del(&timers, &t[i%PER_CONN_TIME]);
44                         if (check)
45                                 timers_check(&timers, NULL);
46                 }
47                 timer_add(&timers, &t[i%PER_CONN_TIME],
48                           time_add(curr, time_from_msec(CONN_TIMEOUT_MS)));
49                 if (check)
50                         timers_check(&timers, NULL);
51         }
52         if (num > PER_CONN_TIME) {
53                 for (i = 0; i < PER_CONN_TIME; i++)
54                         timer_del(&timers, &t[i]);
55         }
56
57         curr = time_sub(time_now(), start);
58         if (check)
59                 timers_check(&timers, NULL);
60         timers_cleanup(&timers);
61         opt_free_table();
62
63         for (i = 0; i < ARRAY_SIZE(timers.level); i++)
64                 if (!timers.level[i])
65                         break;
66
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));
70         return 0;
71 }