Skip to content

Commit

Permalink
add benches for timer extension
Browse files Browse the repository at this point in the history
add benchmarks specifically targeting the use-case
of extending a timer by removing and re-registering
  • Loading branch information
cmeissl committed Jun 8, 2024
1 parent 9558927 commit 89d2703
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ nix = { version = "0.29", default-features = false, features = ["signal"], optio
[dev-dependencies]
futures = "0.3.5"
rustix = { version = "0.38", default-features = false, features = ["net"] }
criterion = { version = "0.4" }

[features]
block_on = ["pin-utils"]
Expand All @@ -48,3 +49,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[[test]]
name = "signals"
harness = false

[[bench]]
name = "timer"
harness = false
100 changes: 100 additions & 0 deletions benches/timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::time::Duration;

use calloop::timer::TimeoutAction;
use criterion::{criterion_group, criterion_main, Criterion};

fn single(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

c.bench_function("extend_single", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

fn mixed(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

c.bench_function("extend_mixed", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

fn mixed_multiple(c: &mut Criterion) {
let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
let loop_handle = event_loop.handle();

for _ in 0..1000 {
let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10 - 1));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();
}

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
let mut timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

for _ in 0..1000 {
let timer = calloop::timer::Timer::from_duration(Duration::from_secs(90 * 10));
loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();
}

c.bench_function("extend_mixed_many", |b| {
b.iter(|| {
loop_handle.remove(timeout_token);

let timer = calloop::timer::Timer::from_duration(Duration::from_secs(60 * 10));
timeout_token = loop_handle
.insert_source(timer, |_, _, _| TimeoutAction::Drop)
.unwrap();

event_loop.dispatch(Some(Duration::ZERO), &mut ()).unwrap();
});
});
}

criterion_group!(benches, single, mixed, mixed_multiple);
criterion_main!(benches);

0 comments on commit 89d2703

Please sign in to comment.