use std::sync::OnceLock;
use std::time::Instant;
pub fn enabled() -> bool {
static ENABLED: OnceLock<bool> = OnceLock::new();
*ENABLED.get_or_init(|| std::env::var("LIEUI_PERF").is_ok_and(|v| v == "1"))
}
pub struct Span {
label: &'static str,
start: Instant,
reported: bool,
}
impl Span {
pub fn start(label: &'static str) -> Self {
Self {
label,
start: Instant::now(),
reported: !enabled(),
}
}
pub fn finish(mut self) {
self.report();
}
fn report(&mut self) {
if !self.reported {
self.reported = true;
eprintln!(
"[lieui-perf] {:<16} {:>8.1}us",
self.label,
self.start.elapsed().as_secs_f64() * 1e6
);
}
}
}
impl Drop for Span {
fn drop(&mut self) {
self.report();
}
}