Skip to content
Myr

Add support for one run with --runs=1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Extend the --runs=N option added in d78c33b (Added options to specify the max/exact numbers of runs., 2018-09-09) to support --runs=1 instead of dying with a usage error. This is useful to do ad-hoc testing of your commands while they might still have syntax errors, or just for doing one run where you don't care about the stddev. Before: $ /usr/bin/hyperfine -s basic -r 1 -L n 5,10 'sleep 0.{n}' Error: Number of runs below two After (-s basic) also works: $ hyperfine -r 1 -L n 5,10 'sleep 0.{n}' Benchmark 1: sleep 0.5 Time (abs ≡): 500.6 ms [User: 0.6 ms, System: 0.0 ms] Benchmark 2: sleep 0.10 Time (abs ≡): 100.8 ms [User: 0.7 ms, System: 0.0 ms] Summary 'sleep 0.10' ran 4.97 ± 0.00 times faster than 'sleep 0.5' This likewise combines correctly with -m and -M, probably not very useful, but if you're tweaking an existing command-line: $ hyperfine -m 1 -M 1 -L n 5,10 'sleep 0.{n}' Benchmark 1: sleep 0.5 Time (abs ≡): 500.6 ms [User: 0.5 ms, System: 0.0 ms] Benchmark 2: sleep 0.10 Time (abs ≡): 100.6 ms [User: 0.6 ms, System: 0.0 ms] Summary 'sleep 0.10' ran 4.98 ± 0.00 times faster than 'sleep 0.5' The "± 0.00" output in "faster than" should probably be adjusted too, or we could keep it for consistency. I didn't implement that because this is the first time I do anything in Rust, and I ran out of template to copy when wanting to quickly implement this in write_benchmark_comparison() in main.rs.

authored by Ævar Arnfjörð Bjarmason · committed by David Peter · parent 4b419c6 · Browse files · 4 files+27 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+27 added−14 removed

  • tests:+13 added−2 removed
  • other:+14 added−12 removed

4 files+27 added−14 removed

modified:src/benchmark.rs+14 added-2 removed
389 unmodified lines
390391 // Compute statistical quantities392 let t_num = times_real.len();393 let t_mean = mean(&times_real);394 let mut t_stddev = 0.0;395 if times_real.len() != 1 {394 let t_stddev = standard_deviation(&times_real, Some(t_mean));396 t_stddev = standard_deviation(&times_real, Some(t_mean));397 }398 let t_median = median(&times_real);399 let t_min = min(&times_real);400 let t_max = max(&times_real);401
9 unmodified lines
411412 let user_str = format_duration(user_mean, Some(time_unit));413 let system_str = format_duration(system_mean, Some(time_unit));414412 if options.output_style != OutputStyleOption::Disabled {415 if options.output_style != OutputStyleOption::Disabled && times_real.len() == 1 {416 println!(417 " Time ({} ≡): {:>8} {:>8} [User: {}, System: {}]",418 "abs".green().bold(),419 mean_str.green().bold(),420 " ".to_string(), // alignment421 user_str.blue(),422 system_str.blue()423 );424 } else if options.output_style != OutputStyleOption::Disabled {425 println!(426 " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",427 "mean".green().bold(),428 "σ".green(),429 mean_str.green().bold(),
71 unmodified lines
modified:src/error.rs-2 removed
55 unmodified lines
56impl Error for ParameterScanError {}5758#[derive(Debug)]59pub enum OptionsError<'a> {60 RunsBelowTwo,60 EmptyRunsRange,61 TooManyCommandNames(usize),62 UnexpectedCommandNameCount(usize, usize),63 NumericParsingError(&'a str, ParseIntError),
4 unmodified lines
68impl<'a> fmt::Display for OptionsError<'a> {69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {70 match *self {71 OptionsError::EmptyRunsRange => write!(f, "Empty runs range"),73 OptionsError::RunsBelowTwo => write!(f, "Number of runs below two"),72 OptionsError::TooManyCommandNames(n) => {73 write!(f, "Too many --command-name options: expected {} at most", n)74 }75 OptionsError::UnexpectedCommandNameCount(real, expected) => {
21 unmodified lines
modified:src/main.rs-8 removed
169 unmodified lines
170 max_runs = Some(runs);171 }172173 match (min_runs, max_runs) {174 (Some(min), _) if min < 2 => {175 // We need at least two runs to compute a variance.176 return Err(OptionsError::RunsBelowTwo);177 }174 (Some(min), None) => {175 options.runs.min = min;176 }181 (_, Some(max)) if max < 2 => {182 // We need at least two runs to compute a variance.183 return Err(OptionsError::RunsBelowTwo);184 }177 (None, Some(max)) => {178 // Since the minimum was not explicit we lower it if max is below the default min.179 options.runs.min = cmp::min(options.runs.min, max);180 options.runs.max = Some(max);
270 unmodified lines
modified:tests/integration_tests.rs+13 added-2 removed
21 unmodified lines
22 .success();23}2425#[test]26fn at_least_two_runs_are_required() {26fn one_run_is_supported() {27 hyperfine()28 .arg("--runs=1")29 .arg("echo dummy benchmark")30 .assert()31 .failure();31 .success();32}3334struct ExecutionOrderTest {35 cmd: assert_cmd::Command,
73 unmodified lines
109 }110}111112#[test]113fn benchmarks_are_executed_sequentially_one() {114 ExecutionOrderTest::new()115 .arg("--runs=1")116 .command("command 1")117 .command("command 2")118 .expect_output("command 1")119 .expect_output("command 2")120 .run();121}122123#[test]124fn benchmarks_are_executed_sequentially() {125 ExecutionOrderTest::new()126 .arg("--runs=2")127 .command("command 1")
98 unmodified lines