Skip to content

Commit 9021774

Browse files
committed
Fix benches: actually poll futures in benches
1 parent 165d1d6 commit 9021774

File tree

2 files changed

+124
-108
lines changed

2 files changed

+124
-108
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

+82-72
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 {
65
let mut group = c.benchmark_group("stream::iter");
76

87
group.bench_function("futures", |b| {
9-
b.iter(move || async {
10-
use futures::stream::{iter, StreamExt};
11-
let mut stream = iter(1..=1000);
12-
while let Some(item) = stream.next().await {
13-
black_box(item);
14-
}
8+
b.iter(|| {
9+
executor::block_on(async {
10+
use futures::stream::{iter, StreamExt};
11+
let mut stream = iter(1..=1000);
12+
while let Some(item) = stream.next().await {
13+
black_box(item);
14+
}
15+
})
1516
})
1617
});
1718
group.bench_function("async_combinators", |b| {
18-
b.iter(move || async {
19-
use futures::stream::StreamExt;
20-
use futures_async_combinators::stream::iter;
21-
let mut stream = iter(1..=1000);
22-
while let Some(item) = stream.next().await {
23-
black_box(item);
24-
}
19+
b.iter(|| {
20+
executor::block_on(async {
21+
use futures::stream::StreamExt;
22+
use futures_async_combinators::stream::iter;
23+
let mut stream = iter(1..=1000);
24+
while let Some(item) = stream.next().await {
25+
black_box(item);
26+
}
27+
})
2528
})
2629
});
2730

2831
group.finish();
29-
});
3032
}
3133

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

3637
group.bench_function("futures", |b| {
37-
b.iter(move || async {
38-
use futures::stream::{iter, StreamExt};
39-
let mut stream = iter(1..=1000);
40-
while let Some(item) = stream.next().await {
41-
black_box(item);
42-
}
38+
b.iter(|| {
39+
executor::block_on(async {
40+
use futures::stream::{iter, StreamExt};
41+
let mut stream = iter(1..=1000);
42+
while let Some(item) = stream.next().await {
43+
black_box(item);
44+
}
45+
})
4346
})
4447
});
4548
group.bench_function("async_combinators", |b| {
46-
b.iter(move || async {
47-
use futures::stream::iter;
48-
use futures_async_combinators::stream::next;
49-
let mut stream = iter(1..=1000);
50-
while let Some(item) = next(&mut stream).await {
51-
black_box(item);
52-
}
49+
b.iter(|| {
50+
executor::block_on(async {
51+
use futures::stream::iter;
52+
use futures_async_combinators::stream::next;
53+
let mut stream = iter(1..=1000);
54+
while let Some(item) = next(&mut stream).await {
55+
black_box(item);
56+
}
57+
})
5358
})
5459
});
5560

5661
group.finish();
57-
});
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 {
8893
let mut group = c.benchmark_group("stream::map");
8994

9095
group.bench_function("futures", |b| {
91-
b.iter(move || async {
92-
use futures::stream::{iter, StreamExt};
93-
let stream = iter(1..=1000);
94-
let stream = stream.map(|x| x + 42);
95-
let vec: Vec<_> = stream.collect().await;
96-
black_box(vec)
96+
b.iter(|| {
97+
executor::block_on(async {
98+
use futures::stream::{iter, StreamExt};
99+
let stream = iter(1..=1000);
100+
let stream = stream.map(|x| x + 42);
101+
let vec: Vec<_> = stream.collect().await;
102+
black_box(vec);
103+
})
97104
})
98105
});
99106
group.bench_function("async_combinators", |b| {
100-
b.iter(move || async {
101-
use futures::stream::{iter, StreamExt};
102-
use futures_async_combinators::stream::map;
103-
let stream = iter(1..=1000);
104-
let stream = map(stream, |x| x + 42);
105-
let vec: Vec<_> = stream.collect().await;
106-
black_box(vec)
107+
b.iter(|| {
108+
executor::block_on(async {
109+
use futures::stream::{iter, StreamExt};
110+
use futures_async_combinators::stream::map;
111+
let stream = iter(1..=1000);
112+
let stream = map(stream, |x| x + 42);
113+
let vec: Vec<_> = stream.collect().await;
114+
black_box(vec);
115+
})
107116
})
108117
});
109118

110119
group.finish();
111-
});
112120
}
113121

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

118125
group.bench_function("futures", |b| {
119-
b.iter(move || async {
120-
use futures::stream::{iter, StreamExt};
121-
use futures_async_combinators::future::ready;
122-
let stream = iter(1..=1000);
123-
let acc = stream.fold(0, |acc, x| ready(acc + x));
124-
black_box(acc).await
126+
b.iter(|| {
127+
executor::block_on(async {
128+
use futures::stream::{iter, StreamExt};
129+
use futures_async_combinators::future::ready;
130+
let stream = iter(1..=1000);
131+
let acc = stream.fold(0, |acc, x| ready(acc + x));
132+
black_box(acc).await;
133+
})
125134
})
126135
});
127136
group.bench_function("async_combinators", |b| {
128-
b.iter(move || async {
129-
use futures::stream::iter;
130-
use futures_async_combinators::future::ready;
131-
use futures_async_combinators::stream::fold;
132-
let stream = iter(1..=1000);
133-
let acc = fold(stream, 0, |acc, x| ready(acc + x));
134-
black_box(acc).await
137+
b.iter(|| {
138+
executor::block_on(async {
139+
use futures::stream::iter;
140+
use futures_async_combinators::future::ready;
141+
use futures_async_combinators::stream::fold;
142+
let stream = iter(1..=1000);
143+
let acc = fold(stream, 0, |acc, x| ready(acc + x));
144+
black_box(acc).await;
145+
})
135146
})
136147
});
137148

138149
group.finish();
139-
});
140150
}
141151

142152
criterion_group!(

0 commit comments

Comments
 (0)