m: Migrate benchmarks to criterion (#35)

* m: Migrate to criterion

* Update CI
This commit is contained in:
John Nunley 2023-01-23 19:30:43 +00:00 committed by GitHub
parent f196463b09
commit a988ee3e46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 69 deletions

View File

@ -59,7 +59,7 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Install Rust - name: Install Rust
run: rustup update stable run: rustup update stable
- run: cargo clippy --all-features --tests --examples - run: cargo clippy --all-features --all-targets
fmt: fmt:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -25,6 +25,11 @@ slab = "0.4.4"
[dev-dependencies] [dev-dependencies]
async-channel = "1.4.1" async-channel = "1.4.1"
async-io = "1.1.9" async-io = "1.1.9"
criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] }
easy-parallel = "3.1.0" easy-parallel = "3.1.0"
num_cpus = "1.13.0" num_cpus = "1.13.0"
once_cell = "1.16.0" once_cell = "1.16.0"
[[bench]]
name = "executor"
harness = false

View File

@ -1,10 +1,7 @@
#![feature(test)]
extern crate test;
use std::future::Future; use std::future::Future;
use async_executor::Executor; use async_executor::Executor;
use criterion::{criterion_group, criterion_main, Criterion};
use futures_lite::{future, prelude::*}; use futures_lite::{future, prelude::*};
const TASKS: usize = 300; const TASKS: usize = 300;
@ -23,87 +20,104 @@ fn run(f: impl FnOnce()) {
}); });
} }
#[bench] fn create(c: &mut Criterion) {
fn create(b: &mut test::Bencher) { c.bench_function("executor::create", |b| {
b.iter(move || { b.iter(|| {
let ex = Executor::new(); let ex = Executor::new();
let task = ex.spawn(async {}); let task = ex.spawn(async {});
future::block_on(ex.run(task)); future::block_on(ex.run(task));
})
}); });
} }
#[bench] fn spawn_one(c: &mut Criterion) {
fn spawn_one(b: &mut test::Bencher) { c.bench_function("executor::spawn_one", |b| {
run(|| { run(|| {
b.iter(move || { b.iter(|| {
future::block_on(async { EX.spawn(async {}).await }); 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;
}
}); });
}); });
}); });
} }
#[bench] fn spawn_many(c: &mut Criterion) {
fn spawn_recursively(b: &mut test::Bencher) { c.bench_function("executor::spawn_many_local", |b| {
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static { run(|| {
async move { b.iter(move || {
if i != 0 { future::block_on(async {
EX.spawn(async move { let mut tasks = Vec::new();
let fut = go(i - 1).boxed(); for _ in 0..LIGHT_TASKS {
fut.await; tasks.push(EX.spawn(async {}));
}) }
.await; 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<Output = ()> + Send + 'static {
async move {
if i != 0 {
EX.spawn(async move {
let fut = go(i - 1).boxed();
fut.await;
})
.await;
}
} }
} }
}
run(|| { run(|| {
b.iter(move || { b.iter(move || {
future::block_on(async { future::block_on(async {
let mut tasks = Vec::new(); let mut tasks = Vec::new();
for _ in 0..TASKS { for _ in 0..TASKS {
tasks.push(EX.spawn(go(STEPS))); tasks.push(EX.spawn(go(STEPS)));
} }
for task in tasks { for task in tasks {
task.await; task.await;
} }
});
}); });
}); });
}); });
} }
#[bench] fn yield_now(c: &mut Criterion) {
fn yield_now(b: &mut test::Bencher) { c.bench_function("executor::yield_now", |b| {
run(|| { run(|| {
b.iter(move || { b.iter(move || {
future::block_on(async { future::block_on(async {
let mut tasks = Vec::new(); let mut tasks = Vec::new();
for _ in 0..TASKS { for _ in 0..TASKS {
tasks.push(EX.spawn(async move { tasks.push(EX.spawn(async move {
for _ in 0..STEPS { for _ in 0..STEPS {
future::yield_now().await; future::yield_now().await;
} }
})); }));
} }
for task in tasks { for task in tasks {
task.await; task.await;
} }
});
}); });
}); });
}); });
} }
criterion_group!(
benches,
create,
spawn_one,
spawn_many,
spawn_recursively,
yield_now,
);
criterion_main!(benches);