Skip to content
Myr

expose HYPERFINE_ITERATION to intermediate commands

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

Fixes: #781 Expose the `$HYPERFINE_ITERATION` variable to intermediate commands. This will allow iteration-specific prepare/conclude, such as: ```bash hyperfine --runs 5 --prepare 'echo touch file${HYPERFINE_ITERATION}.dat' \ 'echo testing file${HYPERFINE_ITERATION}.dat' \ --conclude 'echo rm file${HYPERFINE_ITERATION}.dat' --show-output ```

authored by willcl-ark · committed by David Peter · parent 29a0ef1 · Browse files · 3 files+158 added−26 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+158 added−26 removed

  • tests:+116 added−0 removed
  • other:+42 added−26 removed

3 files+158 added−26 removed

modified:src/benchmark/executor.rs+6 added-6 removed
34 unmodified lines
35 /// Run the given command and measure the execution time36 fn run_command_and_measure(37 &self,38 command: &Command<'_>,39 iteration: BenchmarkIteration,39 iteration: &BenchmarkIteration,40 command_failure_action: Option<CmdFailureAction>,41 output_policy: &CommandOutputPolicy,42 ) -> Result<Measurement>;43
11 unmodified lines
55}5657fn run_command_and_measure_common(58 mut command: std::process::Command,59 iteration: BenchmarkIteration,59 iteration: &BenchmarkIteration,60 command_failure_action: CmdFailureAction,61 command_input_policy: &CommandInputPolicy,62 command_output_policy: &CommandOutputPolicy,63 command_name: &str,
67 unmodified lines
131impl Executor for RawExecutor<'_> {132 fn run_command_and_measure(133 &self,134 command: &Command<'_>,135 iteration: BenchmarkIteration,135 iteration: &BenchmarkIteration,136 command_failure_action: Option<CmdFailureAction>,137 output_policy: &CommandOutputPolicy,138 ) -> Result<Measurement> {139 run_command_and_measure_common(
34 unmodified lines
174impl Executor for ShellExecutor<'_> {175 fn run_command_and_measure(176 &self,177 command: &Command<'_>,178 iteration: BenchmarkIteration,178 iteration: &BenchmarkIteration,179 command_failure_action: Option<CmdFailureAction>,180 output_policy: &CommandOutputPolicy,181 ) -> Result<Measurement> {182 let on_windows_cmd = cfg!(windows) && *self.shell == Shell::Default("cmd.exe");
46 unmodified lines
229 for _ in 0..COUNT {230 // Just run the shell without any command231 let res = self.run_command_and_measure(232 &Command::new(None, ""),233 BenchmarkIteration::NonBenchmarkRun,233 &BenchmarkIteration::NonBenchmarkRun,234 None,235 &CommandOutputPolicy::Null,236 );237
65 unmodified lines
303impl Executor for MockExecutor {304 fn run_command_and_measure(305 &self,306 command: &Command<'_>,307 _iteration: BenchmarkIteration,307 _iteration: &BenchmarkIteration,308 _command_failure_action: Option<CmdFailureAction>,309 _output_policy: &CommandOutputPolicy,310 ) -> Result<Measurement> {311 #[cfg(unix)]
39 unmodified lines
modified:src/benchmark/mod.rs+36 added-20 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))
3 unmodified lines
74 fn run_setup_command(75 &self,76 parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,77 output_policy: &CommandOutputPolicy,78 iteration: executor::BenchmarkIteration,79 ) -> Result<Measurement> {80 let command = self81 .options82 .setup_command
3 unmodified lines
86 let error_output = "The setup command terminated with a non-zero exit code. \87 Append ' || true' to the command if you are sure that this can be ignored.";8889 Ok(command88 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy))90 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy, &iteration))91 .transpose()?92 .unwrap_or_default())93 }94
1 unmodified line
96 fn run_cleanup_command(97 &self,98 parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,99 output_policy: &CommandOutputPolicy,100 iteration: executor::BenchmarkIteration,101 ) -> Result<Measurement> {102 let command = self103 .options104 .cleanup_command
3 unmodified lines
108 let error_output = "The cleanup command terminated with a non-zero exit code. \109 Append ' || true' to the command if you are sure that this can be ignored.";110111 Ok(command109 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy))112 .map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy, &iteration))113 .transpose()?114 .unwrap_or_default())115 }116
1 unmodified line
118 fn run_preparation_command(119 &self,120 command: &Command<'_>,121 output_policy: &CommandOutputPolicy,122 iteration: &executor::BenchmarkIteration,123 ) -> Result<Measurement> {124 let error_output = "The preparation command terminated with a non-zero exit code. \125 Append ' || true' to the command if you are sure that this can be ignored.";126123 self.run_intermediate_command(command, error_output, output_policy)127 self.run_intermediate_command(command, error_output, output_policy, iteration)128 }129130 /// Run the command specified by `--conclude`.131 fn run_conclusion_command(132 &self,133 command: &Command<'_>,134 output_policy: &CommandOutputPolicy,135 iteration: executor::BenchmarkIteration,136 ) -> Result<Measurement> {137 let error_output = "The conclusion command terminated with a non-zero exit code. \138 Append ' || true' to the command if you are sure that this can be ignored.";139135 self.run_intermediate_command(command, error_output, output_policy)140 self.run_intermediate_command(command, error_output, output_policy, &iteration)141 }142143 /// Run the benchmark for a single command144 pub fn run(&self) -> Result<BenchmarkResult> {
24 unmodified lines
169 self.command.get_parameters().iter().cloned(),170 )171 });172168 let run_preparation_command = || {173 let run_preparation_command = |iteration: &executor::BenchmarkIteration| {174 preparation_command175 .as_ref()171 .map(|cmd| self.run_preparation_command(cmd, output_policy))176 .map(|cmd| self.run_preparation_command(cmd, output_policy, iteration))177 .transpose()178 };179180 let conclusion_command = self.options.conclusion_command.as_ref().map(|values| {
7 unmodified lines
188 conclusion_command,189 self.command.get_parameters().iter().cloned(),190 )191 });187 let run_conclusion_command = || {192 let run_conclusion_command = |iteration: executor::BenchmarkIteration| {193 conclusion_command194 .as_ref()190 .map(|cmd| self.run_conclusion_command(cmd, output_policy))195 .map(|cmd| self.run_conclusion_command(cmd, output_policy, iteration))196 .transpose()197 };198194 self.run_setup_command(self.command.get_parameters().iter().cloned(), output_policy)?;199 self.run_setup_command(200 self.command.get_parameters().iter().cloned(),201 output_policy,202 executor::BenchmarkIteration::NonBenchmarkRun,203 )?;204205 // Warmup phase206 if self.options.warmup_count > 0 {207 let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {
6 unmodified lines
214 None215 };216217 for i in 0..self.options.warmup_count {218 let warmup_iteration = BenchmarkIteration::Warmup(i);209 let _ = run_preparation_command()?;219 let _ = run_preparation_command(&warmup_iteration)?;220 let _ = self.executor.run_command_and_measure(221 self.command,212 BenchmarkIteration::Warmup(i),222 &warmup_iteration,223 None,224 output_policy,225 )?;216 let _ = run_conclusion_command()?;226 let _ = run_conclusion_command(warmup_iteration)?;227 if let Some(bar) = progress_bar.as_ref() {228 bar.inc(1)229 }230 }
12 unmodified lines
243 } else {244 None245 };246247 let benchmark_iteration = BenchmarkIteration::Benchmark(0);237 let preparation_result = run_preparation_command()?;248 let preparation_result = run_preparation_command(&benchmark_iteration)?;249 let preparation_overhead = preparation_result.map_or(Time::zero(), |res| {250 res.time_wall_clock + self.executor.time_overhead()251 });252253 // Initial timing run254 let res = self.executor.run_command_and_measure(255 self.command,245 BenchmarkIteration::Benchmark(0),256 &benchmark_iteration,257 None,258 output_policy,259 )?;260 let success = res.exit_status.success();261251 let conclusion_result = run_conclusion_command()?;262 let conclusion_result = run_conclusion_command(benchmark_iteration)?;263 let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {264 res.time_wall_clock + self.executor.time_overhead()265 });266
32 unmodified lines
299 }300301 // Gather statistics (perform the actual benchmark)302 for i in 0..count_remaining {303 let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);292 run_preparation_command()?;304 run_preparation_command(&benchmark_iteration)?;305306 let msg = {307 let t_wall_clock_mean = measurements.time_wall_clock_mean();308 let time_unit = self
9 unmodified lines
318 }319320 let res = self.executor.run_command_and_measure(321 self.command,310 BenchmarkIteration::Benchmark(i + 1),322 &benchmark_iteration,323 None,324 output_policy,325 )?;326 let success = res.exit_status.success();
5 unmodified lines
332 if let Some(bar) = progress_bar.as_ref() {333 bar.inc(1)334 }335324 run_conclusion_command()?;336 run_conclusion_command(benchmark_iteration)?;337 }338339 if let Some(bar) = progress_bar.as_ref() {340 bar.finish_and_clear()
110 unmodified lines
451 if self.options.output_style != OutputStyleOption::Disabled {452 console_writeln!(io::stdout(), " ")?;453 }454443 self.run_cleanup_command(self.command.get_parameters().iter().cloned(), output_policy)?;455 self.run_cleanup_command(456 self.command.get_parameters().iter().cloned(),457 output_policy,458 executor::BenchmarkIteration::NonBenchmarkRun,459 )?;460461 Ok(BenchmarkResult {462 command: self.command.get_name(),463 command_with_unused_parameters: self.command.get_name_with_unused_parameters(),
34 unmodified lines
modified:tests/integration_tests.rs+116 added
1035 unmodified lines
1036 .arg("dir \"..\\src\\\" \"..\\tests\\\"")1037 .assert()1038 .success();1039}10401041#[cfg(unix)]1042#[test]1043fn hyperfine_iteration_env_var_in_prepare_command() {1044 use tempfile::tempdir;10451046 let tempdir = tempdir().unwrap();1047 let output_path = tempdir.path().join("iteration_output.txt");10481049 // Write HYPERFINE_ITERATION value to a file during prepare1050 hyperfine()1051 .arg("--runs=2")1052 .arg("--warmup=1")1053 .arg(format!(1054 "--prepare=echo $HYPERFINE_ITERATION >> {}",1055 output_path.to_string_lossy()1056 ))1057 .arg("echo test")1058 .assert()1059 .success();10601061 let contents = std::fs::read_to_string(output_path).unwrap();1062 let lines: Vec<&str> = contents.lines().collect();10631064 assert_eq!(lines.len(), 3);1065 assert_eq!(lines[0], "warmup-0");1066 assert_eq!(lines[1], "0");1067 assert_eq!(lines[2], "1");1068}10691070#[cfg(windows)]1071#[test]1072fn hyperfine_iteration_env_var_in_prepare_command() {1073 use tempfile::tempdir;10741075 let tempdir = tempdir().unwrap();1076 let output_path = tempdir.path().join("iteration_output.txt");10771078 // Write HYPERFINE_ITERATION value to a file during prepare1079 hyperfine()1080 .arg("--runs=2")1081 .arg("--warmup=1")1082 .arg(format!(1083 "--prepare=echo %HYPERFINE_ITERATION% >> {}",1084 output_path.to_string_lossy()1085 ))1086 .arg("echo test")1087 .assert()1088 .success();10891090 let contents = std::fs::read_to_string(output_path).unwrap();1091 let lines: Vec<String> = contents.lines().map(|l| l.trim().to_string()).collect();10921093 assert_eq!(lines.len(), 3);1094 assert_eq!(lines[0], "warmup-0");1095 assert_eq!(lines[1], "0");1096 assert_eq!(lines[2], "1");1097}10981099#[cfg(unix)]1100#[test]1101fn hyperfine_iteration_env_var_in_conclude_command() {1102 use tempfile::tempdir;11031104 let tempdir = tempdir().unwrap();1105 let output_path = tempdir.path().join("iteration_output.txt");11061107 // Write HYPERFINE_ITERATION value to a file during conclude1108 hyperfine()1109 .arg("--runs=2")1110 .arg("--warmup=1")1111 .arg(format!(1112 "--conclude=echo $HYPERFINE_ITERATION >> {}",1113 output_path.to_string_lossy()1114 ))1115 .arg("echo test")1116 .assert()1117 .success();11181119 let contents = std::fs::read_to_string(output_path).unwrap();1120 let lines: Vec<&str> = contents.lines().collect();11211122 assert_eq!(lines.len(), 3);1123 assert_eq!(lines[0], "warmup-0");1124 assert_eq!(lines[1], "0");1125 assert_eq!(lines[2], "1");1126}11271128#[cfg(windows)]1129#[test]1130fn hyperfine_iteration_env_var_in_conclude_command() {1131 use tempfile::tempdir;11321133 let tempdir = tempdir().unwrap();1134 let output_path = tempdir.path().join("iteration_output.txt");11351136 // Write HYPERFINE_ITERATION value to a file during conclude1137 hyperfine()1138 .arg("--runs=2")1139 .arg("--warmup=1")1140 .arg(format!(1141 "--conclude=echo %HYPERFINE_ITERATION% >> {}",1142 output_path.to_string_lossy()1143 ))1144 .arg("echo test")1145 .assert()1146 .success();11471148 let contents = std::fs::read_to_string(output_path).unwrap();1149 let lines: Vec<String> = contents.lines().map(|l| l.trim().to_string()).collect();11501151 assert_eq!(lines.len(), 3);1152 assert_eq!(lines[0], "warmup-0");1153 assert_eq!(lines[1], "0");1154 assert_eq!(lines[2], "1");1155}