use bevy_platform::sync::atomic::{AtomicBool, Ordering};
use bevy_platform::time::Instant;
pub struct TimerGauge {
pub name: &'static str,
start: Instant,
}
impl TimerGauge {
#[doc(hidden)]
pub fn from_metric_name(name: &'static str) -> Self {
Self {
name,
start: Instant::now(),
}
}
}
#[macro_export]
macro_rules! timer_gauge {
($name:literal $(,)?) => {
$crate::metrics::TimerGauge::from_metric_name(::core::concat!($name, "/time_ms"))
};
}
impl Drop for TimerGauge {
fn drop(&mut self) {
metrics::gauge!(self.name).set(self.start.elapsed().as_secs_f64() * 1e3_f64);
}
}
pub struct DormantTimerGauge {
timer: TimerGauge,
inactive: AtomicBool,
}
impl DormantTimerGauge {
#[doc(hidden)]
pub fn from_metric_name(name: &'static str) -> Self {
Self {
timer: TimerGauge::from_metric_name(name),
inactive: AtomicBool::new(true),
}
}
pub fn activate(&self) {
self.inactive.store(false, Ordering::Relaxed)
}
}
#[macro_export]
macro_rules! dormant_timer_gauge {
($name:literal $(,)?) => {
$crate::metrics::DormantTimerGauge::from_metric_name(::core::concat!($name, "/time_ms"))
};
}
impl Drop for DormantTimerGauge {
fn drop(&mut self) {
if !self.inactive.load(Ordering::Relaxed) {
metrics::gauge!(self.timer.name)
.set(self.timer.start.elapsed().as_secs_f64() * 1e3_f64);
}
}
}