doc_sync/
doc_sync.rs

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