Debian Codegraph @

benches/timer.rs

raw
1 use std::time::Duration;
2
3 use calloop::timer::TimeoutAction;
4 use criterion::{criterion_group, criterion_main, Criterion};
5
6 fn single(c: &mut Criterion) {
7 let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
8 let loop_handle = event_loop.handle();
9
10 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
11 let mut timeout_token = loop_handle
12 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
13 .unwrap();
14
15 c.bench_function("extend_single", |b| {
16 b.iter(|| {
17 loop_handle.remove(timeout_token);
18
19 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
20 timeout_token = loop_handle
21 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
22 .unwrap();
23
24 event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
25 });
26 });
27 }
28
29 fn mixed(c: &mut Criterion) {
30 let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
31 let loop_handle = event_loop.handle();
32
33 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
34 loop_handle
35 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
36 .unwrap();
37
38 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
39 let mut timeout_token = loop_handle
40 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
41 .unwrap();
42
43 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
44 loop_handle
45 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
46 .unwrap();
47
48 c.bench_function("extend_mixed", |b| {
49 b.iter(|| {
50 loop_handle.remove(timeout_token);
51
52 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
53 timeout_token = loop_handle
54 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
55 .unwrap();
56
57 event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
58 });
59 });
60 }
61
62 fn mixed_multiple(c: &mut Criterion) {
63 let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
64 let loop_handle = event_loop.handle();
65
66 for _ in 0..1000 {
67 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
68 loop_handle
69 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
70 .unwrap();
71 }
72
73 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
74 let mut timeout_token = loop_handle
75 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
76 .unwrap();
77
78 for _ in 0..1000 {
79 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
80 loop_handle
81 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
82 .unwrap();
83 }
84
85 c.bench_function("extend_mixed_many", |b| {
86 b.iter(|| {
87 loop_handle.remove(timeout_token);
88
89 let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
90 timeout_token = loop_handle
91 .insert_source(timer, |_, _, _| TimeoutAction::Drop)
92 .unwrap();
93
94 event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
95 });
96 });
97 }
98
99 criterion_group!(benches, single, mixed, mixed_multiple);
100 criterion_main!(benches);