Skip to content

Commit

Permalink
Add performance benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Mar 30, 2023
1 parent 767e6db commit e0964a9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = ["", "macro"]
default-members = ["macro"]
default-members = ["", "macro"]

[package]
name = "pollster"
Expand All @@ -14,6 +14,10 @@ edition = "2018"
license = "Apache-2.0/MIT"
readme = "README.md"

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

[features]
macro = ["pollster-macro"]

Expand All @@ -23,6 +27,7 @@ pollster-macro = { version = "0.1", path = "macro", optional = true }
[dev-dependencies]
futures-timer = "3.0"
tokio = { version = "1", features = ["sync"] }
criterion = "0.4"

[package.metadata.docs.rs]
all-features = true
Expand Down
63 changes: 63 additions & 0 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use criterion::*;

use core::future::{poll_fn, ready};
use core::task::{Poll, Waker};
use pollster::block_on;

fn benches() {
let mut c = Criterion::default();

// Benchmark raw performance of pollster - processing of ready futures
c.bench_function("poll_ready", |b| {
b.iter(|| block_on(ready(black_box(false))))
});

// Benchmark how pollster fares with futures that return Pending,
// but immediately wake up the thread.
c.bench_function("wakeup_and_pending", |b| {
b.iter(|| {
let mut polled = false;
let fut = poll_fn(|cx| {
if !polled {
polled = true;
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
});
block_on(fut)
})
});

// Benchmark how pollster fares with futures that get woken up from another thread.
let (tx, rx) = std::sync::mpsc::channel::<Waker>();

let thread = std::thread::spawn(move || {
for waker in rx.into_iter() {
waker.wake();
}
});

c.bench_function("wait_for_thread", |b| {
b.iter(|| {
let mut polled = false;
let fut = poll_fn(|cx| {
if !polled {
polled = true;
tx.send(cx.waker().clone()).unwrap();
Poll::Pending
} else {
Poll::Ready(())
}
});
block_on(fut)
})
});

core::mem::drop(tx);

thread.join().unwrap();
}

criterion_main!(benches);

0 comments on commit e0964a9

Please sign in to comment.