Crate future_timed

Crate future_timed 

Source
Expand description

Future timing instrumentation.

Provides instrumentation to record the time taken by a future. This includes the busy time and the idle time. The busy time of a future is the sum of all the time consumed during calls to Future::poll on that future. On the other hand, The idle time of a future is the sum of all the time between calls to Future::poll. The time before the first poll is not included.

§Usage

Add future-timed to your Cargo.toml dependencies:

future-timed = "0.1"

Use the TimedFutureExt extension trait to add an instrumentation closure on a future:

    let output = some_async_fn()
        .timed(|Timing { idle, busy }| {
            assert!(!idle.is_zero());
            assert!(!busy.is_zero());
        })
        .await;

    do_something_with_output(output);

§Comparison with similar crates

This work is based almost entirely on the future-timing crate but sports a different API. While future-timing requires destructuring the future’s output into the timing data and the futures output itself, fuiture-timed allows to report inline and compose the output with subsequent future combinators, for example from the futures crate:

let output = async {
        tokio::time::sleep(Duration::from_micros(10)).await;
        21
    }
    .timed(|Timing { busy, .. }| {
        println!("busy for {busy:?}");
    })
    .map(|n| 2 * n)
    .await;

assert_eq!(output, 42);

Note that in that case you measure the combined time for all wrapped futures.

§License

This project is licensed under the MIT license.

Structs§

Timed
Future for the timed function and timed method.
Timing
Timing information for an instrumented future.

Traits§

TimedFutureExt
An extension trait for Futures that adds the timed method.

Functions§

timed
Instrument a future to record its timing.