From a95897c41904105e2107f587926f76c208b3111c Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Sun, 29 Mar 2020 02:04:10 +0300 Subject: [PATCH] Fix benches: actually poll futures in benches --- benches/future.rs | 78 +++++++++++++++-------------- benches/stream.rs | 122 +++++++++++++++++++++++++--------------------- 2 files changed, 108 insertions(+), 92 deletions(-) diff --git a/benches/future.rs b/benches/future.rs index 187386e..038af4d 100644 --- a/benches/future.rs +++ b/benches/future.rs @@ -2,22 +2,24 @@ use criterion::*; use futures::executor; fn bench_ready(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("future::ready"); + let mut group = c.benchmark_group("future::ready"); - group.bench_function("futures", |b| { - b.iter(move || async { - black_box(futures::future::ready(42)).await + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { + black_box(futures::future::ready(42).await); }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { - black_box(futures_async_combinators::future::ready(42)).await + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { + black_box(futures_async_combinators::future::ready(42).await); }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_poll_fn(c: &mut Criterion) { @@ -27,47 +29,51 @@ fn bench_poll_fn(c: &mut Criterion) { Poll::Ready(42) } - executor::block_on(async { - let mut group = c.benchmark_group("future::poll_fn"); + let mut group = c.benchmark_group("future::poll_fn"); - group.bench_function("futures", |b| { - b.iter(move || async { - black_box(futures::future::poll_fn(ret_42)).await + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { + black_box(futures::future::poll_fn(ret_42).await); }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { - black_box(futures_async_combinators::future::poll_fn(ret_42)).await + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { + black_box(futures_async_combinators::future::poll_fn(ret_42)).await; }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_map(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("future::map"); + let mut group = c.benchmark_group("future::map"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::future::*; let fut = ready(40); let fut = fut.map(|x| x + 2); - black_box(fut).await + black_box(fut.await); }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures_async_combinators::future::*; let fut = ready(40); let fut = map(fut, |x| x + 2); - black_box(fut).await + black_box(fut.await); }) - }); - - group.finish(); + }) }); + + group.finish(); } criterion_group!(benches, bench_ready, bench_poll_fn, bench_map); diff --git a/benches/stream.rs b/benches/stream.rs index 17af7e6..482c501 100644 --- a/benches/stream.rs +++ b/benches/stream.rs @@ -2,20 +2,22 @@ use criterion::*; use futures::executor; fn bench_stream_iter(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("stream::iter"); + let mut group = c.benchmark_group("stream::iter"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; let mut stream = iter(1..=1000); while let Some(item) = stream.next().await { black_box(item); } }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::StreamExt; use futures_async_combinators::stream::iter; let mut stream = iter(1..=1000); @@ -23,27 +25,29 @@ fn bench_stream_iter(c: &mut Criterion) { black_box(item); } }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_stream_next(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("stream::next"); + let mut group = c.benchmark_group("stream::next"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; let mut stream = iter(1..=1000); while let Some(item) = stream.next().await { black_box(item); } }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::iter; use futures_async_combinators::stream::next; let mut stream = iter(1..=1000); @@ -51,92 +55,98 @@ fn bench_stream_next(c: &mut Criterion) { black_box(item); } }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_stream_collect(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("stream::collect"); + let mut group = c.benchmark_group("stream::collect"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; let stream = iter(1..=1000); let vec: Vec<_> = stream.collect().await; - black_box(vec) + black_box(vec); }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::iter; use futures_async_combinators::stream::collect; let stream = iter(1..=1000); let vec: Vec<_> = collect(stream).await; - black_box(vec) + black_box(vec); }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_stream_map(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("stream::map"); + let mut group = c.benchmark_group("stream::map"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; let stream = iter(1..=1000); let stream = stream.map(|x| x + 42); let vec: Vec<_> = stream.collect().await; - black_box(vec) + black_box(vec); }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; use futures_async_combinators::stream::map; let stream = iter(1..=1000); let stream = map(stream, |x| x + 42); let vec: Vec<_> = stream.collect().await; - black_box(vec) + black_box(vec); }) - }); - - group.finish(); + }) }); + + group.finish(); } fn bench_stream_fold(c: &mut Criterion) { - executor::block_on(async { - let mut group = c.benchmark_group("stream::fold"); + let mut group = c.benchmark_group("stream::fold"); - group.bench_function("futures", |b| { - b.iter(move || async { + group.bench_function("futures", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::{iter, StreamExt}; use futures_async_combinators::future::ready; let stream = iter(1..=1000); let acc = stream.fold(0, |acc, x| ready(acc + x)); - black_box(acc).await + black_box(acc).await; }) - }); - group.bench_function("async_combinators", |b| { - b.iter(move || async { + }) + }); + group.bench_function("async_combinators", |b| { + b.iter(|| { + executor::block_on(async { use futures::stream::iter; use futures_async_combinators::future::ready; use futures_async_combinators::stream::fold; let stream = iter(1..=1000); let acc = fold(stream, 0, |acc, x| ready(acc + x)); - black_box(acc).await + black_box(acc).await; }) - }); - - group.finish(); + }) }); + + group.finish(); } criterion_group!(