|
| 1 | +# Benchmarking Asynchronous Functions |
| 2 | + |
| 3 | +Asynchronous functions run within an async runtime, which can introduce overhead, especially when benchmarking small, simple functions (e.g., using mocks). |
| 4 | + |
| 5 | +[Criterion](https://github.com/bheisler/criterion.rs) supports asynchronous benchmarking and allows users to configure the runtime. It also provides settings that can reduce the overhead introduced by the async runtime. |
| 6 | + |
| 7 | +## Configuring Criterion with Async Runtimes |
| 8 | + |
| 9 | +To minimize noise from the runtime, consider the following: |
| 10 | + |
| 11 | +### Use a New Runtime Instance for Each Iteration |
| 12 | + |
| 13 | +Asynchronous runtimes configure themselves during initialization based on system state, potentially leading to runtime overhead accumulating in one direction. Using a new runtime for each iteration helps distribute this overhead. |
| 14 | + |
| 15 | +Example using Tokio: |
| 16 | + |
| 17 | +```rust |
| 18 | +// Using one runtime for all iterations leads to consistent overhead. |
| 19 | +let runner = tokio::runtime::Runtime::new().unwrap(); |
| 20 | +bencher |
| 21 | + .to_async(&runner) |
| 22 | + .iter(|bencher| ...); |
| 23 | + |
| 24 | +// Using a new runtime per iteration distributes the overhead. |
| 25 | +bencher |
| 26 | + .to_async(tokio::runtime::Runtime::new().unwrap()) |
| 27 | + .iter(|bencher| ...); |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +### Increase Warm-up Time |
| 32 | + |
| 33 | +Allow the system to stabilize by increasing the warm-up time. This ensures that faulty runtime configurations are minimized when the system is cold. |
| 34 | + |
| 35 | +### Additional Configurations |
| 36 | + |
| 37 | +- **Increase sample size and measurement time**: Reduces noise and outliers. |
| 38 | +- **Lower significance level**: Helps with noisy benchmarks to reduce false positives changes. |
| 39 | +- **Raise noise threshold**: Reduces false positives in performance changes. |
| 40 | + |
| 41 | +### Example of the Configuring: |
| 42 | + |
| 43 | +```rust |
| 44 | +criterion_group! { |
| 45 | + ... |
| 46 | + config = Criterion::default() |
| 47 | + // Warm-up time allows stable spawning of multiple async runtimes. |
| 48 | + .warm_up_time(Duration::from_secs(10)) |
| 49 | + // Increased measurement time and sample size reduce noise. |
| 50 | + .measurement_time(Duration::from_secs(20)) |
| 51 | + .sample_size(200) |
| 52 | + // Settings to reduce noise in the results. |
| 53 | + .significance_level(0.01) |
| 54 | + .noise_threshold(0.03); |
| 55 | + ... |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Mocking Async Traits |
| 60 | + |
| 61 | +When benchmarking generic functions with async traits, avoid calling `Future::poll()` on mocks to reduce async overhead. Achieve this by using a non-async inner function, marked as `#[inline(never)]`, within an always-inlined async function. This ensures the non-async function is used without inlining it to mimic the actual trait implementation. |
| 62 | + |
| 63 | +Example: |
| 64 | + |
| 65 | +```rust |
| 66 | +trait FooTrait { |
| 67 | + async fn bar(&self) -> usize; |
| 68 | +} |
| 69 | + |
| 70 | +struct MockFoo; |
| 71 | + |
| 72 | +impl FooTrait for MockFoo { |
| 73 | + #[inline(always)] |
| 74 | + async fn bar(&self) -> usize { |
| 75 | + #[inline(never)] |
| 76 | + fn inner_bar() -> usize { |
| 77 | + black_box(0) |
| 78 | + } |
| 79 | + |
| 80 | + inner_bar() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +``` |
| 85 | +This avoids calling `Future::poll()` when invoking the async function: |
| 86 | + |
| 87 | +```rust |
| 88 | +async some_fun(foo: FooTrait) { |
| 89 | + // The call can be made without awaiting. |
| 90 | + let val = foo.bar().await; |
| 91 | + |
| 92 | + // `MockFoo::bar()` inlines, avoiding future polling, since `inner_bar()` isn't async. |
| 93 | + let val = inner_bar(); |
| 94 | +} |
| 95 | + |
| 96 | +``` |
0 commit comments