Add --conclude option with tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This implementation of --conclude is basically identical to the existing implementation for --prepare. I have not adapted any existing tests. All tests which test --conclude functionality are new. The --conclude commands will run in the same order in which they are supplied on the cli in the same manner as --prepare and there must either be one --conclude command, or else the same number of --concludes as there are commands.
authored by Jack O'Connor · committed by David Peter · parent 91388e7 · Browse files · 5 files·+239 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+239 added−2 removed
- tests:+167 added−0 removed
- other:+72 added−2 removed
Provenance
- Commit
e13d0cb- Tree
252f513- Author
Jack O'Connor <github@jackoconnor.dev>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
91388e7
5 files changed+239 added−2 removed
modified:src/benchmark/mod.rs+38 added-2 removed
108 unmodified lines109 110 self.run_intermediate_command(command, error_output)111 }112 113+ /// Run the command specified by `--conclude`.114+ fn run_conclusion_command(&self, command: &Command<'_>) -> Result<TimingResult> {115+ let error_output = "The conclusion command terminated with a non-zero exit code. \116+ Append ' || true' to the command if you are sure that this can be ignored.";117+118+ self.run_intermediate_command(command, error_output)119+ }120+121 /// Run the benchmark for a single command122 pub fn run(&self) -> Result<BenchmarkResult> {123 if self.options.output_style != OutputStyleOption::Disabled {124 println!(28 unmodified lines153 .map(|cmd| self.run_preparation_command(cmd))154 .transpose()155 };156 157+ let conclusion_command = self.options.conclusion_command.as_ref().map(|values| {158+ let conclusion_command = if values.len() == 1 {159+ &values[0]160+ } else {161+ &values[self.number]162+ };163+ Command::new_parametrized(164+ None,165+ conclusion_command,166+ self.command.get_parameters().iter().cloned(),167+ )168+ });169+ let run_conclusion_command = || {170+ conclusion_command171+ .as_ref()172+ .map(|cmd| self.run_conclusion_command(cmd))173+ .transpose()174+ };175+176 self.run_setup_command(self.command.get_parameters().iter().cloned())?;177 178 // Warmup phase179 if self.options.warmup_count > 0 {9 unmodified lines189 190 for _ in 0..self.options.warmup_count {191 let _ = run_preparation_command()?;192 let _ = self.executor.run_command_and_measure(self.command, None)?;193+ let _ = run_conclusion_command()?;194 if let Some(bar) = progress_bar.as_ref() {195 bar.inc(1)196 }197 }20 unmodified lines218 // Initial timing run219 let (res, status) = self.executor.run_command_and_measure(self.command, None)?;220 let success = status.success();221 222+ let conclusion_result = run_conclusion_command()?;223+ let conclusion_overhead =224+ conclusion_result.map_or(0.0, |res| res.time_real + self.executor.time_overhead());225+226 // Determine number of benchmark runs227 let runs_in_min_time = (self.options.min_benchmarking_time196- / (res.time_real + self.executor.time_overhead() + preparation_overhead))197- as u64;228+ / (res.time_real229+ + self.executor.time_overhead()230+ + preparation_overhead231+ + conclusion_overhead)) as u64;232 233 let count = {234 let min = cmp::max(runs_in_min_time, self.options.run_bounds.min);235 48 unmodified lines284 285 if let Some(bar) = progress_bar.as_ref() {286 bar.inc(1)287 }288+289+ run_conclusion_command()?;290 }291 292 if let Some(bar) = progress_bar.as_ref() {293 bar.finish_and_clear()133 unmodified linesmodified:src/cli.rs+17 added
101 unmodified lines102 be run prior to the corresponding benchmark command.",103 ),104 )105 .arg(106+ Arg::new("conclude")107+ .long("conclude")108+ .short('C')109+ .action(ArgAction::Append)110+ .num_args(1)111+ .value_name("CMD")112+ .value_hint(ValueHint::CommandString)113+ .help(114+ "Execute CMD after each timing run. This is useful for killing \115+ long-running processes started (e.g. a web server started in --prepare), \116+ for example.\nThe --conclude option can be specified once for all \117+ commands or multiple times, once for each command. In the latter case, \118+ each conclude command will be run after the corresponding benchmark \119+ command.",120+ ),121+ )122+ .arg(123 Arg::new("cleanup")124 .long("cleanup")125 .short('c')126 .action(ArgAction::Set)260 unmodified linesmodified:src/options.rs+17 added
205 unmodified lines206 207 /// Command(s) to run before each timing run208 pub preparation_command: Option<Vec<String>>,209 210+ /// Command(s) to run after each timing run211+ pub conclusion_command: Option<Vec<String>>,212+213 /// Command to run before each *batch* of timing runs, i.e. before each individual benchmark214 pub setup_command: Option<String>,215 216 /// Command to run after each *batch* of timing runs, i.e. after each individual benchmark28 unmodified lines245 warmup_count: 0,246 min_benchmarking_time: 3.0,247 command_failure_action: CmdFailureAction::RaiseError,248 preparation_command: None,249+ conclusion_command: None,250 setup_command: None,251 cleanup_command: None,252 output_style: OutputStyleOption::Full,253 sort_order_speed_comparison: SortOrder::MeanTime,53 unmodified lines307 options.preparation_command = matches308 .get_many::<String>("prepare")309 .map(|values| values.map(String::from).collect::<Vec<String>>());310 311+ options.conclusion_command = matches312+ .get_many::<String>("conclude")313+ .map(|values| values.map(String::from).collect::<Vec<String>>());314+315 options.cleanup_command = matches.get_one::<String>("cleanup").map(String::from);316 317 options.command_output_policy = if matches.get_flag("show-output") {318 CommandOutputPolicy::Inherit120 unmodified lines439 number of benchmark commands."440 );441 }442 443+ if let Some(conclusion_command) = &self.conclusion_command {444+ ensure!(445+ conclusion_command.len() <= 1446+ || commands.num_commands() == conclusion_command.len(),447+ "The '--conclude' option has to be provided just once or N times, where N is the \448+ number of benchmark commands."449+ );450+ }451+452 Ok(())453 }454 }455 43 unmodified linesmodified:tests/execution_order_tests.rs+88 added
52 unmodified lines53 self.arg("--prepare");54 self.command(output)55 }56 57+ fn conclude(&mut self, output: &str) -> &mut Self {58+ self.arg("--conclude");59+ self.command(output)60+ }61+62 fn cleanup(&mut self, output: &str) -> &mut Self {63 self.arg("--cleanup");64 self.command(output)65 }99 unmodified lines165 .run();166 }167 168 #[test]169+fn conclude_commands_are_executed_after_each_timing_run() {170+ ExecutionOrderTest::new()171+ .arg("--runs=2")172+ .conclude("conclude")173+ .command("command 1")174+ .command("command 2")175+ .expect_output("command 1")176+ .expect_output("conclude")177+ .expect_output("command 1")178+ .expect_output("conclude")179+ .expect_output("command 2")180+ .expect_output("conclude")181+ .expect_output("command 2")182+ .expect_output("conclude")183+ .run();184+}185+186+#[test]187 fn prepare_commands_are_executed_before_each_warmup() {188 ExecutionOrderTest::new()189 .arg("--warmup=2")190 .arg("--runs=1")19 unmodified lines210 .run();211 }212 213 #[test]214+fn conclude_commands_are_executed_after_each_warmup() {215+ ExecutionOrderTest::new()216+ .arg("--warmup=2")217+ .arg("--runs=1")218+ .conclude("conclude")219+ .command("command 1")220+ .command("command 2")221+ // warmup 1222+ .expect_output("command 1")223+ .expect_output("conclude")224+ .expect_output("command 1")225+ .expect_output("conclude")226+ // benchmark 1227+ .expect_output("command 1")228+ .expect_output("conclude")229+ // warmup 2230+ .expect_output("command 2")231+ .expect_output("conclude")232+ .expect_output("command 2")233+ .expect_output("conclude")234+ // benchmark 2235+ .expect_output("command 2")236+ .expect_output("conclude")237+ .run();238+}239+240+#[test]241 fn cleanup_commands_are_executed_once_after_each_benchmark() {242 ExecutionOrderTest::new()243 .arg("--runs=2")244 .cleanup("cleanup")39 unmodified lines284 .run();285 }286 287 #[test]288+fn setup_prepare_conclude_cleanup_combined() {289+ ExecutionOrderTest::new()290+ .arg("--warmup=1")291+ .arg("--runs=2")292+ .setup("setup")293+ .prepare("prepare")294+ .command("command1")295+ .command("command2")296+ .conclude("conclude")297+ .cleanup("cleanup")298+ // 1299+ .expect_output("setup")300+ .expect_output("prepare")301+ .expect_output("command1")302+ .expect_output("conclude")303+ .expect_output("prepare")304+ .expect_output("command1")305+ .expect_output("conclude")306+ .expect_output("prepare")307+ .expect_output("command1")308+ .expect_output("conclude")309+ .expect_output("cleanup")310+ // 2311+ .expect_output("setup")312+ .expect_output("prepare")313+ .expect_output("command2")314+ .expect_output("conclude")315+ .expect_output("prepare")316+ .expect_output("command2")317+ .expect_output("conclude")318+ .expect_output("prepare")319+ .expect_output("command2")320+ .expect_output("conclude")321+ .expect_output("cleanup")322+ .run();323+}324+325+#[test]326 fn single_parameter_value() {327 ExecutionOrderTest::new()328 .arg("--runs=2")329 .arg("--parameter-list")37 unmodified linesmodified:tests/integration_tests.rs+79 added
74 unmodified lines75 ));76 }77 78 #[test]79+fn fails_with_wrong_number_of_conclude_options() {80+ hyperfine()81+ .arg("--runs=1")82+ .arg("--conclude=echo a")83+ .arg("--conclude=echo b")84+ .arg("echo a")85+ .arg("echo b")86+ .assert()87+ .success();88+89+ hyperfine()90+ .arg("--runs=1")91+ .arg("--conclude=echo a")92+ .arg("--conclude=echo b")93+ .arg("echo a")94+ .arg("echo b")95+ .arg("echo c")96+ .assert()97+ .failure()98+ .stderr(predicate::str::contains(99+ "The '--conclude' option has to be provided",100+ ));101+}102+103+#[test]104 fn fails_with_duplicate_parameter_names() {105 hyperfine()106 .arg("--parameter-list")107 .arg("x")83 unmodified lines191 "The preparation command terminated with a non-zero exit code.",192 ));193 }194 195+#[test]196+fn fails_for_unknown_conclude_command() {197+ hyperfine()198+ .arg("--conclude=some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")199+ .arg("echo test")200+ .assert()201+ .failure()202+ .stderr(predicate::str::contains(203+ "The conclusion command terminated with a non-zero exit code.",204+ ));205+}206+207 #[cfg(unix)]208 #[test]209 fn can_run_failing_commands_with_ignore_failure_option() {210 hyperfine()146 unmodified lines357 .stdout(predicate::str::contains("30 runs"));358 }359 360 #[test]361+fn takes_conclusion_command_into_account_for_computing_number_of_runs() {362+ hyperfine_debug()363+ .arg("--conclude=sleep 0.02")364+ .arg("sleep 0.01")365+ .assert()366+ .success()367+ .stdout(predicate::str::contains("100 runs"));368+369+ // Shell overhead needs to be added to both the conclude command and the actual command,370+ // leading to a total benchmark time of (cmd + shell + conclude + shell = 0.1 s)371+ hyperfine_debug()372+ .arg("--shell=sleep 0.01")373+ .arg("--conclude=sleep 0.03")374+ .arg("sleep 0.05")375+ .assert()376+ .success()377+ .stdout(predicate::str::contains("30 runs"));378+}379+380+#[test]381+fn takes_both_preparation_and_conclusion_command_into_account_for_computing_number_of_runs() {382+ hyperfine_debug()383+ .arg("--prepare=sleep 0.01")384+ .arg("--conclude=sleep 0.01")385+ .arg("sleep 0.01")386+ .assert()387+ .success()388+ .stdout(predicate::str::contains("100 runs"));389+390+ // Shell overhead needs to be added to both the prepare, conclude and the actual command,391+ // leading to a total benchmark time of (prepare + shell + cmd + shell + conclude + shell = 0.1 s)392+ hyperfine_debug()393+ .arg("--shell=sleep 0.01")394+ .arg("--prepare=sleep 0.01")395+ .arg("--conclude=sleep 0.01")396+ .arg("sleep 0.05")397+ .assert()398+ .success()399+ .stdout(predicate::str::contains("30 runs"));400+}401+402+#[test]403 fn shows_benchmark_comparison_with_relative_times() {404 hyperfine_debug()405 .arg("sleep 1.0")406 .arg("sleep 2.0")125 unmodified lines