Skip to content

Commit

Permalink
test(benches): measure overhead on yielding
Browse files Browse the repository at this point in the history
  • Loading branch information
loyd committed Jun 16, 2024
1 parent 9a86cac commit fb30cd0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ harness = false
name = "trace_id"
path = "trace_id.rs"
harness = false

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

use criterion::{criterion_group, criterion_main, Criterion};

use elfo::Context;

mod common;

fn baseline(c: &mut Criterion) {
async fn testee(iter_count: u64) -> Duration {
let start = Instant::now();
for _ in 0..iter_count {
tokio::task::yield_now().await;
}
start.elapsed()
}

c.bench_function("baseline", |b| {
b.iter_custom(|iter_count| {
common::make_st_runtime().block_on(async {
tokio::task::spawn(async move { testee(iter_count).await })
.await
.unwrap()
})
})
});
}

async fn testee(_ctx: Context, iter_count: u64) -> Duration {
let start = Instant::now();
for _ in 0..iter_count {
tokio::task::yield_now().await;
}
start.elapsed()
}

fn without_telemetry(c: &mut Criterion) {
c.bench_function("without_telemetry", |b| {
assert!(metrics::try_recorder().is_none());
b.iter_custom(|iter_count| common::bench_singleton(iter_count, testee))
});
}

// TODO: with stuck detection (requires runtime parameter).

fn with_telemetry(c: &mut Criterion) {
c.bench_function("with_telemetry", |b| {
// Install a recorder but don't run the telemeter actor.
elfo::batteries::telemeter::init();
assert!(metrics::try_recorder().is_some());
b.iter_custom(|iter_count| common::bench_singleton(iter_count, testee))
});
}

criterion_group!(cases, baseline, without_telemetry, with_telemetry);
criterion_main!(cases);

0 comments on commit fb30cd0

Please sign in to comment.