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

impl AsyncExecutor for tokio::runtime::Handle #712

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Async benchmarking with Tokio may be done via a `tokio::runtime::Handle`, not only a `tokio::runtime::Runtime`

## [0.5.1] - 2023-05-26

### Fixed
Expand Down
14 changes: 7 additions & 7 deletions book/src/user_guide/benchmarking_async.md
Expand Up @@ -44,13 +44,13 @@ the same executor that you would use in production. If your executor is not list
implement the `criterion::async_executor::AsyncExecutor` trait for it to add support, or send a pull
request.

| Crate | Feature | Executor Struct |
| --------- | ----------------------------- | ----------------------------------------------------- |
| Tokio | "async_tokio" | `tokio::runtime::Runtime`, `&tokio::runtime::Runtime` |
| async-std | "async_std" (note underscore) | `AsyncStdExecutor` |
| Smol | "async_smol" | `SmolExecutor` |
| futures | "async_futures" | `FuturesExecutor` |
| Other | "async" | |
| Crate | Feature | Executor Struct |
| --------- | ----------------------------- | ------------------------------------------------------------------ |
| Tokio | "async_tokio" | In `tokio::runtime`, `Runtime`, `&Runtime`, `Handle`, or `&Handle` |
| async-std | "async_std" (note underscore) | `AsyncStdExecutor` |
| Smol | "async_smol" | `SmolExecutor` |
| futures | "async_futures" | `FuturesExecutor` |
| Other | "async" | |

### Considerations when benchmarking async functions

Expand Down
14 changes: 13 additions & 1 deletion src/async_executor.rs
Expand Up @@ -2,7 +2,7 @@
//! Criterion.rs' async benchmarking support.
//!
//! Implementations are provided for:
//! * Tokio (implemented directly for `tokio::Runtime`)
//! * Tokio (implemented directly for `tokio::runtime::Runtime` and `tokio::runtime::Handle`)
//! * Async-std
//! * Smol
//! * The Futures crate
Expand Down Expand Up @@ -54,6 +54,18 @@ impl AsyncExecutor for &tokio::runtime::Runtime {
(*self).block_on(future)
}
}
#[cfg(feature = "async_tokio")]
impl AsyncExecutor for tokio::runtime::Handle {
fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
self.block_on(future)
}
}
#[cfg(feature = "async_tokio")]
impl AsyncExecutor for &tokio::runtime::Handle {
fn block_on<T>(&self, future: impl Future<Output = T>) -> T {
(*self).block_on(future)
}
}

/// Runs futures on the 'async-std' crate's global executor
#[cfg(feature = "async_std")]
Expand Down