Skip to content

Commit 6aba704

Browse files
authored
bench: Add benchmarks for lower thread counts (#38)
1 parent b8885f9 commit 6aba704

File tree

1 file changed

+83
-78
lines changed

1 file changed

+83
-78
lines changed

benches/executor.rs

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const LIGHT_TASKS: usize = 25_000;
1010

1111
static EX: Executor<'_> = Executor::new();
1212

13-
fn run(f: impl FnOnce()) {
13+
fn run(f: impl FnOnce(), multithread: bool) {
14+
let limit = if multithread { num_cpus::get() } else { 1 };
15+
1416
let (s, r) = async_channel::bounded::<()>(1);
1517
easy_parallel::Parallel::new()
16-
.each(0..num_cpus::get(), |_| future::block_on(EX.run(r.recv())))
18+
.each(0..limit, |_| future::block_on(EX.run(r.recv())))
1719
.finish(move || {
1820
let _s = s;
1921
f()
@@ -30,94 +32,97 @@ fn create(c: &mut Criterion) {
3032
});
3133
}
3234

33-
fn spawn_one(c: &mut Criterion) {
34-
c.bench_function("executor::spawn_one", |b| {
35-
run(|| {
36-
b.iter(|| {
37-
future::block_on(async { EX.spawn(async {}).await });
38-
});
35+
fn running_benches(c: &mut Criterion) {
36+
for (group_name, multithread) in [("single_thread", false), ("multi_thread", true)].iter() {
37+
let mut group = c.benchmark_group(group_name.to_string());
38+
39+
group.bench_function("executor::spawn_one", |b| {
40+
run(
41+
|| {
42+
b.iter(|| {
43+
future::block_on(async { EX.spawn(async {}).await });
44+
});
45+
},
46+
*multithread,
47+
);
3948
});
40-
});
41-
}
4249

43-
fn spawn_many(c: &mut Criterion) {
44-
c.bench_function("executor::spawn_many_local", |b| {
45-
run(|| {
46-
b.iter(move || {
47-
future::block_on(async {
48-
let mut tasks = Vec::new();
49-
for _ in 0..LIGHT_TASKS {
50-
tasks.push(EX.spawn(async {}));
51-
}
52-
for task in tasks {
53-
task.await;
54-
}
55-
});
56-
});
50+
group.bench_function("executor::spawn_many_local", |b| {
51+
run(
52+
|| {
53+
b.iter(move || {
54+
future::block_on(async {
55+
let mut tasks = Vec::new();
56+
for _ in 0..LIGHT_TASKS {
57+
tasks.push(EX.spawn(async {}));
58+
}
59+
for task in tasks {
60+
task.await;
61+
}
62+
});
63+
});
64+
},
65+
*multithread,
66+
);
5767
});
58-
});
59-
}
6068

61-
fn spawn_recursively(c: &mut Criterion) {
62-
c.bench_function("executor::spawn_recursively", |b| {
63-
#[allow(clippy::manual_async_fn)]
64-
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
65-
async move {
66-
if i != 0 {
67-
EX.spawn(async move {
68-
let fut = go(i - 1).boxed();
69-
fut.await;
70-
})
71-
.await;
69+
group.bench_function("executor::spawn_recursively", |b| {
70+
#[allow(clippy::manual_async_fn)]
71+
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
72+
async move {
73+
if i != 0 {
74+
EX.spawn(async move {
75+
let fut = go(i - 1).boxed();
76+
fut.await;
77+
})
78+
.await;
79+
}
7280
}
7381
}
74-
}
7582

76-
run(|| {
77-
b.iter(move || {
78-
future::block_on(async {
79-
let mut tasks = Vec::new();
80-
for _ in 0..TASKS {
81-
tasks.push(EX.spawn(go(STEPS)));
82-
}
83-
for task in tasks {
84-
task.await;
85-
}
86-
});
87-
});
83+
run(
84+
|| {
85+
b.iter(move || {
86+
future::block_on(async {
87+
let mut tasks = Vec::new();
88+
for _ in 0..TASKS {
89+
tasks.push(EX.spawn(go(STEPS)));
90+
}
91+
for task in tasks {
92+
task.await;
93+
}
94+
});
95+
});
96+
},
97+
*multithread,
98+
);
8899
});
89-
});
90-
}
91100

92-
fn yield_now(c: &mut Criterion) {
93-
c.bench_function("executor::yield_now", |b| {
94-
run(|| {
95-
b.iter(move || {
96-
future::block_on(async {
97-
let mut tasks = Vec::new();
98-
for _ in 0..TASKS {
99-
tasks.push(EX.spawn(async move {
100-
for _ in 0..STEPS {
101-
future::yield_now().await;
101+
group.bench_function("executor::yield_now", |b| {
102+
run(
103+
|| {
104+
b.iter(move || {
105+
future::block_on(async {
106+
let mut tasks = Vec::new();
107+
for _ in 0..TASKS {
108+
tasks.push(EX.spawn(async move {
109+
for _ in 0..STEPS {
110+
future::yield_now().await;
111+
}
112+
}));
102113
}
103-
}));
104-
}
105-
for task in tasks {
106-
task.await;
107-
}
108-
});
109-
});
114+
for task in tasks {
115+
task.await;
116+
}
117+
});
118+
});
119+
},
120+
*multithread,
121+
);
110122
});
111-
});
123+
}
112124
}
113125

114-
criterion_group!(
115-
benches,
116-
create,
117-
spawn_one,
118-
spawn_many,
119-
spawn_recursively,
120-
yield_now,
121-
);
126+
criterion_group!(benches, create, running_benches);
122127

123128
criterion_main!(benches);

0 commit comments

Comments
 (0)