use std::cell::RefCell;
use std::collections::HashMap;
use std::time::Instant;
thread_local! {
static METRICS: RefCell<HashMap<&'static str, u64>> = RefCell::new(HashMap::new());
}
pub struct SpanGuard {
name: &'static str,
start: Instant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PerfMeasurement {
pub elapsed_ns: u64,
}
pub struct PerfScope {
name: &'static str,
start: Instant,
}
impl PerfScope {
#[must_use]
pub fn start(_crate_name: &'static str, name: &'static str) -> Self {
Self {
name,
start: Instant::now(),
}
}
#[must_use]
pub fn finish(self) -> PerfMeasurement {
let elapsed_ns = self.start.elapsed().as_nanos() as u64;
METRICS.with(|metrics| {
let mut map = metrics.borrow_mut();
*map.entry(self.name).or_insert(0) += elapsed_ns;
});
PerfMeasurement { elapsed_ns }
}
}
impl Drop for SpanGuard {
fn drop(&mut self) {
let elapsed = self.start.elapsed().as_nanos() as u64;
METRICS.with(|metrics| {
let mut map = metrics.borrow_mut();
*map.entry(self.name).or_insert(0) += elapsed;
});
}
}
pub fn span(name: &'static str) -> SpanGuard {
SpanGuard {
name,
start: Instant::now(),
}
}
pub fn get_metrics() -> HashMap<&'static str, u64> {
METRICS.with(|metrics| metrics.borrow().clone())
}
pub fn reset_metrics() {
METRICS.with(|metrics| metrics.borrow_mut().clear());
}