use latency_trace::{summary_stats, LatencyTrace};
use std::{thread, time::Duration};
use tracing::{info, instrument, level_filters::LevelFilter, trace, trace_span};
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*, Registry};
#[instrument(level = "info")]
fn f() {
trace!("in f");
for _ in 0..10 {
trace_span!("loop_body").in_scope(|| {
info!("in loop body");
thread::sleep(Duration::from_micros(1200));
g();
});
}
}
#[instrument(level = "info")]
fn g() {
trace!("in g");
thread::sleep(Duration::from_micros(800));
}
fn main() {
let lt = LatencyTrace::default();
let ltl = lt.clone().with_filter(LevelFilter::INFO);
let tfmt = tracing_subscriber::fmt::layer()
.with_span_events(FmtSpan::FULL)
.with_filter(LevelFilter::TRACE);
let layered = Registry::default().with(ltl).with(tfmt);
layered.init();
let latencies = lt.measure_latencies(f);
println!("\nLatency stats below are in microseconds");
for (span_group, stats) in latencies.map_values(summary_stats) {
println!(" * {:?}, {:?}", span_group, stats);
}
println!("\nDebug print of `latencies.map_values(summary_stats)`:");
println!("{:?}", latencies.map_values(summary_stats));
}