Skip to content

Commit 2d827f7

Browse files
committed
Add simple benchmarks
1 parent 6a93397 commit 2d827f7

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

Cargo.toml

Lines changed: 17 additions & 0 deletions
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

Lines changed: 70 additions & 0 deletions
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+
"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+
"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+
"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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"iter",
13+
Benchmark::new("futures", |b| {
14+
b.iter(async move || {
15+
use futures::stream::*;
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+
criterion_group!(benches, bench_stream_iter);
37+
criterion_main!(benches);

0 commit comments

Comments
 (0)