From a988ee3e46d338e78fe38aa2066b9312c6ffe616 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Mon, 23 Jan 2023 19:30:43 +0000 Subject: [PATCH] m: Migrate benchmarks to criterion (#35) * m: Migrate to criterion * Update CI --- .github/workflows/ci.yml | 2 +- Cargo.toml | 5 ++ benches/executor.rs | 150 +++++++++++++++++++++------------------ 3 files changed, 88 insertions(+), 69 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index afa0537..ffcbcf4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ jobs: - uses: actions/checkout@v3 - name: Install Rust run: rustup update stable - - run: cargo clippy --all-features --tests --examples + - run: cargo clippy --all-features --all-targets fmt: runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index faee198..5bd5abd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,11 @@ slab = "0.4.4" [dev-dependencies] async-channel = "1.4.1" async-io = "1.1.9" +criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] } easy-parallel = "3.1.0" num_cpus = "1.13.0" once_cell = "1.16.0" + +[[bench]] +name = "executor" +harness = false diff --git a/benches/executor.rs b/benches/executor.rs index 98f1cb5..7d3ec8c 100644 --- a/benches/executor.rs +++ b/benches/executor.rs @@ -1,10 +1,7 @@ -#![feature(test)] - -extern crate test; - use std::future::Future; use async_executor::Executor; +use criterion::{criterion_group, criterion_main, Criterion}; use futures_lite::{future, prelude::*}; const TASKS: usize = 300; @@ -23,87 +20,104 @@ fn run(f: impl FnOnce()) { }); } -#[bench] -fn create(b: &mut test::Bencher) { - b.iter(move || { - let ex = Executor::new(); - let task = ex.spawn(async {}); - future::block_on(ex.run(task)); +fn create(c: &mut Criterion) { + c.bench_function("executor::create", |b| { + b.iter(|| { + let ex = Executor::new(); + let task = ex.spawn(async {}); + future::block_on(ex.run(task)); + }) }); } -#[bench] -fn spawn_one(b: &mut test::Bencher) { - run(|| { - b.iter(move || { - future::block_on(async { EX.spawn(async {}).await }); - }); - }); -} - -#[bench] -fn spawn_many(b: &mut test::Bencher) { - run(|| { - b.iter(move || { - future::block_on(async { - let mut tasks = Vec::new(); - for _ in 0..LIGHT_TASKS { - tasks.push(EX.spawn(async {})); - } - for task in tasks { - task.await; - } +fn spawn_one(c: &mut Criterion) { + c.bench_function("executor::spawn_one", |b| { + run(|| { + b.iter(|| { + future::block_on(async { EX.spawn(async {}).await }); }); }); }); } -#[bench] -fn spawn_recursively(b: &mut test::Bencher) { - fn go(i: usize) -> impl Future + Send + 'static { - async move { - if i != 0 { - EX.spawn(async move { - let fut = go(i - 1).boxed(); - fut.await; - }) - .await; +fn spawn_many(c: &mut Criterion) { + c.bench_function("executor::spawn_many_local", |b| { + run(|| { + b.iter(move || { + future::block_on(async { + let mut tasks = Vec::new(); + for _ in 0..LIGHT_TASKS { + tasks.push(EX.spawn(async {})); + } + for task in tasks { + task.await; + } + }); + }); + }); + }); +} + +fn spawn_recursively(c: &mut Criterion) { + c.bench_function("executor::spawn_recursively", |b| { + #[allow(clippy::manual_async_fn)] + fn go(i: usize) -> impl Future + Send + 'static { + async move { + if i != 0 { + EX.spawn(async move { + let fut = go(i - 1).boxed(); + fut.await; + }) + .await; + } } } - } - run(|| { - b.iter(move || { - future::block_on(async { - let mut tasks = Vec::new(); - for _ in 0..TASKS { - tasks.push(EX.spawn(go(STEPS))); - } - for task in tasks { - task.await; - } + run(|| { + b.iter(move || { + future::block_on(async { + let mut tasks = Vec::new(); + for _ in 0..TASKS { + tasks.push(EX.spawn(go(STEPS))); + } + for task in tasks { + task.await; + } + }); }); }); }); } -#[bench] -fn yield_now(b: &mut test::Bencher) { - run(|| { - b.iter(move || { - future::block_on(async { - let mut tasks = Vec::new(); - for _ in 0..TASKS { - tasks.push(EX.spawn(async move { - for _ in 0..STEPS { - future::yield_now().await; - } - })); - } - for task in tasks { - task.await; - } +fn yield_now(c: &mut Criterion) { + c.bench_function("executor::yield_now", |b| { + run(|| { + b.iter(move || { + future::block_on(async { + let mut tasks = Vec::new(); + for _ in 0..TASKS { + tasks.push(EX.spawn(async move { + for _ in 0..STEPS { + future::yield_now().await; + } + })); + } + for task in tasks { + task.await; + } + }); }); }); }); } + +criterion_group!( + benches, + create, + spawn_one, + spawn_many, + spawn_recursively, + yield_now, +); + +criterion_main!(benches);