vor 0.2.0

Cross-platform performance instrumentation with an in-app egui panel and live system and GPU metrics.
Documentation
use std::sync::atomic::{AtomicU64, Ordering};

use web_time::Instant;

static EMPTY_SPAN_NS: AtomicU64 = AtomicU64::new(0);

/// Measure the per-span overhead of `tracing::trace_span`.
///
/// Run once at startup; callers subtract [`empty_span_ns`] from any
/// recorded span to isolate the work the span wraps.
pub fn calibrate() {
    let mut samples = [0u64; 1024];
    for s in &mut samples {
        let t = Instant::now();
        {
            let _span = tracing::trace_span!("__empty").entered();
        }
        *s = t.elapsed().as_nanos() as u64;
    }
    samples.sort_unstable();
    EMPTY_SPAN_NS.store(samples[samples.len() / 2], Ordering::Relaxed);
}

pub fn empty_span_ns() -> u64 {
    EMPTY_SPAN_NS.load(Ordering::Relaxed)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_span_calibration_is_measurable() {
        calibrate();
        let ns = empty_span_ns();
        assert!(ns > 0);
        assert!(ns < 100_000);
    }
}