doc_async_probed/
doc_async_probed.rs

1//! Requires feature flag "tokio".
2
3use latency_trace::{summary_stats, LatencyTrace};
4use std::{thread, 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 probed = LatencyTrace::activated_default()
29        .unwrap()
30        .measure_latencies_probed_tokio(f)
31        .unwrap();
32
33    // Let the function run for some time before probing latencies.
34    thread::sleep(Duration::from_micros(48000));
35
36    let latencies1 = probed.probe_latencies();
37    let latencies2 = probed.wait_and_report();
38
39    println!("\nlatencies1 in microseconds");
40    for (span_group, stats) in latencies1.map_values(summary_stats) {
41        println!("  * {:?}, {:?}", span_group, stats);
42    }
43
44    println!("\nlatencies2 in microseconds");
45    for (span_group, stats) in latencies2.map_values(summary_stats) {
46        println!("  * {:?}, {:?}", span_group, stats);
47    }
48}