Files
hyperfine/src/benchmark/measurement.rs
T

145 lines
3.7 KiB
Rust
Raw Normal View History

2026-10-05 14:16:20 +02:00
use std::process::ExitStatus;
2026-10-05 14:15:34 +02:00
use serde::Serialize;
2026-10-05 14:17:52 +02:00
use crate::quantity::{
2026-10-05 14:20:12 +02:00
max, mean, median, min, modified_zscores, serialize_information, serialize_time,
standard_deviation, Information, Time,
2026-10-05 14:17:14 +02:00
};
2026-10-05 14:16:20 +02:00
use crate::util::exit_code::extract_exit_code;
fn serialize_exit_status<S>(exit_status: &ExitStatus, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match extract_exit_code(*exit_status) {
Some(code) => serializer.serialize_i32(code),
None => serializer.serialize_unit(),
}
}
2026-10-05 14:15:34 +02:00
/// Performance metric measurements and exit code for a single run
2026-10-05 14:16:20 +02:00
#[derive(Debug, Clone, Default, Serialize, PartialEq)]
pub struct Measurement {
2026-10-05 14:15:34 +02:00
/// Elapsed wall clock time (real time)
2026-10-05 14:17:14 +02:00
#[serde(serialize_with = "serialize_time")]
pub time_wall_clock: Time,
2022-02-20 13:44:34 +01:00
/// Time spent in user mode
2026-10-05 14:17:14 +02:00
#[serde(serialize_with = "serialize_time")]
pub time_user: Time,
2022-02-20 13:44:34 +01:00
/// Time spent in kernel mode
2026-10-05 14:17:14 +02:00
#[serde(serialize_with = "serialize_time")]
pub time_system: Time,
2024-06-23 13:29:05 +02:00
2026-10-05 14:16:01 +02:00
/// Maximum memory usage of the process
2026-10-05 14:17:14 +02:00
#[serde(serialize_with = "serialize_information")]
pub peak_memory_usage: Information,
2026-10-05 14:16:20 +02:00
// The exit status of the process
2026-10-05 14:16:31 +02:00
#[serde(rename = "exit_code", serialize_with = "serialize_exit_status")]
2026-10-05 14:16:20 +02:00
pub exit_status: ExitStatus,
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:17:14 +02:00
pub fn wall_clock_times(&self) -> Vec<Time> {
2026-10-05 14:15:34 +02:00
self.measurements
.iter()
2026-10-05 14:16:20 +02:00
.map(|m| m.time_wall_clock)
2026-10-05 14:15:34 +02:00
.collect()
2026-10-05 10:10:09 +02:00
}
2026-10-05 14:15:34 +02:00
/// The average wall clock time
2026-10-05 14:17:14 +02:00
pub fn time_wall_clock_mean(&self) -> Time {
2026-10-05 14:15:34 +02:00
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:17:14 +02:00
pub fn stddev(&self) -> Option<Time> {
2026-10-05 14:15:34 +02:00
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
2026-10-05 14:17:14 +02:00
pub fn median(&self) -> Time {
2026-10-05 14:15:34 +02:00
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
2026-10-05 14:17:14 +02:00
pub fn min(&self) -> Time {
2026-10-05 14:15:34 +02:00
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
2026-10-05 14:17:14 +02:00
pub fn max(&self) -> Time {
2026-10-05 14:15:34 +02:00
max(&self.wall_clock_times())
}
2026-10-05 14:20:12 +02:00
/// Compute modified Z-scores for the wall clock times
pub fn modified_zscores(&self) -> Vec<f64> {
modified_zscores(&self.wall_clock_times())
}
2026-10-05 14:15:34 +02:00
/// The average user time
2026-10-05 14:17:14 +02:00
pub fn time_user_mean(&self) -> Time {
2026-10-05 14:15:34 +02:00
mean(
&self
.measurements
.iter()
2026-10-05 14:16:20 +02:00
.map(|m| m.time_user)
2026-10-05 14:15:34 +02:00
.collect::<Vec<_>>(),
)
}
/// The average system time
2026-10-05 14:17:14 +02:00
pub fn time_system_mean(&self) -> Time {
2026-10-05 14:15:34 +02:00
mean(
&self
.measurements
.iter()
2026-10-05 14:16:20 +02:00
.map(|m| m.time_system)
2026-10-05 14:15:34 +02:00
.collect::<Vec<_>>(),
)
2026-10-05 10:10:09 +02:00
}
2026-10-05 14:17:14 +02:00
pub fn peak_memory_usage_mean(&self) -> Information {
2026-10-05 14:20:12 +02:00
mean(
2026-10-05 14:16:01 +02:00
&self
2026-10-05 14:20:12 +02:00
.measurements
2026-10-05 14:16:01 +02:00
.iter()
2026-10-05 14:20:12 +02:00
.map(|m| m.peak_memory_usage)
2026-10-05 14:16:01 +02:00
.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
}