Fix prepare and conclude behaviour and add tests.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jan-Eric Nitschke · committed by David Peter · parent 9806ed7 · Browse files · 4 files·+306 added−6 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+306 added−6 removed
- tests:+282 added−0 removed
- other:+24 added−6 removed
Provenance
- Commit
d4749b0- Tree
1755d56- Author
Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
9806ed7
4 files changed+306 added−6 removed
modified:src/benchmark/relative_speed.rs+14 added
154 unmodified lines155 assert_relative_eq!(2.5, annotated_results[2].relative_speed);156 }157 158 #[test]159+fn test_compute_relative_speed_with_reference() {160+ use approx::assert_relative_eq;161+162+ let results = vec![create_result("cmd2", 2.0), create_result("cmd3", 5.0)];163+ let reference = create_result("cmd2", 4.0);164+165+ let annotated_results =166+ compute_with_check_from_reference(&results, &reference, SortOrder::Command).unwrap();167+168+ assert_relative_eq!(2.0, annotated_results[0].relative_speed);169+ assert_relative_eq!(1.25, annotated_results[1].relative_speed);170+}171+172+#[test]173 fn test_compute_relative_speed_for_zero_times() {174 let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];175 176 let annotated_results = compute_with_check(&results, SortOrder::Command);3 unmodified linesmodified:src/options.rs+10 added-6 removed
435 unmodified lines436 Ok(options)437 }438 439 pub fn validate_against_command_list(&self, commands: &Commands) -> Result<()> {440+ let num_commands = commands.num_commands()441+ + if self.reference_command.is_some() {442+ 1443+ } else {444+ 0445+ };446 if let Some(preparation_command) = &self.preparation_command {447 ensure!(442- preparation_command.len() <= 1443- || commands.num_commands() == preparation_command.len(),448+ preparation_command.len() <= 1 || num_commands == preparation_command.len(),449 "The '--prepare' option has to be provided just once or N times, where N is the \445- number of benchmark commands."450+ number of benchmark commands including a potential reference."451 );452 }453 454 if let Some(conclusion_command) = &self.conclusion_command {455 ensure!(451- conclusion_command.len() <= 1452- || commands.num_commands() == conclusion_command.len(),456+ conclusion_command.len() <= 1 || num_commands == conclusion_command.len(),457 "The '--conclude' option has to be provided just once or N times, where N is the \454- number of benchmark commands."458+ number of benchmark commands including a potential reference."459 );460 }461 462 Ok(())46 unmodified linesmodified:tests/execution_order_tests.rs+197 added
52 unmodified lines53 self.arg("--prepare");54 self.command(output)55 }56 57+ fn reference(&mut self, output: &str) -> &mut Self {58+ self.arg("--reference");59+ self.command(output)60+ }61+62 fn conclude(&mut self, output: &str) -> &mut Self {63 self.arg("--conclude");64 self.command(output)65 }302 unmodified lines368 .expect_output("command 3 b")369 .expect_output("command 3 b")370 .run();371 }372+373+#[test]374+fn reference_is_executed_first() {375+ ExecutionOrderTest::new()376+ .arg("--runs=1")377+ .reference("reference")378+ .command("command 1")379+ .command("command 2")380+ .expect_output("reference")381+ .expect_output("command 1")382+ .expect_output("command 2")383+ .run();384+}385+386+#[test]387+fn reference_is_executed_first_parameter_value() {388+ ExecutionOrderTest::new()389+ .arg("--runs=2")390+ .reference("reference")391+ .arg("--parameter-list")392+ .arg("number")393+ .arg("1,2,3")394+ .command("command {number}")395+ .expect_output("reference")396+ .expect_output("reference")397+ .expect_output("command 1")398+ .expect_output("command 1")399+ .expect_output("command 2")400+ .expect_output("command 2")401+ .expect_output("command 3")402+ .expect_output("command 3")403+ .run();404+}405+406+#[test]407+fn reference_is_executed_separately_from_commands() {408+ ExecutionOrderTest::new()409+ .arg("--runs=1")410+ .reference("command 1")411+ .command("command 1")412+ .command("command 2")413+ .expect_output("command 1")414+ .expect_output("command 1")415+ .expect_output("command 2")416+ .run();417+}418+419+#[test]420+fn setup_prepare_reference_conclude_cleanup_combined() {421+ ExecutionOrderTest::new()422+ .arg("--warmup=1")423+ .arg("--runs=2")424+ .setup("setup")425+ .prepare("prepare")426+ .reference("reference")427+ .command("command1")428+ .command("command2")429+ .conclude("conclude")430+ .cleanup("cleanup")431+ // reference432+ .expect_output("setup")433+ .expect_output("prepare")434+ .expect_output("reference")435+ .expect_output("conclude")436+ .expect_output("prepare")437+ .expect_output("reference")438+ .expect_output("conclude")439+ .expect_output("prepare")440+ .expect_output("reference")441+ .expect_output("conclude")442+ .expect_output("cleanup")443+ // 1444+ .expect_output("setup")445+ .expect_output("prepare")446+ .expect_output("command1")447+ .expect_output("conclude")448+ .expect_output("prepare")449+ .expect_output("command1")450+ .expect_output("conclude")451+ .expect_output("prepare")452+ .expect_output("command1")453+ .expect_output("conclude")454+ .expect_output("cleanup")455+ // 2456+ .expect_output("setup")457+ .expect_output("prepare")458+ .expect_output("command2")459+ .expect_output("conclude")460+ .expect_output("prepare")461+ .expect_output("command2")462+ .expect_output("conclude")463+ .expect_output("prepare")464+ .expect_output("command2")465+ .expect_output("conclude")466+ .expect_output("cleanup")467+ .run();468+}469+470+#[test]471+fn setup_separete_prepare_separate_conclude_cleanup_combined() {472+ ExecutionOrderTest::new()473+ .arg("--warmup=1")474+ .arg("--runs=2")475+ .setup("setup")476+ .cleanup("cleanup")477+ .prepare("prepare1")478+ .command("command1")479+ .conclude("conclude1")480+ .prepare("prepare2")481+ .command("command2")482+ .conclude("conclude2")483+ // 1484+ .expect_output("setup")485+ .expect_output("prepare1")486+ .expect_output("command1")487+ .expect_output("conclude1")488+ .expect_output("prepare1")489+ .expect_output("command1")490+ .expect_output("conclude1")491+ .expect_output("prepare1")492+ .expect_output("command1")493+ .expect_output("conclude1")494+ .expect_output("cleanup")495+ // 2496+ .expect_output("setup")497+ .expect_output("prepare2")498+ .expect_output("command2")499+ .expect_output("conclude2")500+ .expect_output("prepare2")501+ .expect_output("command2")502+ .expect_output("conclude2")503+ .expect_output("prepare2")504+ .expect_output("command2")505+ .expect_output("conclude2")506+ .expect_output("cleanup")507+ .run();508+}509+510+#[test]511+fn setup_separete_prepare_reference_separate_conclude_cleanup_combined() {512+ ExecutionOrderTest::new()513+ .arg("--warmup=1")514+ .arg("--runs=2")515+ .setup("setup")516+ .cleanup("cleanup")517+ .prepare("prepareref")518+ .reference("reference")519+ .conclude("concluderef")520+ .prepare("prepare1")521+ .command("command1")522+ .conclude("conclude1")523+ .prepare("prepare2")524+ .command("command2")525+ .conclude("conclude2")526+ // reference527+ .expect_output("setup")528+ .expect_output("prepareref")529+ .expect_output("reference")530+ .expect_output("concluderef")531+ .expect_output("prepareref")532+ .expect_output("reference")533+ .expect_output("concluderef")534+ .expect_output("prepareref")535+ .expect_output("reference")536+ .expect_output("concluderef")537+ .expect_output("cleanup")538+ // 1539+ .expect_output("setup")540+ .expect_output("prepare1")541+ .expect_output("command1")542+ .expect_output("conclude1")543+ .expect_output("prepare1")544+ .expect_output("command1")545+ .expect_output("conclude1")546+ .expect_output("prepare1")547+ .expect_output("command1")548+ .expect_output("conclude1")549+ .expect_output("cleanup")550+ // 2551+ .expect_output("setup")552+ .expect_output("prepare2")553+ .expect_output("command2")554+ .expect_output("conclude2")555+ .expect_output("prepare2")556+ .expect_output("command2")557+ .expect_output("conclude2")558+ .expect_output("prepare2")559+ .expect_output("command2")560+ .expect_output("conclude2")561+ .expect_output("cleanup")562+ .run();563+}modified:tests/integration_tests.rs+85 added
61 unmodified lines62 .success();63 64 hyperfine()65 .arg("--runs=1")66+ .arg("--prepare=echo ref")67 .arg("--prepare=echo a")68 .arg("--prepare=echo b")69+ .arg("--reference=echo ref")70 .arg("echo a")71 .arg("echo b")72+ .assert()73+ .success();74+75+ hyperfine()76+ .arg("--runs=1")77+ .arg("--prepare=echo a")78+ .arg("--prepare=echo b")79+ .arg("echo a")80+ .arg("echo b")81 .arg("echo c")82 .assert()83 .failure()84 .stderr(predicate::str::contains(85 "The '--prepare' option has to be provided",86 ));87+88+ hyperfine()89+ .arg("--runs=1")90+ .arg("--prepare=echo a")91+ .arg("--prepare=echo b")92+ .arg("--reference=echo ref")93+ .arg("echo a")94+ .arg("echo b")95+ .assert()96+ .failure()97+ .stderr(predicate::str::contains(98+ "The '--prepare' option has to be provided",99+ ));100 }101 102 #[test]103 fn fails_with_wrong_number_of_conclude_options() {7 unmodified lines111 .success();112 113 hyperfine()114 .arg("--runs=1")115+ .arg("--conclude=echo ref")116 .arg("--conclude=echo a")117 .arg("--conclude=echo b")118+ .arg("--reference=echo ref")119 .arg("echo a")120 .arg("echo b")121+ .assert()122+ .success();123+124+ hyperfine()125+ .arg("--runs=1")126+ .arg("--conclude=echo a")127+ .arg("--conclude=echo b")128+ .arg("echo a")129+ .arg("echo b")130 .arg("echo c")131 .assert()132 .failure()133 .stderr(predicate::str::contains(134 "The '--conclude' option has to be provided",135 ));136+137+ hyperfine()138+ .arg("--runs=1")139+ .arg("--conclude=echo a")140+ .arg("--conclude=echo b")141+ .arg("--reference=echo ref")142+ .arg("echo a")143+ .arg("echo b")144+ .assert()145+ .failure()146+ .stderr(predicate::str::contains(147+ "The '--conclude' option has to be provided",148+ ));149 }150 151 #[test]152 fn fails_with_duplicate_parameter_names() {308 unmodified lines461 );462 }463 464 #[test]465+fn shows_benchmark_comparison_relative_to_reference() {466+ hyperfine_debug()467+ .arg("--reference=sleep 2.0")468+ .arg("sleep 1.0")469+ .arg("sleep 3.0")470+ .assert()471+ .success()472+ .stdout(473+ predicate::str::contains("2.00 ± 0.00 times slower")474+ .and(predicate::str::contains("1.50 ± 0.00 times faster")),475+ );476+}477+478+#[test]479 fn performs_all_benchmarks_in_parameter_scan() {480 hyperfine_debug()481 .arg("--parameter-scan")482 .arg("time")13 unmodified lines496 );497 }498 499 #[test]500+fn performs_reference_and_all_benchmarks_in_parameter_scan() {501+ hyperfine_debug()502+ .arg("--reference=sleep 25")503+ .arg("--parameter-scan")504+ .arg("time")505+ .arg("30")506+ .arg("45")507+ .arg("--parameter-step-size")508+ .arg("5")509+ .arg("sleep {time}")510+ .assert()511+ .success()512+ .stdout(513+ predicate::str::contains("Benchmark 1: sleep 25")514+ .and(predicate::str::contains("Benchmark 2: sleep 30"))515+ .and(predicate::str::contains("Benchmark 3: sleep 35"))516+ .and(predicate::str::contains("Benchmark 4: sleep 40"))517+ .and(predicate::str::contains("Benchmark 5: sleep 45"))518+ .and(predicate::str::contains("Benchmark 6: sleep 50").not()),519+ );520+}521+522+#[test]523 fn intermediate_results_are_not_exported_to_stdout() {524 hyperfine_debug()525 .arg("--style=none") // To only see the Markdown export on stdout526 .arg("--export-markdown")90 unmodified lines