Files
hyperfine/src/benchmark/measurement.rs
T

100 lines
2.7 KiB
Rust
Raw Normal View History

use std::process::ExitStatus;
use crate::quantity::statistics::{max, mean, median, min, modified_zscores, standard_deviation};
2026-10-05 11:14:09 +02:00
use crate::quantity::{Information, Time};
2022-02-20 13:44:34 +01:00
/// Performance measurements and exit status from running a single command
2026-10-05 12:46:10 +02:00
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct Measurement {
2022-02-20 13:44:34 +01:00
/// Wall clock time
pub time_wall_clock: Time,
2022-02-20 13:44:34 +01:00
/// Time spent in user mode
pub time_user: Time,
2022-02-20 13:44:34 +01:00
/// Time spent in kernel mode
pub time_system: Time,
2024-06-23 13:29:05 +02:00
/// Maximum amount of memory used
pub peak_memory_usage: Information,
/// The exit status of the process
2026-10-05 10:09:41 +02:00
pub exit_status: ExitStatus,
2022-02-20 13:44:34 +01:00
}
2026-10-05 10:10:09 +02:00
/// Performance measurements and exit statuses for all runs of a command.
2026-10-05 12:46:10 +02:00
#[derive(Debug, Default, Clone, 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);
}
pub fn wall_clock_times(&self) -> impl Iterator<Item = Time> + '_ {
2026-10-05 10:10:09 +02:00
self.measurements.iter().map(|m| m.time_wall_clock)
}
/// The average wall clock time.
pub fn time_wall_clock_mean(&self) -> Time {
2026-10-05 10:10:09 +02:00
mean(self.wall_clock_times())
}
/// The standard deviation of wall clock times, if at least two runs were measured.
pub fn stddev(&self) -> Option<Time> {
2026-10-05 10:10:09 +02:00
if self.len() < 2 {
None
} else {
let times: Vec<_> = self.wall_clock_times().collect();
Some(standard_deviation(&times, self.time_wall_clock_mean()))
}
}
/// The median wall clock time.
pub fn median(&self) -> Time {
median(self.wall_clock_times())
2026-10-05 10:10:09 +02:00
}
/// The minimum wall clock time.
pub fn min(&self) -> Time {
2026-10-05 10:10:09 +02:00
min(self.wall_clock_times())
}
/// The maximum wall clock time.
pub fn max(&self) -> Time {
2026-10-05 10:10:09 +02:00
max(self.wall_clock_times())
}
/// Compute modified Z-scores for the wall clock times.
pub fn modified_zscores(&self) -> Vec<f64> {
2026-10-05 11:14:09 +02:00
modified_zscores(&self.wall_clock_times().collect::<Vec<_>>())
2026-10-05 10:10:09 +02:00
}
/// The average user time.
pub fn time_user_mean(&self) -> Time {
2026-10-05 10:10:09 +02:00
mean(self.measurements.iter().map(|m| m.time_user))
}
/// The average system time.
pub fn time_system_mean(&self) -> Time {
2026-10-05 10:10:09 +02:00
mean(self.measurements.iter().map(|m| m.time_system))
}
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
}