Skip to content

Commit a95897c

Browse files
committed
Fix benches: actually poll futures in benches
1 parent 0c58293 commit a95897c

File tree

2 files changed

+108
-92
lines changed

2 files changed

+108
-92
lines changed

benches/future.rs

+42-36
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ use criterion::*;
22
use futures::executor;
33

44
fn bench_ready(c: &mut Criterion) {
5-
executor::block_on(async {
6-
let mut group = c.benchmark_group("future::ready");
5+
let mut group = c.benchmark_group("future::ready");
76

8-
group.bench_function("futures", |b| {
9-
b.iter(move || async {
10-
black_box(futures::future::ready(42)).await
7+
group.bench_function("futures", |b| {
8+
b.iter(|| {
9+
executor::block_on(async {
10+
black_box(futures::future::ready(42).await);
1111
})
12-
});
13-
group.bench_function("async_combinators", |b| {
14-
b.iter(move || async {
15-
black_box(futures_async_combinators::future::ready(42)).await
12+
})
13+
});
14+
group.bench_function("async_combinators", |b| {
15+
b.iter(|| {
16+
executor::block_on(async {
17+
black_box(futures_async_combinators::future::ready(42).await);
1618
})
17-
});
18-
19-
group.finish();
19+
})
2020
});
21+
22+
group.finish();
2123
}
2224

2325
fn bench_poll_fn(c: &mut Criterion) {
@@ -27,47 +29,51 @@ fn bench_poll_fn(c: &mut Criterion) {
2729
Poll::Ready(42)
2830
}
2931

30-
executor::block_on(async {
31-
let mut group = c.benchmark_group("future::poll_fn");
32+
let mut group = c.benchmark_group("future::poll_fn");
3233

33-
group.bench_function("futures", |b| {
34-
b.iter(move || async {
35-
black_box(futures::future::poll_fn(ret_42)).await
34+
group.bench_function("futures", |b| {
35+
b.iter(|| {
36+
executor::block_on(async {
37+
black_box(futures::future::poll_fn(ret_42).await);
3638
})
37-
});
38-
group.bench_function("async_combinators", |b| {
39-
b.iter(move || async {
40-
black_box(futures_async_combinators::future::poll_fn(ret_42)).await
39+
})
40+
});
41+
group.bench_function("async_combinators", |b| {
42+
b.iter(|| {
43+
executor::block_on(async {
44+
black_box(futures_async_combinators::future::poll_fn(ret_42)).await;
4145
})
42-
});
43-
44-
group.finish();
46+
})
4547
});
48+
49+
group.finish();
4650
}
4751

4852
fn bench_map(c: &mut Criterion) {
49-
executor::block_on(async {
50-
let mut group = c.benchmark_group("future::map");
53+
let mut group = c.benchmark_group("future::map");
5154

52-
group.bench_function("futures", |b| {
53-
b.iter(move || async {
55+
group.bench_function("futures", |b| {
56+
b.iter(|| {
57+
executor::block_on(async {
5458
use futures::future::*;
5559
let fut = ready(40);
5660
let fut = fut.map(|x| x + 2);
57-
black_box(fut).await
61+
black_box(fut.await);
5862
})
59-
});
60-
group.bench_function("async_combinators", |b| {
61-
b.iter(move || async {
63+
})
64+
});
65+
group.bench_function("async_combinators", |b| {
66+
b.iter(|| {
67+
executor::block_on(async {
6268
use futures_async_combinators::future::*;
6369
let fut = ready(40);
6470
let fut = map(fut, |x| x + 2);
65-
black_box(fut).await
71+
black_box(fut.await);
6672
})
67-
});
68-
69-
group.finish();
73+
})
7074
});
75+
76+
group.finish();
7177
}
7278

7379
criterion_group!(benches, bench_ready, bench_poll_fn, bench_map);

benches/stream.rs

+66-56
Original file line numberDiff line numberDiff line change
@@ -2,141 +2,151 @@ use criterion::*;
22
use futures::executor;
33

44
fn bench_stream_iter(c: &mut Criterion) {
5-
executor::block_on(async {
6-
let mut group = c.benchmark_group("stream::iter");
5+
let mut group = c.benchmark_group("stream::iter");
76

8-
group.bench_function("futures", |b| {
9-
b.iter(move || async {
7+
group.bench_function("futures", |b| {
8+
b.iter(|| {
9+
executor::block_on(async {
1010
use futures::stream::{iter, StreamExt};
1111
let mut stream = iter(1..=1000);
1212
while let Some(item) = stream.next().await {
1313
black_box(item);
1414
}
1515
})
16-
});
17-
group.bench_function("async_combinators", |b| {
18-
b.iter(move || async {
16+
})
17+
});
18+
group.bench_function("async_combinators", |b| {
19+
b.iter(|| {
20+
executor::block_on(async {
1921
use futures::stream::StreamExt;
2022
use futures_async_combinators::stream::iter;
2123
let mut stream = iter(1..=1000);
2224
while let Some(item) = stream.next().await {
2325
black_box(item);
2426
}
2527
})
26-
});
27-
28-
group.finish();
28+
})
2929
});
30+
31+
group.finish();
3032
}
3133

3234
fn bench_stream_next(c: &mut Criterion) {
33-
executor::block_on(async {
34-
let mut group = c.benchmark_group("stream::next");
35+
let mut group = c.benchmark_group("stream::next");
3536

36-
group.bench_function("futures", |b| {
37-
b.iter(move || async {
37+
group.bench_function("futures", |b| {
38+
b.iter(|| {
39+
executor::block_on(async {
3840
use futures::stream::{iter, StreamExt};
3941
let mut stream = iter(1..=1000);
4042
while let Some(item) = stream.next().await {
4143
black_box(item);
4244
}
4345
})
44-
});
45-
group.bench_function("async_combinators", |b| {
46-
b.iter(move || async {
46+
})
47+
});
48+
group.bench_function("async_combinators", |b| {
49+
b.iter(|| {
50+
executor::block_on(async {
4751
use futures::stream::iter;
4852
use futures_async_combinators::stream::next;
4953
let mut stream = iter(1..=1000);
5054
while let Some(item) = next(&mut stream).await {
5155
black_box(item);
5256
}
5357
})
54-
});
55-
56-
group.finish();
58+
})
5759
});
60+
61+
group.finish();
5862
}
5963

6064
fn bench_stream_collect(c: &mut Criterion) {
61-
executor::block_on(async {
62-
let mut group = c.benchmark_group("stream::collect");
65+
let mut group = c.benchmark_group("stream::collect");
6366

64-
group.bench_function("futures", |b| {
65-
b.iter(move || async {
67+
group.bench_function("futures", |b| {
68+
b.iter(|| {
69+
executor::block_on(async {
6670
use futures::stream::{iter, StreamExt};
6771
let stream = iter(1..=1000);
6872
let vec: Vec<_> = stream.collect().await;
69-
black_box(vec)
73+
black_box(vec);
7074
})
71-
});
72-
group.bench_function("async_combinators", |b| {
73-
b.iter(move || async {
75+
})
76+
});
77+
group.bench_function("async_combinators", |b| {
78+
b.iter(|| {
79+
executor::block_on(async {
7480
use futures::stream::iter;
7581
use futures_async_combinators::stream::collect;
7682
let stream = iter(1..=1000);
7783
let vec: Vec<_> = collect(stream).await;
78-
black_box(vec)
84+
black_box(vec);
7985
})
80-
});
81-
82-
group.finish();
86+
})
8387
});
88+
89+
group.finish();
8490
}
8591

8692
fn bench_stream_map(c: &mut Criterion) {
87-
executor::block_on(async {
88-
let mut group = c.benchmark_group("stream::map");
93+
let mut group = c.benchmark_group("stream::map");
8994

90-
group.bench_function("futures", |b| {
91-
b.iter(move || async {
95+
group.bench_function("futures", |b| {
96+
b.iter(|| {
97+
executor::block_on(async {
9298
use futures::stream::{iter, StreamExt};
9399
let stream = iter(1..=1000);
94100
let stream = stream.map(|x| x + 42);
95101
let vec: Vec<_> = stream.collect().await;
96-
black_box(vec)
102+
black_box(vec);
97103
})
98-
});
99-
group.bench_function("async_combinators", |b| {
100-
b.iter(move || async {
104+
})
105+
});
106+
group.bench_function("async_combinators", |b| {
107+
b.iter(|| {
108+
executor::block_on(async {
101109
use futures::stream::{iter, StreamExt};
102110
use futures_async_combinators::stream::map;
103111
let stream = iter(1..=1000);
104112
let stream = map(stream, |x| x + 42);
105113
let vec: Vec<_> = stream.collect().await;
106-
black_box(vec)
114+
black_box(vec);
107115
})
108-
});
109-
110-
group.finish();
116+
})
111117
});
118+
119+
group.finish();
112120
}
113121

114122
fn bench_stream_fold(c: &mut Criterion) {
115-
executor::block_on(async {
116-
let mut group = c.benchmark_group("stream::fold");
123+
let mut group = c.benchmark_group("stream::fold");
117124

118-
group.bench_function("futures", |b| {
119-
b.iter(move || async {
125+
group.bench_function("futures", |b| {
126+
b.iter(|| {
127+
executor::block_on(async {
120128
use futures::stream::{iter, StreamExt};
121129
use futures_async_combinators::future::ready;
122130
let stream = iter(1..=1000);
123131
let acc = stream.fold(0, |acc, x| ready(acc + x));
124-
black_box(acc).await
132+
black_box(acc).await;
125133
})
126-
});
127-
group.bench_function("async_combinators", |b| {
128-
b.iter(move || async {
134+
})
135+
});
136+
group.bench_function("async_combinators", |b| {
137+
b.iter(|| {
138+
executor::block_on(async {
129139
use futures::stream::iter;
130140
use futures_async_combinators::future::ready;
131141
use futures_async_combinators::stream::fold;
132142
let stream = iter(1..=1000);
133143
let acc = fold(stream, 0, |acc, x| ready(acc + x));
134-
black_box(acc).await
144+
black_box(acc).await;
135145
})
136-
});
137-
138-
group.finish();
146+
})
139147
});
148+
149+
group.finish();
140150
}
141151

142152
criterion_group!(

0 commit comments

Comments
 (0)