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

+26 -21
View File
@@ -7,9 +7,9 @@ use crate::options::{
CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,
};
use crate::output::progress_bar::get_progress_bar;
use crate::quantity::{second, Information, Time, Zero};
use crate::timer::execute_and_measure;
use crate::util::randomized_environment_offset;
use crate::util::units::Second;
use super::measurement::{Measurement, Measurements};
@@ -51,7 +51,7 @@ pub trait Executor {
/// performing a measurement. This should return the time
/// that is being used in addition to the actual runtime
/// of the command.
fn time_overhead(&self) -> Second;
fn time_overhead(&self) -> Time;
}
fn run_command_and_measure_common(
@@ -150,8 +150,8 @@ impl Executor for RawExecutor<'_> {
Ok(())
}
fn time_overhead(&self) -> Second {
0.0
fn time_overhead(&self) -> Time {
Time::zero()
}
}
@@ -203,9 +203,9 @@ impl Executor for ShellExecutor<'_> {
// Subtract shell spawning time
if let Some(spawning_time) = self.shell_spawning_time {
result.time_wall_clock =
(result.time_wall_clock - spawning_time.time_wall_clock).max(0.0);
result.time_user = (result.time_user - spawning_time.time_user).max(0.0);
result.time_system = (result.time_system - spawning_time.time_system).max(0.0);
(result.time_wall_clock - spawning_time.time_wall_clock).max(Time::zero());
result.time_user = (result.time_user - spawning_time.time_user).max(Time::zero());
result.time_system = (result.time_system - spawning_time.time_system).max(Time::zero());
}
Ok(result)
@@ -266,14 +266,14 @@ impl Executor for ShellExecutor<'_> {
time_wall_clock: measurements.time_wall_clock_mean(),
time_user: measurements.time_user_mean(),
time_system: measurements.time_system_mean(),
peak_memory_usage: 0,
peak_memory_usage: Information::zero(),
exit_status: ExitStatus::default(),
});
Ok(())
}
fn time_overhead(&self) -> Second {
fn time_overhead(&self) -> Time {
self.shell_spawning_time.unwrap().time_wall_clock
}
}
@@ -288,13 +288,15 @@ impl MockExecutor {
MockExecutor { shell }
}
fn extract_time<S: AsRef<str>>(sleep_command: S) -> Second {
fn extract_time<S: AsRef<str>>(sleep_command: S) -> Time {
assert!(sleep_command.as_ref().starts_with("sleep "));
sleep_command
.as_ref()
.trim_start_matches("sleep ")
.parse::<Second>()
.unwrap()
Time::new::<second>(
sleep_command
.as_ref()
.trim_start_matches("sleep ")
.parse::<f64>()
.unwrap(),
)
}
}
@@ -320,9 +322,9 @@ impl Executor for MockExecutor {
Ok(Measurement {
time_wall_clock: Self::extract_time(command.get_command_line()),
time_user: 0.0,
time_system: 0.0,
peak_memory_usage: 0,
time_user: Time::zero(),
time_system: Time::zero(),
peak_memory_usage: Information::zero(),
exit_status,
})
}
@@ -331,9 +333,9 @@ impl Executor for MockExecutor {
Ok(())
}
fn time_overhead(&self) -> Second {
fn time_overhead(&self) -> Time {
match &self.shell {
None => 0.0,
None => Time::zero(),
Some(shell) => Self::extract_time(shell),
}
}
@@ -341,5 +343,8 @@ impl Executor for MockExecutor {
#[test]
fn test_mock_executor_extract_time() {
assert_eq!(MockExecutor::extract_time("sleep 0.1"), 0.1);
assert_eq!(
MockExecutor::extract_time("sleep 0.1"),
Time::new::<second>(0.1)
);
}
+21 -16
View File
@@ -1,23 +1,23 @@
use std::process::ExitStatus;
use crate::outlier_detection::modified_zscores;
use crate::quantity::{second, Information, Time};
use crate::util::statistics::{max, mean, median, min, standard_deviation};
use crate::util::units::Second;
/// Performance measurements and exit status from running a single command
#[derive(Debug, Default, Copy, Clone)]
pub struct Measurement {
/// Wall clock time
pub time_wall_clock: Second,
pub time_wall_clock: Time,
/// Time spent in user mode
pub time_user: Second,
pub time_user: Time,
/// Time spent in kernel mode
pub time_system: Second,
pub time_system: Time,
/// Maximum amount of memory used, in bytes
pub peak_memory_usage: u64,
/// Maximum amount of memory used
pub peak_memory_usage: Information,
/// The exit status of the process
pub exit_status: ExitStatus,
@@ -42,17 +42,17 @@ impl Measurements {
self.measurements.push(measurement);
}
pub fn wall_clock_times(&self) -> impl Iterator<Item = Second> + '_ {
pub fn wall_clock_times(&self) -> impl Iterator<Item = Time> + '_ {
self.measurements.iter().map(|m| m.time_wall_clock)
}
/// The average wall clock time.
pub fn time_wall_clock_mean(&self) -> Second {
pub fn time_wall_clock_mean(&self) -> Time {
mean(self.wall_clock_times())
}
/// The standard deviation of wall clock times, if at least two runs were measured.
pub fn stddev(&self) -> Option<Second> {
pub fn stddev(&self) -> Option<Time> {
if self.len() < 2 {
None
} else {
@@ -62,32 +62,37 @@ impl Measurements {
}
/// The median wall clock time.
pub fn median(&self) -> Second {
median(&self.wall_clock_times().collect::<Vec<_>>())
pub fn median(&self) -> Time {
median(self.wall_clock_times())
}
/// The minimum wall clock time.
pub fn min(&self) -> Second {
pub fn min(&self) -> Time {
min(self.wall_clock_times())
}
/// The maximum wall clock time.
pub fn max(&self) -> Second {
pub fn max(&self) -> Time {
max(self.wall_clock_times())
}
/// Compute modified Z-scores for the wall clock times.
pub fn modified_zscores(&self) -> Vec<f64> {
modified_zscores(&self.wall_clock_times().collect::<Vec<_>>())
modified_zscores(
&self
.wall_clock_times()
.map(|t| t.get::<second>())
.collect::<Vec<_>>(),
)
}
/// The average user time.
pub fn time_user_mean(&self) -> Second {
pub fn time_user_mean(&self) -> Time {
mean(self.measurements.iter().map(|m| m.time_user))
}
/// The average system time.
pub fn time_system_mean(&self) -> Second {
pub fn time_system_mean(&self) -> Time {
mean(self.measurements.iter().map(|m| m.time_system))
}
}
+33 -22
View File
@@ -18,8 +18,8 @@ use crate::output::format::{format_duration, format_duration_unit};
use crate::output::progress_bar::get_progress_bar;
use crate::output::warnings::{OutlierWarningOptions, Warnings};
use crate::parameter::ParameterNameAndValue;
use crate::quantity::{byte, const_time_from_seconds, ratio, second, Time, Zero};
use crate::util::exit_code::extract_exit_code;
use crate::util::units::Second;
use benchmark_result::BenchmarkResult;
use measurement::{Measurement, Measurements};
@@ -29,7 +29,7 @@ use colored::*;
use self::executor::Executor;
/// Threshold for warning about fast execution time
pub const MIN_EXECUTION_TIME: Second = 5e-3;
pub const MIN_EXECUTION_TIME: Time = const_time_from_seconds(5e-3);
pub struct Benchmark<'a> {
number: usize,
@@ -236,7 +236,7 @@ impl<'a> Benchmark<'a> {
};
let preparation_result = run_preparation_command()?;
let preparation_overhead = preparation_result.map_or(0.0, |res| {
let preparation_overhead = preparation_result.map_or(Time::zero(), |res| {
res.time_wall_clock + self.executor.time_overhead()
});
@@ -250,7 +250,7 @@ impl<'a> Benchmark<'a> {
let success = res.exit_status.success();
let conclusion_result = run_conclusion_command()?;
let conclusion_overhead = conclusion_result.map_or(0.0, |res| {
let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {
res.time_wall_clock + self.executor.time_overhead()
});
@@ -259,7 +259,8 @@ impl<'a> Benchmark<'a> {
/ (res.time_wall_clock
+ self.executor.time_overhead()
+ preparation_overhead
+ conclusion_overhead)) as u64;
+ conclusion_overhead))
.get::<ratio>() as u64;
let count = {
let min = cmp::max(runs_in_min_time, self.options.run_bounds.min);
@@ -292,8 +293,10 @@ impl<'a> Benchmark<'a> {
run_preparation_command()?;
let msg = {
let mean =
format_duration(measurements.time_wall_clock_mean(), self.options.time_unit);
let mean = format_duration(
measurements.time_wall_clock_mean().get::<second>(),
self.options.time_unit,
);
format!("Current estimate: {}", mean.to_string().green())
};
@@ -336,13 +339,14 @@ impl<'a> Benchmark<'a> {
let system_mean = measurements.time_system_mean();
// Formatting and console output
let (mean_str, time_unit) = format_duration_unit(t_mean, self.options.time_unit);
let min_str = format_duration(t_min, Some(time_unit));
let max_str = format_duration(t_max, Some(time_unit));
let (mean_str, time_unit) =
format_duration_unit(t_mean.get::<second>(), self.options.time_unit);
let min_str = format_duration(t_min.get::<second>(), Some(time_unit));
let max_str = format_duration(t_max.get::<second>(), Some(time_unit));
let num_str = format!("{t_num} runs");
let user_str = format_duration(user_mean, Some(time_unit));
let system_str = format_duration(system_mean, Some(time_unit));
let user_str = format_duration(user_mean.get::<second>(), Some(time_unit));
let system_str = format_duration(system_mean.get::<second>(), Some(time_unit));
if self.options.output_style != OutputStyleOption::Disabled {
let mut stdout = io::stdout().lock();
@@ -357,7 +361,8 @@ impl<'a> Benchmark<'a> {
system_str.blue()
)?;
} else {
let stddev_str = format_duration(t_stddev.unwrap(), Some(time_unit));
let stddev_str =
format_duration(t_stddev.unwrap().get::<second>(), Some(time_unit));
console_writeln!(
stdout,
@@ -437,22 +442,28 @@ impl<'a> Benchmark<'a> {
self.run_cleanup_command(self.command.get_parameters().iter().cloned(), output_policy)?;
// Keep the existing export representation: seconds and integer byte counts.
Ok(BenchmarkResult {
command: self.command.get_name(),
command_with_unused_parameters: self.command.get_name_with_unused_parameters(),
mean: t_mean,
stddev: t_stddev,
median: t_median,
user: user_mean,
system: system_mean,
min: t_min,
max: t_max,
times: Some(measurements.wall_clock_times().collect()),
mean: t_mean.get::<second>(),
stddev: t_stddev.map(|t| t.get::<second>()),
median: t_median.get::<second>(),
user: user_mean.get::<second>(),
system: system_mean.get::<second>(),
min: t_min.get::<second>(),
max: t_max.get::<second>(),
times: Some(
measurements
.wall_clock_times()
.map(|t| t.get::<second>())
.collect(),
),
memory_usage_byte: Some(
measurements
.measurements
.iter()
.map(|m| m.peak_memory_usage)
.map(|m| m.peak_memory_usage.get::<byte>() as u64)
.collect(),
),
exit_codes: measurements