use std::time::{Duration, Instant};
pub struct Timer {
wall: Instant,
cpu: Option<CpuTime>,
}
impl Timer {
pub fn start() -> Self {
Self {
wall: Instant::now(),
cpu: CpuTime::now(),
}
}
pub fn report(&self) -> String {
let real = self.wall.elapsed().as_secs_f64();
match (&self.cpu, CpuTime::now()) {
(Some(start), Some(end)) => {
let user = end.user.saturating_sub(start.user).as_secs_f64();
let sys = end.sys.saturating_sub(start.sys).as_secs_f64();
format!("Run Time (s): real {real:.3} user {user:.6} sys {sys:.6}")
}
_ => format!("Run Time (s): real {real:.3}"),
}
}
}
struct CpuTime {
user: Duration,
sys: Duration,
}
impl CpuTime {
#[cfg(unix)]
fn now() -> Option<Self> {
let mut usage = std::mem::MaybeUninit::<libc::rusage>::uninit();
let usage = unsafe {
if libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) != 0 {
return None;
}
usage.assume_init()
};
let duration = |t: libc::timeval| {
Duration::from_secs(u64::try_from(t.tv_sec).unwrap_or(0))
+ Duration::from_micros(u64::try_from(t.tv_usec).unwrap_or(0))
};
Some(Self {
user: duration(usage.ru_utime),
sys: duration(usage.ru_stime),
})
}
#[cfg(not(unix))]
fn now() -> Option<Self> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reports_real_user_and_sys() {
let timer = Timer::start();
let mut x = 0u64;
for i in 0..2_000_000u64 {
x = x.wrapping_add(i * i);
}
std::hint::black_box(x);
let report = timer.report();
let words: Vec<&str> = report.split(' ').collect();
assert_eq!(&words[..3], ["Run", "Time", "(s):"], "{report}");
assert_eq!(words[3], "real", "{report}");
assert!(words[4].parse::<f64>().is_ok(), "{report}");
if cfg!(unix) {
assert_eq!((words[5], words[7]), ("user", "sys"), "{report}");
assert!(words[6].parse::<f64>().is_ok() && words[8].parse::<f64>().is_ok());
}
}
}