Compare commits

..
Author SHA1 Message Date
David Peter 6f557e0494 Extract typed quantity formatting 2026-10-05 11:46:49 +02:00
David Peter 862625fb3d Merge pull request #951 from Likio3000/codex/reject-negative-parameter-steps
Reject negative parameter steps before expanding ranges
2026-10-05 11:32:10 +02:00
David Peter d3d67fc908 Merge pull request #950 from Likio3000/codex/preserve-exports-on-invalid-options
Preserve export files when command options are invalid
2026-10-05 11:30:58 +02:00
David Peter e43bf855c0 Merge pull request #965 from sharkdp/codex/wait4-resource-usage
Use `wait4` for per-command resource usage on Unix
2026-10-05 11:25:55 +02:00
David Peter 430dc5ed25 Merge pull request #966 from sharkdp/codex/csv-six-decimals
Format CSV times with six decimal places
2026-10-05 11:24:01 +02:00
David Peter 1c1be471f9 Merge pull request #964 from sharkdp/codex/windows-timer-precision
Preserve 100-nanosecond precision in Windows CPU times
2026-10-05 11:19:22 +02:00
David Peter f16b6b97bc Merge pull request #963 from sharkdp/codex/measurement-helpers
Extract quantity and command helpers
2026-10-05 11:18:28 +02:00
David Peter b4fb20f85b Format CSV times with six decimal places 2026-10-05 11:16:20 +02:00
David Peter c72a4c764a Use wait4 for per-command resource usage on Unix 2026-10-05 11:15:39 +02:00
David Peter 85293b50fd Preserve 100-nanosecond precision in Windows CPU times 2026-10-05 11:15:01 +02:00
David Peter b5f58651a1 Extract quantity and command helpers 2026-10-05 11:14:09 +02:00
David Peter ecf8a24b1e Merge pull request #962 from sharkdp/codex/quantity-module-layout
Group units, formatting, and statistics under quantity
2026-10-05 11:04:56 +02:00
David Peter 975a25ce44 Merge pull request #961 from sharkdp/codex/uom-quantities
Use uom quantities for benchmark measurements
2026-10-05 10:47:07 +02:00
David Peter 9cdb9c4c50 Merge pull request #840 from sharkdp/dependabot/github_actions/actions/checkout-6
Bump actions/checkout from 5 to 6
2026-10-05 10:30:51 +02:00
Eric Soler 31ae72b2b7 Reject negative parameter steps before expanding ranges 2026-10-03 16:23:02 +00:00
Eric Soler e2717c064e Preserve export files when command options are invalid 2026-10-03 15:09:02 +00:00
dependabot[bot] ba98c8c44b Bump actions/checkout from 5 to 6
Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-21 11:07:22 +00:00
21 changed files with 511 additions and 258 deletions

No files matched your search

+4 -4
View File
@@ -18,7 +18,7 @@ jobs:
name: Extract crate metadata
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
- name: Extract crate information
id: crate_metadata
run: |
@@ -49,7 +49,7 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- uses: actions/checkout@v5
- uses: actions/checkout@v6
- run: cargo fmt -- --check
min_version:
@@ -58,7 +58,7 @@ jobs:
needs: crate_metadata
steps:
- name: Checkout source code
uses: actions/checkout@v5
uses: actions/checkout@v6
- name: Install rust toolchain (v${{ needs.crate_metadata.outputs.msrv }})
uses: dtolnay/rust-toolchain@master
@@ -96,7 +96,7 @@ jobs:
BUILD_CMD: cargo
steps:
- name: Checkout source code
uses: actions/checkout@v5
uses: actions/checkout@v6
- name: Install prerequisites
shell: bash
+1 -1
View File
@@ -180,7 +180,7 @@ The exact syntax may vary depending on your shell and OS.
\fB\-D\fR, \fB\-\-parameter\-step\-size\fR \fIDELTA\fP
.IP
This argument requires \fB\-\-parameter\-scan\fR to be specified as well. Traverse the
range \fIMIN..MAX\fP in steps of \fIDELTA\fP.
range \fIMIN..MAX\fP in steps of \fIDELTA\fP, which must be positive.
.IP
.RS
Example:
+1 -1
View File
@@ -266,7 +266,7 @@ 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: Information::zero(),
peak_memory_usage: measurements.peak_memory_usage_mean(),
exit_status: ExitStatus::default(),
});
+6 -7
View File
@@ -1,7 +1,7 @@
use std::process::ExitStatus;
use crate::quantity::statistics::{max, mean, median, min, modified_zscores, standard_deviation};
use crate::quantity::{second, Information, Time};
use crate::quantity::{Information, Time};
/// Performance measurements and exit status from running a single command
#[derive(Debug, Default, Copy, Clone)]
@@ -77,12 +77,7 @@ impl Measurements {
/// Compute modified Z-scores for the wall clock times.
pub fn modified_zscores(&self) -> Vec<f64> {
modified_zscores(
&self
.wall_clock_times()
.map(|t| t.get::<second>())
.collect::<Vec<_>>(),
)
modified_zscores(&self.wall_clock_times().collect::<Vec<_>>())
}
/// The average user time.
@@ -94,4 +89,8 @@ impl Measurements {
pub fn time_system_mean(&self) -> Time {
mean(self.measurements.iter().map(|m| m.time_system))
}
pub fn peak_memory_usage_mean(&self) -> Information {
mean(self.measurements.iter().map(|m| m.peak_memory_usage))
}
}
+14 -15
View File
@@ -17,9 +17,7 @@ use crate::output::console_writeln;
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, format_duration, format_duration_unit, ratio, second, Time, Zero,
};
use crate::quantity::{byte, const_time_from_seconds, ratio, second, FormatQuantity, Time, Zero};
use crate::util::exit_code::extract_exit_code;
use benchmark_result::BenchmarkResult;
use measurement::{Measurement, Measurements};
@@ -294,10 +292,12 @@ impl<'a> Benchmark<'a> {
run_preparation_command()?;
let msg = {
let mean = format_duration(
measurements.time_wall_clock_mean().get::<second>(),
self.options.time_unit,
);
let t_wall_clock_mean = measurements.time_wall_clock_mean();
let time_unit = self
.options
.time_unit
.unwrap_or(t_wall_clock_mean.suitable_unit());
let mean = t_wall_clock_mean.format(time_unit);
format!("Current estimate: {}", mean.to_string().green())
};
@@ -340,14 +340,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.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 time_unit = self.options.time_unit.unwrap_or(t_mean.suitable_unit());
let mean_str = t_mean.format(time_unit);
let min_str = t_min.format(time_unit);
let max_str = t_max.format(time_unit);
let num_str = format!("{t_num} runs");
let user_str = format_duration(user_mean.get::<second>(), Some(time_unit));
let system_str = format_duration(system_mean.get::<second>(), Some(time_unit));
let user_str = user_mean.format(time_unit);
let system_str = system_mean.format(time_unit);
if self.options.output_style != OutputStyleOption::Disabled {
let mut stdout = io::stdout().lock();
@@ -362,8 +362,7 @@ impl<'a> Benchmark<'a> {
system_str.blue()
)?;
} else {
let stddev_str =
format_duration(t_stddev.unwrap().get::<second>(), Some(time_unit));
let stddev_str = t_stddev.unwrap().format(time_unit);
console_writeln!(
stdout,
+1 -1
View File
@@ -182,7 +182,7 @@ fn build_command() -> Command {
.requires("parameter-scan")
.help(
"This argument requires --parameter-scan to be specified as well. \
Traverse the range MIN..MAX in steps of DELTA.\n\n \
Traverse the range MIN..MAX in steps of DELTA, which must be positive.\n\n \
Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\
This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",
),
+5 -1
View File
@@ -97,10 +97,14 @@ impl<'a> Command<'a> {
&self.parameters
}
pub fn is_parameter_unused(&self, parameter: &str) -> bool {
!self.expression.contains(&format!("{{{parameter}}}"))
}
pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {
self.parameters
.iter()
.filter(move |(parameter, _)| !self.expression.contains(&format!("{{{parameter}}}")))
.filter(move |(parameter, _)| self.is_parameter_unused(parameter))
}
fn replace_parameters_in(&self, original: &str) -> String {
+2
View File
@@ -19,6 +19,8 @@ pub enum ParameterScanError {
TooLarge,
#[error("Zero is not a valid parameter step")]
ZeroStep,
#[error("Parameter step size must be positive")]
NegativeStep,
#[error("A step size is required when the range bounds are floating point numbers. The step size can be specified with the '-D/--parameter-step-size <DELTA>' parameter")]
StepRequired,
#[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]
+5 -3
View File
@@ -19,6 +19,8 @@ impl Exporter for CsvExporter {
_unit: Option<TimeUnit>,
_sort_order: SortOrder,
) -> Result<Vec<u8>> {
const CSV_PRECISION: usize = 6;
let mut writer = WriterBuilder::new().from_writer(vec![]);
{
@@ -48,7 +50,7 @@ impl Exporter for CsvExporter {
res.min,
res.max,
] {
fields.push(Cow::Owned(f.to_string().into_bytes()))
fields.push(Cow::Owned(format!("{f:.CSV_PRECISION$}").into_bytes()))
}
for v in res.parameters.values() {
fields.push(Cow::Borrowed(v.as_bytes()))
@@ -117,7 +119,7 @@ fn test_csv() {
insta::assert_snapshot!(actual, @r#"
command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo
command_a,1,2,1,3,4,5,6,two,one
command_b,11,12,11,13,14,15,16.5,seven,one
command_a,1.000000,2.000000,1.000000,3.000000,4.000000,5.000000,6.000000,two,one
command_b,11.000000,12.000000,11.000000,13.000000,14.000000,15.000000,16.500000,seven,one
"#);
}
+6 -6
View File
@@ -1,7 +1,7 @@
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
use crate::options::SortOrder;
use crate::quantity::{format_duration_value, TimeUnit};
use crate::quantity::{second, FormatQuantity, IsUnit, Time, TimeUnit};
use super::Exporter;
use anyhow::Result;
@@ -50,14 +50,14 @@ pub trait MarkupExporter {
let cmd_str = measurement
.command_with_unused_parameters
.replace('|', "\\|");
let mean_str = format_duration_value(measurement.mean, Some(unit)).0;
let mean_str = Time::new::<second>(measurement.mean).format_value(unit);
let stddev_str = if let Some(stddev) = measurement.stddev {
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
format!(" ± {}", Time::new::<second>(stddev).format_value(unit))
} else {
"".into()
};
let min_str = format_duration_value(measurement.min, Some(unit)).0;
let max_str = format_duration_value(measurement.max, Some(unit)).0;
let min_str = Time::new::<second>(measurement.min).format_value(unit);
let max_str = Time::new::<second>(measurement.max).format_value(unit);
let rel_str = format!("{:.2}", entry.relative_speed);
let rel_stddev_str = if entry.is_reference {
"".into()
@@ -101,7 +101,7 @@ pub trait MarkupExporter {
fn determine_unit_from_results(results: &[BenchmarkResult]) -> TimeUnit {
if let Some(first_result) = results.first() {
// Use the first BenchmarkResult entry to determine the unit for all entries.
format_duration_value(first_result.mean, None).1
Time::new::<second>(first_result.mean).suitable_unit()
} else {
// Default to `Second`.
TimeUnit::Second
+3 -3
View File
@@ -25,7 +25,7 @@ pub mod options;
pub mod outlier_detection;
pub mod output;
pub mod parameter;
mod quantity;
pub mod quantity;
pub mod timer;
pub mod util;
@@ -37,14 +37,14 @@ fn run() -> Result<()> {
let cli_arguments = get_cli_arguments(env::args_os());
let mut options = Options::from_cli_arguments(&cli_arguments)?;
let commands = Commands::from_cli_arguments(&cli_arguments)?;
options.validate_against_command_list(&commands)?;
let export_manager = ExportManager::from_cli_arguments(
&cli_arguments,
options.time_unit,
options.sort_order_exports,
)?;
options.validate_against_command_list(&commands)?;
let mut scheduler = Scheduler::new(&commands, &options, &export_manager);
scheduler.run_benchmarks()?;
scheduler.print_relative_speed_comparison()?;
+2 -2
View File
@@ -14,13 +14,13 @@ pub const OUTLIER_THRESHOLD: f64 = 1.4826 * 10.0;
/// modified Z-score that is larger than `OUTLIER_THRESHOLD`.
#[cfg(test)]
pub fn num_outliers(xs: &[f64]) -> usize {
use crate::quantity::statistics::modified_zscores;
use crate::quantity::statistics::modified_zscores_f64;
if xs.is_empty() {
return 0;
}
let scores = modified_zscores(xs);
let scores = modified_zscores_f64(xs);
scores
.iter()
.filter(|&&s| s.abs() > OUTLIER_THRESHOLD)
+4 -4
View File
@@ -1,7 +1,7 @@
use std::fmt;
use crate::benchmark::MIN_EXECUTION_TIME;
use crate::quantity::{format_duration, second, Time};
use crate::quantity::{FormatQuantity, Time, TimeUnit};
pub struct OutlierWarningOptions {
pub warmup_in_use: bool,
@@ -21,11 +21,11 @@ impl fmt::Display for Warnings {
match *self {
Warnings::FastExecutionTime => write!(
f,
"Command took less than {:.0} ms to complete. Note that the results might be \
"Command took less than {} ms to complete. Note that the results might be \
inaccurate because hyperfine can not calibrate the shell startup time much \
more precise than this limit. You can try to use the `-N`/`--shell=none` \
option to disable the shell completely.",
MIN_EXECUTION_TIME.get::<second>() * 1e3
MIN_EXECUTION_TIME.format_with_precision(TimeUnit::MilliSecond, 0)
),
Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),
Warnings::SlowInitialRun(time_first_run, ref options) => write!(
@@ -33,7 +33,7 @@ impl fmt::Display for Warnings {
"The first benchmarking run for this command was significantly slower than the \
rest ({time}). This could be caused by (filesystem) caches that were not filled until \
after the first run. {hints}",
time=format_duration(time_first_run.get::<second>(), None),
time=time_first_run.format_auto(),
hints=match (options.warmup_in_use, options.prepare_in_use) {
(true, true) => "You are already using both the '--warmup' option as well \
as the '--prepare' option. Consider re-running the benchmark on a quiet system. \
+34
View File
@@ -47,6 +47,10 @@ impl<T: Numeric> RangeStep<T> {
return Err(ParameterScanError::ZeroStep);
}
if step < T::from(0) {
return Err(ParameterScanError::NegativeStep);
}
const MAX_PARAMETERS: usize = 100_000;
match range_step_size_hint(start, end, step) {
(_, Some(size)) if size <= MAX_PARAMETERS => Ok(Self {
@@ -120,6 +124,36 @@ mod tests {
assert_eq!(param_range[10], Decimal::from(1));
}
#[test]
fn test_negative_integer_step() {
for step in [-2, -3, i32::MIN, -1] {
for end in [0, 1, 10] {
let result = RangeStep::new(0, end, step);
assert_eq!(
result.unwrap_err().to_string(),
"Parameter step size must be positive"
);
}
}
}
#[test]
fn test_negative_decimal_step() {
for step in ["-2.0", "-0.1"] {
for end in [0, 1, 10] {
let result = RangeStep::new(
Decimal::ZERO,
Decimal::from(end),
Decimal::from_str(step).unwrap(),
);
assert_eq!(
result.unwrap_err().to_string(),
"Parameter step size must be positive"
);
}
}
}
#[test]
fn test_range_step_validate() {
let result = RangeStep::new(0, 10, 3);
+144 -74
View File
@@ -1,16 +1,64 @@
use std::marker::PhantomData;
pub use uom::num_traits::Zero;
pub use uom::si::f64::{Information, Ratio, Time};
pub use uom::si::information::byte;
pub use uom::si::ratio::ratio;
pub use uom::si::time::{microsecond, nanosecond, second};
use uom::si;
pub use units::TimeUnit;
pub use si::f64::{Information, Ratio, Time};
pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};
pub use si::ratio::ratio;
pub use si::time::{hour, microsecond, millisecond, minute, nanosecond, second};
pub use uom::num_traits::Zero;
pub use units::{InformationUnit, IsUnit, TimeUnit};
pub mod statistics;
mod units;
pub trait FormatQuantity {
type Unit;
fn suitable_unit(&self) -> Self::Unit;
fn format_with_precision(&self, unit: Self::Unit, precision: usize) -> String;
fn format(&self, unit: Self::Unit) -> String;
fn format_auto(&self) -> String;
fn format_value(&self, unit: Self::Unit) -> String;
}
impl FormatQuantity for Time {
type Unit = TimeUnit;
fn suitable_unit(&self) -> TimeUnit {
if *self < Time::new::<millisecond>(1.0) {
TimeUnit::MicroSecond
} else if *self < Time::new::<second>(1.0) {
TimeUnit::MilliSecond
} else {
TimeUnit::Second
}
}
/// Format the time duration in the given unit with the given precision.
fn format_with_precision(&self, u: TimeUnit, precision: usize) -> String {
u.format(*self, precision)
}
/// Format the time duration in the given unit.
fn format(&self, unit: TimeUnit) -> String {
let value = self.format_with_precision(unit, unit.preferred_precision());
format!("{} {}", value, unit.short_name())
}
/// Format the given time duration. The unit will be determined automatically.
fn format_auto(&self) -> String {
self.format(self.suitable_unit())
}
/// Like `format`, but without displaying the unit.
fn format_value(&self, unit: TimeUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision())
}
}
pub const fn const_time_from_seconds(value: f64) -> Time {
// Quantity::new in uom is not yet const: https://docs.rs/uom/0.36.0/uom/si/struct.Quantity.html
Time {
@@ -20,41 +68,43 @@ pub const fn const_time_from_seconds(value: f64) -> Time {
}
}
/// Format the given duration as a string. The output-unit can be enforced by setting `unit` to
/// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.
pub fn format_duration(duration: f64, unit: Option<TimeUnit>) -> String {
let (duration_fmt, _) = format_duration_unit(duration, unit);
duration_fmt
}
impl FormatQuantity for Information {
type Unit = InformationUnit;
/// Like `format_duration`, but returns the target unit as well.
pub fn format_duration_unit(duration: f64, unit: Option<TimeUnit>) -> (String, TimeUnit) {
let (out_str, out_unit) = format_duration_value(duration, unit);
fn suitable_unit(&self) -> InformationUnit {
if *self < Information::new::<kibibyte>(1.0) {
InformationUnit::Byte
} else if *self < Information::new::<mebibyte>(1.0) {
InformationUnit::KibiByte
} else if *self < Information::new::<gibibyte>(1.0) {
InformationUnit::MebiByte
} else {
InformationUnit::GibiByte
}
}
(format!("{} {}", out_str, out_unit.short_name()), out_unit)
}
/// Format the information in the given unit with the given precision.
fn format_with_precision(&self, u: InformationUnit, precision: usize) -> String {
u.format(*self, precision)
}
/// Like `format_duration`, but returns the target unit as well.
pub fn format_duration_value(duration: f64, unit: Option<TimeUnit>) -> (String, TimeUnit) {
if (duration < 0.001 && unit.is_none()) || unit == Some(TimeUnit::MicroSecond) {
(
TimeUnit::MicroSecond.format(duration),
TimeUnit::MicroSecond,
)
} else if (duration < 1.0 && unit.is_none()) || unit == Some(TimeUnit::MilliSecond) {
(
TimeUnit::MilliSecond.format(duration),
TimeUnit::MilliSecond,
)
} else {
let unit = unit.unwrap_or(TimeUnit::Second);
(unit.format(duration), unit)
/// Format the information in the given unit.
fn format(&self, unit: InformationUnit) -> String {
let value = self.format_with_precision(unit, unit.preferred_precision());
format!("{} {}", value, unit.short_name())
}
/// Format the given information. The unit will be determined automatically.
fn format_auto(&self) -> String {
self.format(self.suitable_unit())
}
/// Like `format`, but without displaying the unit.
fn format_value(&self, unit: InformationUnit) -> String {
self.format_with_precision(unit, unit.preferred_precision())
}
}
#[cfg(test)]
use uom::si::{information::kibibyte, time::millisecond};
#[test]
fn test_time() {
let time = Time::new::<millisecond>(123.4);
@@ -82,52 +132,72 @@ fn test_information() {
}
#[test]
fn test_format_duration_unit_basic() {
let (out_str, out_unit) = format_duration_unit(1.3, None);
fn test_suiteable_unit_time() {
assert_eq!(Time::new::<second>(1.3).suitable_unit(), TimeUnit::Second);
assert_eq!(Time::new::<second>(1.0).suitable_unit(), TimeUnit::Second);
assert_eq!(
Time::new::<second>(0.999).suitable_unit(),
TimeUnit::MilliSecond
);
assert_eq!(
Time::new::<second>(0.0005).suitable_unit(),
TimeUnit::MicroSecond
);
assert_eq!(
Time::new::<second>(0.).suitable_unit(),
TimeUnit::MicroSecond
);
assert_eq!(
Time::new::<second>(1000.0).suitable_unit(),
TimeUnit::Second
);
}
assert_eq!("1.300 s", out_str);
assert_eq!(TimeUnit::Second, out_unit);
#[test]
fn test_suitable_unit_information() {
assert_eq!(
Information::new::<byte>(512.0).suitable_unit(),
InformationUnit::Byte
);
assert_eq!(
Information::new::<byte>(2048.0).suitable_unit(),
InformationUnit::KibiByte
);
assert_eq!(
Information::new::<byte>(2_097_152.0).suitable_unit(),
InformationUnit::MebiByte
);
assert_eq!(
Information::new::<byte>(2_147_483_648.0).suitable_unit(),
InformationUnit::GibiByte
);
}
let (out_str, out_unit) = format_duration_unit(1.0, None);
#[test]
fn test_format() {
let time = Time::new::<millisecond>(123.4);
assert_eq!(time.format(TimeUnit::Second), "0.123 s");
assert_eq!(time.format(TimeUnit::MilliSecond), "123.4 ms");
assert_eq!(time.format_auto(), "123.4 ms");
assert_eq!(time.format(TimeUnit::MicroSecond), "123400.0 µs");
assert_eq!("1.000 s", out_str);
assert_eq!(TimeUnit::Second, out_unit);
let (out_str, out_unit) = format_duration_unit(0.999, None);
assert_eq!("999.0 ms", out_str);
assert_eq!(TimeUnit::MilliSecond, out_unit);
let (out_str, out_unit) = format_duration_unit(0.0005, None);
assert_eq!("500.0 µs", out_str);
assert_eq!(TimeUnit::MicroSecond, out_unit);
let (out_str, out_unit) = format_duration_unit(0.0, None);
assert_eq!("0.0 µs", out_str);
assert_eq!(TimeUnit::MicroSecond, out_unit);
let (out_str, out_unit) = format_duration_unit(1000.0, None);
assert_eq!("1000.000 s", out_str);
assert_eq!(TimeUnit::Second, out_unit);
let peak_memory_usage = Information::new::<kibibyte>(8.);
assert_eq!(peak_memory_usage.format_auto(), "8.0 KiB");
assert_eq!(peak_memory_usage.format(InformationUnit::Byte), "8192 B");
assert_eq!(
peak_memory_usage.format(InformationUnit::KibiByte),
"8.0 KiB"
);
}
#[test]
fn test_format_duration_unit_with_unit() {
let (out_str, out_unit) = format_duration_unit(1.3, Some(TimeUnit::Second));
let out = Time::new::<second>(1.3).format(TimeUnit::Second);
assert_eq!("1.300 s", out);
assert_eq!("1.300 s", out_str);
assert_eq!(TimeUnit::Second, out_unit);
let out = Time::new::<second>(1.3).format(TimeUnit::MilliSecond);
assert_eq!("1300.0 ms", out);
let (out_str, out_unit) = format_duration_unit(1.3, Some(TimeUnit::MilliSecond));
assert_eq!("1300.0 ms", out_str);
assert_eq!(TimeUnit::MilliSecond, out_unit);
let (out_str, out_unit) = format_duration_unit(1.3, Some(TimeUnit::MicroSecond));
assert_eq!("1300000.0 µs", out_str);
assert_eq!(TimeUnit::MicroSecond, out_unit);
let out = Time::new::<second>(1.3).format(TimeUnit::MicroSecond);
assert_eq!("1300000.0 µs", out);
}
+6 -1
View File
@@ -112,7 +112,7 @@ pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q], mean: Q) -> Q {
///
/// References:
/// - <https://en.wikipedia.org/wiki/Median_absolute_deviation>
pub fn modified_zscores(xs: &[f64]) -> Vec<f64> {
pub fn modified_zscores_f64(xs: &[f64]) -> Vec<f64> {
assert!(!xs.is_empty());
// Compute sample median:
@@ -131,6 +131,11 @@ pub fn modified_zscores(xs: &[f64]) -> Vec<f64> {
xs.iter().map(|&x| (x - x_median) / mad).collect()
}
pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {
let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();
modified_zscores_f64(&values)
}
#[cfg(test)]
mod tests {
use super::*;
+135 -25
View File
@@ -1,41 +1,146 @@
//! This module contains common units.
//! This module contains common units for time and information quantities.
use std::marker::PhantomData;
use crate::quantity::{
byte, gibibyte, hour, kibibyte, mebibyte, microsecond, millisecond, minute, second, tebibyte,
Information, Time,
};
pub trait IsUnit {
type Quantity;
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Self::Quantity>>;
fn preferred_precision(&self) -> usize;
fn short_name(&self) -> &'static str {
self.dispatch().short_name()
}
fn format(&self, value: Self::Quantity, precision: usize) -> String {
self.dispatch().format_value(value, precision)
}
}
/// Supported time units
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit {
Second,
MilliSecond,
MicroSecond,
MilliSecond,
Second,
Minute,
Hour,
}
impl TimeUnit {
/// The abbreviation of the TimeUnit.
pub fn short_name(self) -> String {
impl IsUnit for TimeUnit {
type Quantity = Time;
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Time>> {
match self {
TimeUnit::Second => String::from("s"),
TimeUnit::MilliSecond => String::from("ms"),
TimeUnit::MicroSecond => String::from("µs"),
TimeUnit::Minute => String::from("min"),
TimeUnit::Hour => String::from("h"),
TimeUnit::MicroSecond => Box::new(TimeUnitDispatcher::<microsecond>::new()),
TimeUnit::MilliSecond => Box::new(TimeUnitDispatcher::<millisecond>::new()),
TimeUnit::Second => Box::new(TimeUnitDispatcher::<second>::new()),
TimeUnit::Minute => Box::new(TimeUnitDispatcher::<minute>::new()),
TimeUnit::Hour => Box::new(TimeUnitDispatcher::<hour>::new()),
}
}
/// Returns the value in seconds formatted for the TimeUnit.
pub fn format(self, value: f64) -> String {
fn preferred_precision(&self) -> usize {
match self {
TimeUnit::Second => format!("{value:.3}"),
TimeUnit::MilliSecond => format!("{:.1}", value * 1e3),
TimeUnit::MicroSecond => format!("{:.1}", value * 1e6),
TimeUnit::Minute => format!("{:.1}", value / 60.0),
TimeUnit::Hour => format!("{:.1}", value / 3600.0),
TimeUnit::Second => 3,
_ => 1,
}
}
}
/// Supported information units
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InformationUnit {
Byte,
KibiByte,
MebiByte,
GibiByte,
TebiByte,
}
impl IsUnit for InformationUnit {
type Quantity = Information;
fn dispatch(&self) -> Box<dyn UnitImpl<Quantity = Information>> {
match self {
InformationUnit::Byte => Box::new(InformationUnitDispatcher::<byte>::new()),
InformationUnit::KibiByte => Box::new(InformationUnitDispatcher::<kibibyte>::new()),
InformationUnit::MebiByte => Box::new(InformationUnitDispatcher::<mebibyte>::new()),
InformationUnit::GibiByte => Box::new(InformationUnitDispatcher::<gibibyte>::new()),
InformationUnit::TebiByte => Box::new(InformationUnitDispatcher::<tebibyte>::new()),
}
}
fn preferred_precision(&self) -> usize {
match self {
InformationUnit::Byte => 0,
_ => 1,
}
}
}
pub trait UnitImpl {
type Quantity;
fn short_name(&self) -> &'static str;
fn format_value(&self, value: Self::Quantity, precision: usize) -> String;
}
struct TimeUnitDispatcher<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> {
u: PhantomData<U>,
}
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> TimeUnitDispatcher<U> {
fn new() -> Self {
TimeUnitDispatcher { u: PhantomData }
}
}
impl<U: uom::si::time::Unit + uom::Conversion<f64, T = f64>> UnitImpl for TimeUnitDispatcher<U> {
type Quantity = Time;
fn short_name(&self) -> &'static str {
U::abbreviation()
}
fn format_value(&self, value: Time, precision: usize) -> String {
format!("{value:.precision$}", value = value.get::<U>())
}
}
struct InformationUnitDispatcher<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> {
u: PhantomData<U>,
}
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> InformationUnitDispatcher<U> {
fn new() -> Self {
InformationUnitDispatcher { u: PhantomData }
}
}
impl<U: uom::si::information::Unit + uom::Conversion<f64, T = f64>> UnitImpl
for InformationUnitDispatcher<U>
{
type Quantity = Information;
fn short_name(&self) -> &'static str {
U::abbreviation()
}
fn format_value(&self, value: Information, precision: usize) -> String {
format!("{value:.precision$}", value = value.get::<U>())
}
}
#[test]
fn test_unit_short_name() {
fn test_time_unit_short_name() {
assert_eq!("s", TimeUnit::Second.short_name());
assert_eq!("ms", TimeUnit::MilliSecond.short_name());
assert_eq!("µs", TimeUnit::MicroSecond.short_name());
@@ -46,11 +151,16 @@ fn test_unit_short_name() {
// Note - the values are rounded when formatted.
#[test]
fn test_unit_format() {
let value: f64 = 123.456789;
assert_eq!("123.457", TimeUnit::Second.format(value));
assert_eq!("123456.8", TimeUnit::MilliSecond.format(value));
use crate::quantity::FormatQuantity;
assert_eq!("1234.6", TimeUnit::MicroSecond.format(0.00123456));
assert_eq!("2.1", TimeUnit::Minute.format(value));
assert_eq!("0.0", TimeUnit::Hour.format(value));
let value = Time::new::<second>(123.456789);
assert_eq!("123.457", value.format_value(TimeUnit::Second));
assert_eq!("123456.8", value.format_value(TimeUnit::MilliSecond));
assert_eq!(
"1234.6",
Time::new::<second>(0.00123456).format_value(TimeUnit::MicroSecond)
);
assert_eq!("2.1", value.format_value(TimeUnit::Minute));
assert_eq!("0.0", value.format_value(TimeUnit::Hour));
}
+6 -13
View File
@@ -24,19 +24,6 @@ use std::process::{ChildStdout, Command};
use anyhow::Result;
#[cfg(not(windows))]
#[derive(Debug, Copy, Clone)]
struct CPUTimes {
/// Total amount of time spent executing in user mode
pub user_usec: i64,
/// Total amount of time spent executing in kernel mode
pub system_usec: i64,
/// Maximum amount of memory used by the process, in bytes
pub memory_usage_byte: u64,
}
/// Discard the output of a child process.
fn discard(output: ChildStdout) {
const CHUNK_SIZE: usize = 64 << 10;
@@ -95,9 +82,15 @@ pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
discard(output);
}
#[cfg(not(windows))]
let (time_user, time_system, peak_memory_usage, exit_status) = cpu_timer.stop(child)?;
#[cfg(windows)]
let exit_status = child.wait()?;
let time_wall_clock = wallclock_timer.stop();
#[cfg(windows)]
let (time_user, time_system, peak_memory_usage) = cpu_timer.stop();
Ok(Measurement {
+68 -87
View File
@@ -1,108 +1,89 @@
#![cfg(not(windows))]
use std::convert::TryFrom;
use std::mem;
use std::io;
use std::mem::MaybeUninit;
use std::os::unix::process::ExitStatusExt;
use std::process::{Child, ExitStatus};
use crate::quantity::{byte, microsecond, Information, Time};
use crate::timer::CPUTimes;
use anyhow::Result;
use crate::quantity::{byte, kibibyte, microsecond, second, Information, Time};
#[derive(Debug, Copy, Clone)]
pub struct CPUInterval {
struct ResourceUsage {
/// Total amount of time spent executing in user mode
pub user: Time,
pub time_user: Time,
/// Total amount of time spent executing in kernel mode
pub system: Time,
pub time_system: Time,
/// Maximum amount of memory used by the process, in bytes
pub memory_usage: Information,
}
pub struct CPUTimer {
start_cpu: CPUTimes,
#[allow(clippy::useless_conversion)]
fn convert_timeval(tv: libc::timeval) -> Time {
let sec = tv.tv_sec as f64;
let usec = tv.tv_usec as f64;
Time::new::<second>(sec) + Time::new::<microsecond>(usec)
}
impl CPUTimer {
pub fn start() -> Self {
CPUTimer {
start_cpu: get_cpu_times(),
#[allow(clippy::useless_conversion)]
fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
drop(child.stdin.take());
let pid = child.id() as i32;
let mut status = 0;
let mut rusage = MaybeUninit::zeroed();
loop {
let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };
if result >= 0 {
break;
}
let error = io::Error::last_os_error();
if error.kind() != io::ErrorKind::Interrupted {
return Err(error);
}
}
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,
Information::new::<byte>(end_cpu.memory_usage_byte as f64),
)
}
}
let rusage = unsafe { rusage.assume_init() };
/// Read CPU execution times ('user' and 'system')
fn get_cpu_times() -> CPUTimes {
use libc::{getrusage, rusage, RUSAGE_CHILDREN};
let result: rusage = unsafe {
let mut buf = mem::zeroed();
let success = getrusage(RUSAGE_CHILDREN, &mut buf);
assert_eq!(0, success);
buf
};
const MICROSEC_PER_SEC: i64 = 1000 * 1000;
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
let max_rss_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
result.ru_maxrss
let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
// A f64 can represent integers up to 2^53 exactly, so we can represent
// all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision
// is larger than 1 byte, but that's considered acceptable.
Information::new::<byte>(rusage.ru_maxrss as f64)
} else {
result.ru_maxrss * 1024
Information::new::<kibibyte>(rusage.ru_maxrss as f64)
};
#[allow(clippy::useless_conversion)]
CPUTimes {
user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC
+ i64::from(result.ru_utime.tv_usec),
system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC
+ i64::from(result.ru_stime.tv_usec),
memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),
Ok((
ExitStatus::from_raw(status),
ResourceUsage {
time_user: convert_timeval(rusage.ru_utime),
time_system: convert_timeval(rusage.ru_stime),
memory_usage: memory_usage_byte.into(),
},
))
}
pub struct CPUTimer {}
impl CPUTimer {
pub fn start() -> Self {
Self {}
}
pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {
let (status, usage) = wait4(child)?;
Ok((
usage.time_user,
usage.time_system,
usage.memory_usage,
status,
))
}
}
/// Compute the time intervals in between two `CPUTimes` snapshots
fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
CPUInterval {
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;
#[test]
fn test_cpu_time_interval() {
let t_a = CPUTimes {
user_usec: 12345,
system_usec: 54321,
memory_usage_byte: 0,
};
let t_b = CPUTimes {
user_usec: 20000,
system_usec: 70000,
memory_usage_byte: 0,
};
let t_zero = cpu_time_interval(&t_a, &t_a);
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.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.get::<second>());
assert_relative_eq!(-0.015679, t_ba.system.get::<second>());
}
+5 -10
View File
@@ -27,9 +27,7 @@ use windows_sys::{
},
};
use crate::quantity::{microsecond, Information, Time, Zero};
const HUNDRED_NS_PER_MS: i64 = 10;
use crate::quantity::{nanosecond, Information, Time, Zero};
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
#[allow(non_upper_case_globals)]
@@ -108,17 +106,14 @@ impl CPUTimer {
// The `TotalUserTime` is "The total amount of user-mode execution time 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 user: i64 = job_object_info.TotalUserTime / HUNDRED_NS_PER_MS;
let user_time = Time::new::<nanosecond>((job_object_info.TotalUserTime as f64) * 100.0);
// The `TotalKernelTime` is "The total amount of kernel-mode execution time
// 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;
(
Time::new::<microsecond>(user as f64),
Time::new::<microsecond>(kernel as f64),
Information::zero(),
)
let system_time =
Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);
(user_time, system_time, Information::zero())
} else {
(Time::zero(), Time::zero(), Information::zero())
}
+59
View File
@@ -779,6 +779,22 @@ fn performs_all_benchmarks_in_parameter_scan() {
");
}
#[test]
fn rejects_negative_parameter_steps() {
for step in ["-1", "-1.0"] {
hyperfine()
.args(["--parameter-scan", "n", "0", "1"])
.arg(format!("--parameter-step-size={step}"))
.arg("echo {n}")
.assert()
.code(1)
.stdout(predicate::str::is_empty())
.stderr(predicate::str::contains(
"Parameter step size must be positive",
));
}
}
#[test]
fn performs_reference_and_all_benchmarks_in_parameter_scan() {
let _settings = snapshot_settings().bind_to_scope();
@@ -870,6 +886,49 @@ fn exports_intermediate_results_to_file() {
assert!(contents.contains("true"));
}
#[test]
fn invalid_command_options_preserve_export_files() {
use tempfile::tempdir;
for (option, values) in [
("--prepare", ["echo first", "echo second"]),
("--conclude", ["echo first", "echo second"]),
("--output", ["null", "pipe"]),
] {
let directory = tempdir().unwrap();
let existing_export = directory.path().join("previous.json");
let new_export = directory.path().join("new.csv");
let previous_results =
b"{\"results\":[{\"command\":\"previous benchmark\",\"mean\":1.0}]}\n";
std::fs::write(&existing_export, previous_results).unwrap();
hyperfine()
.arg("--runs=1")
.arg("--export-json")
.arg(&existing_export)
.arg("--export-csv")
.arg(&new_export)
.args([option, values[0], option, values[1]])
.arg("echo benchmark")
.assert()
.failure()
.stderr(predicate::str::contains(format!(
"The '{option}' option has to be provided just once or N times"
)));
assert_eq!(
std::fs::read(&existing_export).unwrap(),
previous_results,
"{option} validation must preserve previous results"
);
assert!(
!new_export.exists(),
"{} validation must not create export files",
option
);
}
}
#[test]
fn unused_parameters_are_shown_in_benchmark_name() {
hyperfine()