Files
hyperfine/src/benchmark/measurement.rs
T

120 lines
2.9 KiB
Rust
Raw Normal View History

2026-10-05 14:15:34 +02:00
use serde::Serialize;
2026-10-05 14:16:01 +02:00
use crate::benchmark::quantity::{max, mean, median, min, standard_deviation, Byte, Second};
use crate::outlier_detection::modified_zscores;
2026-10-05 14:15:34 +02:00
/// Performance metric measurements and exit code for a single run
2026-10-05 14:15:49 +02:00
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Measurement {
2026-10-05 14:15:34 +02:00
/// Elapsed wall clock time (real time)
pub wall_clock_time: Second,
2022-02-20 13:44:34 +01:00
/// Time spent in user mode
2026-10-05 14:15:34 +02:00
pub user_time: Second,
2022-02-20 13:44:34 +01:00
/// Time spent in kernel mode
2026-10-05 14:15:34 +02:00
pub system_time: Second,
2024-06-23 13:29:05 +02:00
2026-10-05 14:16:01 +02:00
/// Maximum memory usage of the process
pub memory_usage_byte: Byte,
2026-10-05 14:15:34 +02:00
/// Exit codes of the process
pub exit_code: Option<i32>,
2022-02-20 13:44:34 +01:00
}
2026-10-05 10:10:09 +02:00
2026-10-05 14:15:34 +02:00
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
2026-10-05 10:10:09 +02:00
pub struct Measurements {
pub measurements: Vec<Measurement>,
}
impl Measurements {
2026-10-05 12:46:10 +02:00
pub fn new(measurements: Vec<Measurement>) -> Self {
Self { measurements }
}
2026-10-05 10:10:09 +02:00
pub fn len(&self) -> usize {
self.measurements.len()
}
pub fn is_empty(&self) -> bool {
self.measurements.is_empty()
}
pub fn push(&mut self, measurement: Measurement) {
self.measurements.push(measurement);
}
2026-10-05 14:15:34 +02:00
pub fn wall_clock_times(&self) -> Vec<Second> {
self.measurements
.iter()
.map(|m| m.wall_clock_time)
.collect()
2026-10-05 10:10:09 +02:00
}
2026-10-05 14:15:34 +02:00
/// The average wall clock time
pub fn mean(&self) -> Second {
mean(&self.wall_clock_times())
2026-10-05 10:10:09 +02:00
}
/// The standard deviation of all wall clock times. Not available if only one run has been performed
2026-10-05 14:15:34 +02:00
pub fn stddev(&self) -> Option<Second> {
let times = self.wall_clock_times();
2026-10-05 14:16:01 +02:00
if times.len() < 2 {
2026-10-05 14:15:34 +02:00
None
2026-10-05 14:16:01 +02:00
} else {
Some(standard_deviation(&times))
2026-10-05 10:10:09 +02:00
}
}
2026-10-05 14:15:34 +02:00
/// The median wall clock time
pub fn median(&self) -> Second {
median(&self.wall_clock_times())
2026-10-05 10:10:09 +02:00
}
2026-10-05 14:15:34 +02:00
/// The minimum wall clock time
pub fn min(&self) -> Second {
min(&self.wall_clock_times())
2026-10-05 10:10:09 +02:00
}
2026-10-05 14:15:34 +02:00
/// The maximum wall clock time
pub fn max(&self) -> Second {
max(&self.wall_clock_times())
}
/// The average user time
pub fn user_mean(&self) -> Second {
mean(
&self
.measurements
.iter()
.map(|m| m.user_time)
.collect::<Vec<_>>(),
)
}
/// The average system time
pub fn system_mean(&self) -> Second {
mean(
&self
.measurements
.iter()
.map(|m| m.system_time)
.collect::<Vec<_>>(),
)
2026-10-05 10:10:09 +02:00
}
pub fn modified_zscores(&self) -> Vec<f64> {
2026-10-05 14:16:01 +02:00
modified_zscores(
&self
.wall_clock_times()
.iter()
.map(|t| t.value_in::<Second>()) // TODO
.collect::<Vec<_>>(),
)
2026-10-05 10:10:09 +02:00
}
2026-10-05 11:14:09 +02:00
pub fn peak_memory_usage_mean(&self) -> Information {
mean(self.measurements.iter().map(|m| m.peak_memory_usage))
}
2026-10-05 10:10:09 +02:00
}