use alloc::format;
use bevy_platform::sync::atomic::{AtomicBool, Ordering};
use bevy_platform::time::Instant;
pub struct TimerGauge {
pub name: &'static str,
start: Instant,
}
impl TimerGauge {
pub fn new(name: &'static str) -> Self {
Self {
name,
start: Instant::now(),
}
}
}
impl Drop for TimerGauge {
fn drop(&mut self) {
metrics::gauge!(format!("{}/time_ms", self.name))
.set(self.start.elapsed().as_secs_f64() * 1e3_f64);
}
}
pub struct DormantTimerGauge {
timer: TimerGauge,
inactive: AtomicBool,
}
impl DormantTimerGauge {
pub fn new(name: &'static str) -> Self {
Self {
timer: TimerGauge::new(name),
inactive: AtomicBool::new(true),
}
}
pub fn activate(&self) {
self.inactive.store(false, Ordering::Relaxed)
}
}
impl Drop for DormantTimerGauge {
fn drop(&mut self) {
if !self.inactive.load(Ordering::Relaxed) {
metrics::gauge!(format!("{}/time_ms", self.timer.name))
.set(self.timer.start.elapsed().as_secs_f64() * 1e3_f64);
}
}
}