Skip to content
Myr

Merge pull request #807 from willcl-ark/expose-run-var

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

expose HYPERFINE_ITERATION to intermediate commands

authored by David Peter · committed by GitHub · parents 05de077, 3558138 · Browse files · 3 files+81 added−18 removed
This commit

State

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

Diff stats

3 files+81 added−18 removed

  • tests:+42 added−0 removed
  • other:+39 added−18 removed

3 files+81 added−18 removed

modified:src/benchmark/executor.rs+1 added
13 unmodified lines
14use super::measurement::{Measurement, Measurements};1516use anyhow::{bail, Context, Result};1718#[derive(Clone, Copy)]19pub enum BenchmarkIteration {20 NonBenchmarkRun,21 Warmup(u64),22 Benchmark(u64),
329 unmodified lines
modified:src/benchmark/mod.rs+38 added-18 removed
56 unmodified lines
57 &self,58 command: &Command<'_>,59 error_output: &'static str,60 output_policy: &CommandOutputPolicy,61 iteration: executor::BenchmarkIteration,62 ) -> Result<Measurement> {63 self.executor64 .run_command_and_measure(65 command,65 executor::BenchmarkIteration::NonBenchmarkRun,66 iteration,67 Some(CmdFailureAction::RaiseError),68 output_policy,69 )70 .map_err(|_| anyhow!(error_output))
14 unmodified lines
85 let error_output = "The setup command terminated with a non-zero exit code. \86 Append ' || true' to the command if you are sure that this can be ignored.";8788 Ok(command88 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy))89 .map(|cmd| {90 self.run_intermediate_command(91 &cmd,92 error_output,93 output_policy,94 BenchmarkIteration::NonBenchmarkRun,95 )96 })97 .transpose()?98 .unwrap_or_default())99 }100
12 unmodified lines
113 let error_output = "The cleanup command terminated with a non-zero exit code. \114 Append ' || true' to the command if you are sure that this can be ignored.";115116 Ok(command109 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy))117 .map(|cmd| {118 self.run_intermediate_command(119 &cmd,120 error_output,121 output_policy,122 BenchmarkIteration::NonBenchmarkRun,123 )124 })125 .transpose()?126 .unwrap_or_default())127 }128
1 unmodified line
130 fn run_preparation_command(131 &self,132 command: &Command<'_>,133 output_policy: &CommandOutputPolicy,134 iteration: executor::BenchmarkIteration,135 ) -> Result<Measurement> {136 let error_output = "The preparation command terminated with a non-zero exit code. \137 Append ' || true' to the command if you are sure that this can be ignored.";138123 self.run_intermediate_command(command, error_output, output_policy)139 self.run_intermediate_command(command, error_output, output_policy, iteration)140 }141142 /// Run the command specified by `--conclude`.143 fn run_conclusion_command(144 &self,145 command: &Command<'_>,146 output_policy: &CommandOutputPolicy,147 iteration: executor::BenchmarkIteration,148 ) -> Result<Measurement> {149 let error_output = "The conclusion command terminated with a non-zero exit code. \150 Append ' || true' to the command if you are sure that this can be ignored.";151135 self.run_intermediate_command(command, error_output, output_policy)152 self.run_intermediate_command(command, error_output, output_policy, iteration)153 }154155 /// Run the benchmark for a single command156 pub fn run(&self) -> Result<BenchmarkResult> {
24 unmodified lines
181 self.command.get_parameters().iter().cloned(),182 )183 });184168 let run_preparation_command = || {185 let run_preparation_command = |iteration: executor::BenchmarkIteration| {186 preparation_command187 .as_ref()171 .map(|cmd| self.run_preparation_command(cmd, output_policy))188 .map(|cmd| self.run_preparation_command(cmd, output_policy, iteration))189 .transpose()190 };191192 let conclusion_command = self.options.conclusion_command.as_ref().map(|values| {
7 unmodified lines
200 conclusion_command,201 self.command.get_parameters().iter().cloned(),202 )203 });187 let run_conclusion_command = || {204 let run_conclusion_command = |iteration: executor::BenchmarkIteration| {205 conclusion_command206 .as_ref()190 .map(|cmd| self.run_conclusion_command(cmd, output_policy))207 .map(|cmd| self.run_conclusion_command(cmd, output_policy, iteration))208 .transpose()209 };210211 self.run_setup_command(self.command.get_parameters().iter().cloned(), output_policy)?;
10 unmodified lines
222 None223 };224225 for i in 0..self.options.warmup_count {226 let warmup_iteration = BenchmarkIteration::Warmup(i);209 let _ = run_preparation_command()?;227 let _ = run_preparation_command(warmup_iteration)?;228 let _ = self.executor.run_command_and_measure(229 self.command,212 BenchmarkIteration::Warmup(i),230 warmup_iteration,231 None,232 output_policy,233 )?;216 let _ = run_conclusion_command()?;234 let _ = run_conclusion_command(warmup_iteration)?;235 if let Some(bar) = progress_bar.as_ref() {236 bar.inc(1)237 }238 }
12 unmodified lines
251 } else {252 None253 };254255 let benchmark_iteration = BenchmarkIteration::Benchmark(0);237 let preparation_result = run_preparation_command()?;256 let preparation_result = run_preparation_command(benchmark_iteration)?;257 let preparation_overhead = preparation_result.map_or(Time::zero(), |res| {258 res.time_wall_clock + self.executor.time_overhead()259 });260261 // Initial timing run262 let res = self.executor.run_command_and_measure(263 self.command,245 BenchmarkIteration::Benchmark(0),264 benchmark_iteration,265 None,266 output_policy,267 )?;268 let success = res.exit_status.success();269251 let conclusion_result = run_conclusion_command()?;270 let conclusion_result = run_conclusion_command(benchmark_iteration)?;271 let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {272 res.time_wall_clock + self.executor.time_overhead()273 });274
32 unmodified lines
307 }308309 // Gather statistics (perform the actual benchmark)310 for i in 0..count_remaining {311 let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);292 run_preparation_command()?;312 run_preparation_command(benchmark_iteration)?;313314 let msg = {315 let t_wall_clock_mean = measurements.time_wall_clock_mean();316 let time_unit = self
9 unmodified lines
326 }327328 let res = self.executor.run_command_and_measure(329 self.command,310 BenchmarkIteration::Benchmark(i + 1),330 benchmark_iteration,331 None,332 output_policy,333 )?;334 let success = res.exit_status.success();
5 unmodified lines
340 if let Some(bar) = progress_bar.as_ref() {341 bar.inc(1)342 }343324 run_conclusion_command()?;344 run_conclusion_command(benchmark_iteration)?;345 }346347 if let Some(bar) = progress_bar.as_ref() {348 bar.finish_and_clear()
161 unmodified lines
modified:tests/integration_tests.rs+42 added
1035 unmodified lines
1036 .arg("dir \"..\\src\\\" \"..\\tests\\\"")1037 .assert()1038 .success();1039}10401041#[test]1042fn hyperfine_iteration_env_var_in_prepare_and_conclude_commands() {1043 let tempdir = tempfile::tempdir().unwrap();1044 let output_path = tempdir.path().join("iteration output.txt");1045 let iteration = if cfg!(windows) {1046 "%HYPERFINE_ITERATION%"1047 } else {1048 "$HYPERFINE_ITERATION"1049 };1050 let command = |phase| format!(r#"echo {phase}:{iteration} >> "iteration output.txt""#);10511052 hyperfine()1053 .current_dir(tempdir.path())1054 .arg("--runs=2")1055 .arg("--warmup=1")1056 .arg("--prepare")1057 .arg(command("prepare"))1058 .arg(command("benchmark"))1059 .arg("--conclude")1060 .arg(command("conclude"))1061 .assert()1062 .success();10631064 let contents = std::fs::read_to_string(output_path).unwrap();1065 // cmd.exe's echo includes the space before the redirection operator.1066 let lines: Vec<&str> = contents.lines().map(str::trim_end).collect();1067 assert_eq!(1068 lines,1069 [1070 "prepare:warmup-0",1071 "benchmark:warmup-0",1072 "conclude:warmup-0",1073 "prepare:0",1074 "benchmark:0",1075 "conclude:0",1076 "prepare:1",1077 "benchmark:1",1078 "conclude:1",1079 ]1080 );1081}