Skip to content

Commit

Permalink
impl AsyncExecutor for tokio::runtime::Handle (#712)
Browse files Browse the repository at this point in the history
This will allow Tokio-based async benching without necessarily having
access to the `Runtime` itself (e.g. inside of a `#[tokio::main]`
function); a `Handle` is always obtainable via `Handle::current()`.
  • Loading branch information
kpreid authored Aug 4, 2024
1 parent 20cd7e2 commit a5f00c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- gnuplot version is now correctly detected when using certain Windows binaries/configurations that used to fail

### 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
Original file line number Diff line number Diff line change
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
12 changes: 12 additions & 0 deletions src/async_executor.rs
Original file line number Diff line number Diff line change
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

0 comments on commit a5f00c4

Please sign in to comment.