Skip to content

bench: Add benchmarks for timers #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,7 +42,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);