doc_async/
doc_async.rs

1//! Requires feature flag "tokio".
2
3use latency_trace::{summary_stats, LatencyTrace};
4use std::time::Duration;
5use tracing::{instrument, trace_span, Instrument};
6
7#[instrument(level = "trace")]
8async fn f() {
9    for _ in 0..1000 {
10        async {
11            // Simulated work
12            tokio::time::sleep(Duration::from_micros(1200)).await;
13
14            g().await;
15        }
16        .instrument(trace_span!("loop_body"))
17        .await
18    }
19}
20
21#[instrument(level = "trace")]
22async fn g() {
23    // Simulated work
24    tokio::time::sleep(Duration::from_micros(800)).await;
25}
26
27fn main() {
28    let latencies = LatencyTrace::activated_default()
29        .unwrap()
30        .measure_latencies_tokio(f);
31
32    println!("\nLatency stats below are in microseconds");
33    for (span_group, stats) in latencies.map_values(summary_stats) {
34        println!("  * {:?}, {:?}", span_group, stats);
35    }
36
37    // A shorter way to print the summary stats, with uglier formatting.
38    println!("\nDebug print of `latencies.map_values(summary_stats)`:");
39    println!("{:?}", latencies.map_values(summary_stats));
40}