Skip to content

Commit 2fcbbde

Browse files
author
Stjepan Glavina
committed
Add benchmarks
1 parent 98aac61 commit 2fcbbde

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ vec-arena = "1.0.0"
2222

2323
[dev-dependencies]
2424
async-channel = "1.4.1"
25-
async-io = "0.2.0"
25+
async-io = "1.1.9"
2626
easy-parallel = "3.1.0"
27+
num_cpus = "1.13.0"

benches/executor.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#![feature(test)]
2+
3+
extern crate test;
4+
5+
use std::future::Future;
6+
7+
use async_executor::Executor;
8+
use futures_lite::future::{self, FutureExt};
9+
10+
const TASKS: usize = 300;
11+
const STEPS: usize = 300;
12+
const LIGHT_TASKS: usize = 25_000;
13+
14+
static EX: Executor<'_> = Executor::new();
15+
16+
fn run(f: impl FnOnce()) {
17+
let (s, r) = async_channel::bounded::<()>(1);
18+
easy_parallel::Parallel::new()
19+
.each(0..num_cpus::get(), |_| future::block_on(EX.run(r.recv())))
20+
.finish(move || {
21+
let _s = s;
22+
f()
23+
});
24+
}
25+
26+
#[bench]
27+
fn create(b: &mut test::Bencher) {
28+
b.iter(move || {
29+
let ex = Executor::new();
30+
let task = ex.spawn(async {});
31+
future::block_on(ex.run(task));
32+
});
33+
}
34+
35+
#[bench]
36+
fn spawn_one(b: &mut test::Bencher) {
37+
run(|| {
38+
b.iter(move || {
39+
future::block_on(async { EX.spawn(async {}).await });
40+
});
41+
});
42+
}
43+
44+
#[bench]
45+
fn spawn_many(b: &mut test::Bencher) {
46+
run(|| {
47+
b.iter(move || {
48+
future::block_on(async {
49+
let mut tasks = Vec::new();
50+
for _ in 0..LIGHT_TASKS {
51+
tasks.push(EX.spawn(async {}));
52+
}
53+
for task in tasks {
54+
task.await;
55+
}
56+
});
57+
});
58+
});
59+
}
60+
61+
#[bench]
62+
fn spawn_recursively(b: &mut test::Bencher) {
63+
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
64+
async move {
65+
if i != 0 {
66+
EX.spawn(async move {
67+
let fut = go(i - 1).boxed();
68+
fut.await;
69+
})
70+
.await;
71+
}
72+
}
73+
}
74+
75+
run(|| {
76+
b.iter(move || {
77+
future::block_on(async {
78+
let mut tasks = Vec::new();
79+
for _ in 0..TASKS {
80+
tasks.push(EX.spawn(go(STEPS)));
81+
}
82+
for task in tasks {
83+
task.await;
84+
}
85+
});
86+
});
87+
});
88+
}
89+
90+
#[bench]
91+
fn yield_now(b: &mut test::Bencher) {
92+
run(|| {
93+
b.iter(move || {
94+
future::block_on(async {
95+
let mut tasks = Vec::new();
96+
for _ in 0..TASKS {
97+
tasks.push(EX.spawn(async move {
98+
for _ in 0..STEPS {
99+
future::yield_now().await;
100+
}
101+
}));
102+
}
103+
for task in tasks {
104+
task.await;
105+
}
106+
});
107+
});
108+
});
109+
}

0 commit comments

Comments
 (0)