#performance #async #instrument #profile #tracing

async-instrumenter

A wrapper over futures which allows to time how long the future takes to execute

5 releases

0.1.4 Aug 17, 2023
0.1.3 Aug 17, 2023
0.1.2 Aug 17, 2023
0.1.1 Aug 17, 2023
0.1.0 Aug 17, 2023

#131 in Profiling

GPL-3.0+

15KB
91 lines

Async Instrumenter

Crates.io

This Rust library offers a useful future which allows you to time exactly how long any particular future took to execute.

Please note that this is only as accurate as Rust's Instant type. So while it offers a good benchmark of how long, it is limited to how precise std/os/hardware is measuring the time. (If you know of a more accurate implementation, I am all ears)

Usage couldn't be simpler:

async fn sleep() {
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}

// debug log our `sleep()` future with a custom log message
// will only print debug log if `debug_assertions` are enabled
//
// elapsed arg must be used
dbg_instrument!("sleep() took {elapsed:?}", sleep()).await;

// same as above, except with a predefined macro provided log
// message
dbg_instrument!(sleep()).await;

// will always print a debug log message regardless of
// `debug_assertions` status
//
// elapsed arg must be used
instrument!("{elapsed:?}", sleep()).await;

// same as above, except with a predefined macro provided
// log message
instrument!(sleep()).await;

// we can also manually create an instrumenting future if
//we require custom behavior or access to the elapsed data
let res = InstrumentFuture::new(sleep()).await;
println!("took {:?} with result {:?}", res.elapsed, res.result);

Dependencies

~0.4–1MB
~21K SLoC