use std::io::{self, Write};
use std::sync::OnceLock;
use std::time::Instant;
use crate::bench::format_ns;
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct SubMsTick(Instant);
impl SubMsTick {
#[inline]
pub fn elapsed_ns(self) -> u64 {
self.0.elapsed().as_nanos() as u64
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubMsTimerCheckpoint {
pub label: String,
pub since_last_ns: u64,
pub since_start_ns: u64,
pub is_stop: bool,
}
pub struct SubMsTimer {
name: String,
started_at: Instant,
last_at: Instant,
stopped_at: Option<Instant>,
checkpoints: Vec<SubMsTimerCheckpoint>,
}
impl SubMsTimer {
#[inline]
pub fn nanos_now() -> u64 {
static EPOCH: OnceLock<Instant> = OnceLock::new();
let epoch = EPOCH.get_or_init(Instant::now);
epoch.elapsed().as_nanos() as u64
}
#[inline]
pub fn tick() -> SubMsTick {
SubMsTick(Instant::now())
}
#[inline]
pub fn measure_ns<F, T>(f: F) -> (T, u64)
where
F: FnOnce() -> T,
{
let t0 = Instant::now();
let r = f();
let elapsed = t0.elapsed().as_nanos() as u64;
(r, elapsed)
}
pub fn unnamed() -> Self {
Self::new("")
}
pub fn new(name: &str) -> Self {
let now = Instant::now();
Self {
name: name.to_string(),
started_at: now,
last_at: now,
stopped_at: None,
checkpoints: Vec::new(),
}
}
pub fn start(&mut self) -> &mut Self {
let now = Instant::now();
self.started_at = now;
self.last_at = now;
self.stopped_at = None;
self.checkpoints.clear();
self
}
pub fn reset(&mut self) -> &mut Self {
self.start()
}
pub fn mark(&mut self, label: &str) -> u64 {
let now = Instant::now();
let since_start = now.duration_since(self.started_at).as_nanos() as u64;
let since_last = now.duration_since(self.last_at).as_nanos() as u64;
self.last_at = now;
self.checkpoints.push(SubMsTimerCheckpoint {
label: label.to_string(),
since_last_ns: since_last,
since_start_ns: since_start,
is_stop: false,
});
since_start
}
pub fn lap(&mut self, label: &str) -> u64 {
self.mark(label)
}
pub fn stop(&mut self, label: &str) -> u64 {
let now = Instant::now();
let since_start = now.duration_since(self.started_at).as_nanos() as u64;
let since_last = now.duration_since(self.last_at).as_nanos() as u64;
self.last_at = now;
self.stopped_at = Some(now);
self.checkpoints.push(SubMsTimerCheckpoint {
label: label.to_string(),
since_last_ns: since_last,
since_start_ns: since_start,
is_stop: true,
});
since_start
}
pub fn is_stopped(&self) -> bool {
self.stopped_at.is_some()
}
pub fn elapsed_ns(&self) -> u64 {
let end = self.stopped_at.unwrap_or_else(Instant::now);
end.duration_since(self.started_at).as_nanos() as u64
}
pub fn name(&self) -> &str {
&self.name
}
pub fn checkpoints(&self) -> &[SubMsTimerCheckpoint] {
&self.checkpoints
}
pub fn print<W: Write>(&self, out: &mut W) -> io::Result<()> {
writeln!(
out,
"timer \"{}\" total={}",
self.name,
format_ns(self.elapsed_ns())
)?;
for cp in &self.checkpoints {
let label = if cp.is_stop {
format!("{} *", cp.label)
} else {
cp.label.clone()
};
writeln!(
out,
" {:<18} +{:>8} {:>8}",
label,
format_ns(cp.since_last_ns),
format_ns(cp.since_start_ns)
)?;
}
Ok(())
}
}
#[cfg(test)]
#[path = "timer_tests.rs"]
mod tests;