Skip to content

Commit 80ac73f

Browse files
authored
Merge pull request #3 from kpp/benchmarks
Add simple benchmarks
2 parents 6a93397 + 444a44d commit 80ac73f

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,20 @@ pin-utils = "=0.1.0-alpha.4"
2323
[dependencies.futures]
2424
version = "=0.3.0-alpha.18"
2525
package = "futures-preview"
26+
27+
[dev-dependencies]
28+
criterion = "0.2"
29+
30+
[[bench]]
31+
name = "stream"
32+
harness = false
33+
34+
[[bench]]
35+
name = "future"
36+
harness = false
37+
38+
[profile.bench]
39+
opt-level = 3
40+
debug = false
41+
lto = true
42+
debug-assertions = false

benches/future.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#![feature(async_closure)]
2+
3+
#[macro_use]
4+
extern crate criterion;
5+
use criterion::{black_box, Benchmark, Criterion};
6+
7+
use futures::executor;
8+
9+
fn bench_ready(c: &mut Criterion) {
10+
executor::block_on(async {
11+
c.bench(
12+
"future::ready",
13+
Benchmark::new("futures", |b| {
14+
b.iter(async move || black_box(futures::future::ready(42)).await)
15+
})
16+
.with_function("async_combinators", |b| {
17+
b.iter(async move || black_box(futures_async_combinators::future::ready(42)).await)
18+
}),
19+
);
20+
});
21+
}
22+
23+
fn bench_poll_fn(c: &mut Criterion) {
24+
use core::task::{Context, Poll};
25+
26+
fn ret_42(_cx: &mut Context<'_>) -> Poll<i32> {
27+
Poll::Ready(42)
28+
}
29+
30+
executor::block_on(async {
31+
c.bench(
32+
"future::poll_fn",
33+
Benchmark::new("futures", |b| {
34+
b.iter(async move || black_box(futures::future::poll_fn(ret_42)).await)
35+
})
36+
.with_function("async_combinators", |b| {
37+
b.iter(async move || {
38+
black_box(futures_async_combinators::future::poll_fn(ret_42)).await
39+
})
40+
}),
41+
);
42+
});
43+
}
44+
45+
fn bench_map(c: &mut Criterion) {
46+
executor::block_on(async {
47+
c.bench(
48+
"future::map",
49+
Benchmark::new("futures", |b| {
50+
b.iter(async move || {
51+
use futures::future::*;
52+
let fut = ready(40);
53+
let fut = fut.map(|x| x + 2);
54+
black_box(fut).await
55+
})
56+
})
57+
.with_function("async_combinators", |b| {
58+
b.iter(async move || {
59+
use futures_async_combinators::future::*;
60+
let fut = ready(40);
61+
let fut = map(fut, |x| x + 2);
62+
black_box(fut).await
63+
})
64+
}),
65+
);
66+
});
67+
}
68+
69+
criterion_group!(benches, bench_ready, bench_poll_fn, bench_map);
70+
criterion_main!(benches);

benches/stream.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#![feature(async_closure)]
2+
3+
#[macro_use]
4+
extern crate criterion;
5+
use criterion::{black_box, Benchmark, Criterion};
6+
7+
use futures::executor;
8+
9+
fn bench_stream_iter(c: &mut Criterion) {
10+
executor::block_on(async {
11+
c.bench(
12+
"stream::iter",
13+
Benchmark::new("futures", |b| {
14+
b.iter(async move || {
15+
use futures::stream::{iter, StreamExt};
16+
let mut stream = iter(1..=1000);
17+
while let Some(item) = stream.next().await {
18+
black_box(item);
19+
}
20+
})
21+
})
22+
.with_function("async_combinators", |b| {
23+
b.iter(async move || {
24+
use futures::stream::StreamExt;
25+
use futures_async_combinators::stream::iter;
26+
let mut stream = iter(1..=1000);
27+
while let Some(item) = stream.next().await {
28+
black_box(item);
29+
}
30+
})
31+
}),
32+
);
33+
});
34+
}
35+
36+
fn bench_stream_next(c: &mut Criterion) {
37+
executor::block_on(async {
38+
c.bench(
39+
"stream::next",
40+
Benchmark::new("futures", |b| {
41+
b.iter(async move || {
42+
use futures::stream::{iter, StreamExt};
43+
let mut stream = iter(1..=1000);
44+
while let Some(item) = stream.next().await {
45+
black_box(item);
46+
}
47+
})
48+
})
49+
.with_function("async_combinators", |b| {
50+
b.iter(async move || {
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+
})
58+
}),
59+
);
60+
});
61+
}
62+
63+
fn bench_stream_collect(c: &mut Criterion) {
64+
executor::block_on(async {
65+
c.bench(
66+
"stream::collect",
67+
Benchmark::new("futures", |b| {
68+
b.iter(async move || {
69+
use futures::stream::{iter, StreamExt};
70+
let stream = iter(1..=1000);
71+
let vec: Vec<_> = stream.collect().await;
72+
black_box(vec)
73+
})
74+
})
75+
.with_function("async_combinators", |b| {
76+
b.iter(async move || {
77+
use futures::stream::iter;
78+
use futures_async_combinators::stream::collect;
79+
let stream = iter(1..=1000);
80+
let vec: Vec<_> = collect(stream).await;
81+
black_box(vec)
82+
})
83+
}),
84+
);
85+
});
86+
}
87+
88+
fn bench_stream_map(c: &mut Criterion) {
89+
executor::block_on(async {
90+
c.bench(
91+
"stream::map",
92+
Benchmark::new("futures", |b| {
93+
b.iter(async move || {
94+
use futures::stream::{iter, StreamExt};
95+
let stream = iter(1..=1000);
96+
let stream = stream.map(|x| x + 42);
97+
let vec: Vec<_> = stream.collect().await;
98+
black_box(vec)
99+
})
100+
})
101+
.with_function("async_combinators", |b| {
102+
b.iter(async move || {
103+
use futures::stream::{iter, StreamExt};
104+
use futures_async_combinators::stream::map;
105+
let stream = iter(1..=1000);
106+
let stream = map(stream, |x| x + 42);
107+
let vec: Vec<_> = stream.collect().await;
108+
black_box(vec)
109+
})
110+
}),
111+
);
112+
});
113+
}
114+
115+
criterion_group!(
116+
benches,
117+
bench_stream_iter,
118+
bench_stream_next,
119+
bench_stream_collect,
120+
bench_stream_map,
121+
);
122+
criterion_main!(benches);

0 commit comments

Comments
 (0)