Skip to content

Commit

Permalink
Add benchmarks for timers (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Apr 28, 2023
1 parent 174da0b commit 5d291fd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ exclude = ["/.*"]
name = "io"
harness = false

[[bench]]
name = "timer"
harness = false

[dependencies]
async-lock = "2.6"
cfg-if = "1"
Expand All @@ -37,7 +41,7 @@ autocfg = "1"
async-channel = "1"
async-net = "1"
blocking = "1"
criterion = "0.4"
criterion = { version = "0.4", default-features = false, features = ["cargo_bench_support"] }
getrandom = "0.2.7"
signal-hook = "0.3"
tempfile = "3"
Expand Down
39 changes: 39 additions & 0 deletions benches/timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Benchmarks for registering timers.
use async_io::Timer;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use futures_lite::future;
use std::time::Duration;

/// Create a new `Timer` and poll it once to register it into the timer wheel.
fn make_timer() -> Timer {
let mut timer = Timer::after(Duration::from_secs(1));
future::block_on(future::poll_once(&mut timer));
timer
}

/// Benchmark the time it takes to register and deregister a timer.
fn register_timer(c: &mut Criterion) {
let mut group = c.benchmark_group("register_timer");
for prev_timer_count in [0, 1_000_000] {
// Add timers to the timer wheel.
let mut timers = Vec::new();
for _ in 0..prev_timer_count {
timers.push(make_timer());
}

// Benchmark registering a timer.
group.bench_function(
format!("register_timer.({} previous timers)", prev_timer_count),
|b| {
b.iter(|| {
let timer = make_timer();
black_box(timer);
});
},
);
}
}

criterion_group!(benches, register_timer);
criterion_main!(benches);

0 comments on commit 5d291fd

Please sign in to comment.