Use uom quantities for benchmark measurements

This commit is contained in:
David Peter committed 2026-10-05 10:32:32 +02:00
1 parent fbdb75efc3
commit a8e56efecc
14 files changed
+301 -118

No files matched your search

+15 -13
View File
@@ -3,16 +3,16 @@
use std::convert::TryFrom;
use std::mem;
use crate::quantity::{byte, microsecond, Information, Time};
use crate::timer::CPUTimes;
use crate::util::units::Second;
#[derive(Debug, Copy, Clone)]
pub struct CPUInterval {
/// Total amount of time spent executing in user mode
pub user: Second,
pub user: Time,
/// Total amount of time spent executing in kernel mode
pub system: Second,
pub system: Time,
}
pub struct CPUTimer {
@@ -26,13 +26,13 @@ impl CPUTimer {
}
}
pub fn stop(&self) -> (Second, Second, u64) {
pub fn stop(&self) -> (Time, Time, Information) {
let end_cpu = get_cpu_times();
let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);
(
cpu_interval.user,
cpu_interval.system,
end_cpu.memory_usage_byte,
Information::new::<byte>(end_cpu.memory_usage_byte as f64),
)
}
}
@@ -70,11 +70,13 @@ fn get_cpu_times() -> CPUTimes {
/// Compute the time intervals in between two `CPUTimes` snapshots
fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
CPUInterval {
user: ((end.user_usec - start.user_usec) as f64) * 1e-6,
system: ((end.system_usec - start.system_usec) as f64) * 1e-6,
user: Time::new::<microsecond>((end.user_usec - start.user_usec) as f64),
system: Time::new::<microsecond>((end.system_usec - start.system_usec) as f64),
}
}
#[cfg(test)]
use crate::quantity::second;
#[cfg(test)]
use approx::assert_relative_eq;
@@ -93,14 +95,14 @@ fn test_cpu_time_interval() {
};
let t_zero = cpu_time_interval(&t_a, &t_a);
assert!(t_zero.user.abs() < f64::EPSILON);
assert!(t_zero.system.abs() < f64::EPSILON);
assert!(t_zero.user.get::<second>().abs() < f64::EPSILON);
assert!(t_zero.system.get::<second>().abs() < f64::EPSILON);
let t_ab = cpu_time_interval(&t_a, &t_b);
assert_relative_eq!(0.007655, t_ab.user);
assert_relative_eq!(0.015679, t_ab.system);
assert_relative_eq!(0.007655, t_ab.user.get::<second>());
assert_relative_eq!(0.015679, t_ab.system.get::<second>());
let t_ba = cpu_time_interval(&t_b, &t_a);
assert_relative_eq!(-0.007655, t_ba.user);
assert_relative_eq!(-0.015679, t_ba.system);
assert_relative_eq!(-0.007655, t_ba.user.get::<second>());
assert_relative_eq!(-0.015679, t_ba.system.get::<second>());
}
+5 -3
View File
@@ -1,6 +1,6 @@
use std::time::Instant;
use crate::util::units::Second;
use crate::quantity::{nanosecond, second, Time};
pub struct WallClockTimer {
start: Instant,
@@ -13,8 +13,10 @@ impl WallClockTimer {
}
}
pub fn stop(&self) -> Second {
pub fn stop(&self) -> Time {
let duration = self.start.elapsed();
duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-9
Time::new::<second>(duration.as_secs() as f64)
+ Time::new::<nanosecond>(duration.subsec_nanos() as f64)
}
}
+8 -4
View File
@@ -27,7 +27,7 @@ use windows_sys::{
},
};
use crate::util::units::Second;
use crate::quantity::{microsecond, Information, Time, Zero};
const HUNDRED_NS_PER_MS: i64 = 10;
@@ -86,7 +86,7 @@ impl CPUTimer {
Self { job_object }
}
pub fn stop(&self) -> (Second, Second, u64) {
pub fn stop(&self) -> (Time, Time, Information) {
let mut job_object_info =
mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();
@@ -114,9 +114,13 @@ impl CPUTimer {
// for all active processes associated with the job, as well as all terminated
// processes no longer associated with the job, in 100-nanosecond ticks."
let kernel: i64 = job_object_info.TotalKernelTime / HUNDRED_NS_PER_MS;
(user as f64 * 1e-6, kernel as f64 * 1e-6, 0)
(
Time::new::<microsecond>(user as f64),
Time::new::<microsecond>(kernel as f64),
Information::zero(),
)
} else {
(0.0, 0.0, 0)
(Time::zero(), Time::zero(), Information::zero())
}
}
}