Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs and examples for async functions #583

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions book/src/user_guide/benchmarking_async.md
Expand Up @@ -52,6 +52,28 @@ request.
| futures | "async_futures" | `FuturesExecutor` |
| Other | "async" | |

### An Example with tokio::runtime::Runtime

```rust

// Compile Criterion.rs with features = ["async_tokio"]
#[macro_use] extern crate criterion;

use criterion::*;

fn bench(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
c.bench_function("tokio_thread_sleep", move |b| {
b.to_async(&rt).iter(|| async move {
tokio::time::sleep(Duration::from_millis(10)).await;
})
});
}

criterion_group!(benches, bench);
criterion_main!(benches);
```

### Considerations when benchmarking async functions

Async functions naturally result in more measurement overhead than synchronous functions. It is
Expand Down
22 changes: 22 additions & 0 deletions src/bencher.rs
Expand Up @@ -388,6 +388,28 @@ impl<'a, M: Measurement> Bencher<'a, M> {
}

/// Convert this bencher into an AsyncBencher, which enables async/await support.
///
/// # Example
///
/// ```rust
/// // Compile Criterion.rs with features = ["async_tokio"]
/// #[macro_use] extern crate criterion;
///
/// use criterion::*;
///
/// fn bench(c: &mut Criterion) {
/// let rt = tokio::runtime::Runtime::new().unwrap();
/// c.bench_function("tokio_thread_sleep", move |b| {
/// b.to_async(&rt).iter(|| async move {
/// tokio::time::sleep(Duration::from_millis(10)).await;
/// })
/// });
/// }
///
/// criterion_group!(benches, bench);
/// criterion_main!(benches);
/// ```
///
#[cfg(feature = "async")]
pub fn to_async<'b, A: AsyncExecutor>(&'b mut self, runner: A) -> AsyncBencher<'a, 'b, A, M> {
AsyncBencher { b: self, runner }
Expand Down