execution_order_tests.rs
This file
Path
tests/execution_order_tests.rs
540 lines · 14.6 KiB · Rust
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
1use std::{fs::File, io::Read, path::PathBuf};23use tempfile::{tempdir, TempDir};45mod common;6use common::hyperfine;78struct ExecutionOrderTest {9 cmd: assert_cmd::Command,10 expected_content: String,11 logfile_path: PathBuf,12 #[allow(dead_code)]13 tempdir: TempDir,14}1516impl ExecutionOrderTest {17 fn new() -> Self {18 let tempdir = tempdir().unwrap();19 let logfile_path = tempdir.path().join("output.log");20 let mut cmd = hyperfine();21 cmd.arg("-S");2223 ExecutionOrderTest {24 cmd,25 expected_content: String::new(),26 logfile_path,27 tempdir,28 }29 }3031 fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {32 self.cmd.arg(arg.as_ref());33 self34 }3536 fn get_command(&self, output: &str) -> String {37 format!(38 "echo {output} >> {path}",39 output = output,40 path = self.logfile_path.to_string_lossy()41 )42 }4344 fn command(&mut self, output: &str) -> &mut Self {45 self.arg(self.get_command(output));46 self47 }4849 fn setup(&mut self, output: &str) -> &mut Self {50 self.arg("--setup");51 self.command(output)52 }5354 fn prepare(&mut self, output: &str) -> &mut Self {55 self.arg("--prepare");56 self.command(output)57 }5859 fn conclude(&mut self, output: &str) -> &mut Self {60 self.arg("--conclude");61 self.command(output)62 }6364 fn cleanup(&mut self, output: &str) -> &mut Self {65 self.arg("--cleanup");66 self.command(output)67 }6869 fn expect_output(&mut self, output: &str) -> &mut Self {70 self.expected_content.push_str(output);7172 #[cfg(windows)]73 {74 self.expected_content.push_str(" \r");75 }7677 self.expected_content.push('\n');78 self79 }8081 fn run(&mut self) {82 self.cmd.assert().success();8384 let mut f = File::open(&self.logfile_path).unwrap();85 let mut content = String::new();86 f.read_to_string(&mut content).unwrap();8788 assert_eq!(content, self.expected_content);89 }90}9192impl Default for ExecutionOrderTest {93 fn default() -> Self {94 Self::new()95 }96}9798#[test]99fn benchmarks_are_executed_sequentially_one() {100 ExecutionOrderTest::new()101 .arg("--runs=1")102 .command("command 1")103 .command("command 2")104 .expect_output("command 1")105 .expect_output("command 2")106 .run();107}108109#[test]110fn benchmarks_are_executed_sequentially() {111 ExecutionOrderTest::new()112 .arg("--runs=2")113 .command("command 1")114 .command("command 2")115 .expect_output("command 1")116 .expect_output("command 1")117 .expect_output("command 2")118 .expect_output("command 2")119 .run();120}121122#[test]123fn warmup_runs_are_executed_before_benchmarking_runs() {124 ExecutionOrderTest::new()125 .arg("--runs=2")126 .arg("--warmup=3")127 .command("command 1")128 .expect_output("command 1")129 .expect_output("command 1")130 .expect_output("command 1")131 .expect_output("command 1")132 .expect_output("command 1")133 .run();134}135136#[test]137fn setup_commands_are_executed_before_each_series_of_timing_runs() {138 ExecutionOrderTest::new()139 .arg("--runs=2")140 .setup("setup")141 .command("command 1")142 .command("command 2")143 .expect_output("setup")144 .expect_output("command 1")145 .expect_output("command 1")146 .expect_output("setup")147 .expect_output("command 2")148 .expect_output("command 2")149 .run();150}151152#[test]153fn prepare_commands_are_executed_before_each_timing_run() {154 ExecutionOrderTest::new()155 .arg("--runs=2")156 .prepare("prepare")157 .command("command 1")158 .command("command 2")159 .expect_output("prepare")160 .expect_output("command 1")161 .expect_output("prepare")162 .expect_output("command 1")163 .expect_output("prepare")164 .expect_output("command 2")165 .expect_output("prepare")166 .expect_output("command 2")167 .run();168}169170#[test]171fn conclude_commands_are_executed_after_each_timing_run() {172 ExecutionOrderTest::new()173 .arg("--runs=2")174 .conclude("conclude")175 .command("command 1")176 .command("command 2")177 .expect_output("command 1")178 .expect_output("conclude")179 .expect_output("command 1")180 .expect_output("conclude")181 .expect_output("command 2")182 .expect_output("conclude")183 .expect_output("command 2")184 .expect_output("conclude")185 .run();186}187188#[test]189fn prepare_commands_are_executed_before_each_warmup() {190 ExecutionOrderTest::new()191 .arg("--warmup=2")192 .arg("--runs=1")193 .prepare("prepare")194 .command("command 1")195 .command("command 2")196 // warmup 1197 .expect_output("prepare")198 .expect_output("command 1")199 .expect_output("prepare")200 .expect_output("command 1")201 // benchmark 1202 .expect_output("prepare")203 .expect_output("command 1")204 // warmup 2205 .expect_output("prepare")206 .expect_output("command 2")207 .expect_output("prepare")208 .expect_output("command 2")209 // benchmark 2210 .expect_output("prepare")211 .expect_output("command 2")212 .run();213}214215#[test]216fn conclude_commands_are_executed_after_each_warmup() {217 ExecutionOrderTest::new()218 .arg("--warmup=2")219 .arg("--runs=1")220 .conclude("conclude")221 .command("command 1")222 .command("command 2")223 // warmup 1224 .expect_output("command 1")225 .expect_output("conclude")226 .expect_output("command 1")227 .expect_output("conclude")228 // benchmark 1229 .expect_output("command 1")230 .expect_output("conclude")231 // warmup 2232 .expect_output("command 2")233 .expect_output("conclude")234 .expect_output("command 2")235 .expect_output("conclude")236 // benchmark 2237 .expect_output("command 2")238 .expect_output("conclude")239 .run();240}241242#[test]243fn cleanup_commands_are_executed_once_after_each_benchmark() {244 ExecutionOrderTest::new()245 .arg("--runs=2")246 .cleanup("cleanup")247 .command("command 1")248 .command("command 2")249 .expect_output("command 1")250 .expect_output("command 1")251 .expect_output("cleanup")252 .expect_output("command 2")253 .expect_output("command 2")254 .expect_output("cleanup")255 .run();256}257258#[test]259fn setup_prepare_cleanup_combined() {260 ExecutionOrderTest::new()261 .arg("--warmup=1")262 .arg("--runs=2")263 .setup("setup")264 .prepare("prepare")265 .command("command1")266 .command("command2")267 .cleanup("cleanup")268 // 1269 .expect_output("setup")270 .expect_output("prepare")271 .expect_output("command1")272 .expect_output("prepare")273 .expect_output("command1")274 .expect_output("prepare")275 .expect_output("command1")276 .expect_output("cleanup")277 // 2278 .expect_output("setup")279 .expect_output("prepare")280 .expect_output("command2")281 .expect_output("prepare")282 .expect_output("command2")283 .expect_output("prepare")284 .expect_output("command2")285 .expect_output("cleanup")286 .run();287}288289#[test]290fn setup_prepare_conclude_cleanup_combined() {291 ExecutionOrderTest::new()292 .arg("--warmup=1")293 .arg("--runs=2")294 .setup("setup")295 .prepare("prepare")296 .command("command1")297 .command("command2")298 .conclude("conclude")299 .cleanup("cleanup")300 // 1301 .expect_output("setup")302 .expect_output("prepare")303 .expect_output("command1")304 .expect_output("conclude")305 .expect_output("prepare")306 .expect_output("command1")307 .expect_output("conclude")308 .expect_output("prepare")309 .expect_output("command1")310 .expect_output("conclude")311 .expect_output("cleanup")312 // 2313 .expect_output("setup")314 .expect_output("prepare")315 .expect_output("command2")316 .expect_output("conclude")317 .expect_output("prepare")318 .expect_output("command2")319 .expect_output("conclude")320 .expect_output("prepare")321 .expect_output("command2")322 .expect_output("conclude")323 .expect_output("cleanup")324 .run();325}326327#[test]328fn single_parameter_value() {329 ExecutionOrderTest::new()330 .arg("--runs=2")331 .arg("--parameter-list")332 .arg("number")333 .arg("1,2,3")334 .command("command {number}")335 .expect_output("command 1")336 .expect_output("command 1")337 .expect_output("command 2")338 .expect_output("command 2")339 .expect_output("command 3")340 .expect_output("command 3")341 .run();342}343344#[test]345fn multiple_parameter_values() {346 ExecutionOrderTest::new()347 .arg("--runs=2")348 .arg("--parameter-list")349 .arg("number")350 .arg("1,2,3")351 .arg("--parameter-list")352 .arg("letter")353 .arg("a,b")354 .command("command {number} {letter}")355 .expect_output("command 1 a")356 .expect_output("command 1 a")357 .expect_output("command 2 a")358 .expect_output("command 2 a")359 .expect_output("command 3 a")360 .expect_output("command 3 a")361 .expect_output("command 1 b")362 .expect_output("command 1 b")363 .expect_output("command 2 b")364 .expect_output("command 2 b")365 .expect_output("command 3 b")366 .expect_output("command 3 b")367 .run();368}369370#[test]371fn commands_execute_in_input_order() {372 ExecutionOrderTest::new()373 .arg("--runs=1")374 .command("reference")375 .command("command 1")376 .command("command 2")377 .expect_output("reference")378 .expect_output("command 1")379 .expect_output("command 2")380 .run();381}382383#[test]384fn repeated_commands_execute_separately() {385 ExecutionOrderTest::new()386 .arg("--runs=1")387 .command("command 1")388 .command("command 1")389 .command("command 2")390 .expect_output("command 1")391 .expect_output("command 1")392 .expect_output("command 2")393 .run();394}395396#[test]397fn setup_prepare_reference_conclude_cleanup_combined() {398 ExecutionOrderTest::new()399 .arg("--warmup=1")400 .arg("--runs=2")401 .setup("setup")402 .prepare("prepare")403 .command("reference")404 .command("command1")405 .command("command2")406 .conclude("conclude")407 .cleanup("cleanup")408 // reference409 .expect_output("setup")410 .expect_output("prepare")411 .expect_output("reference")412 .expect_output("conclude")413 .expect_output("prepare")414 .expect_output("reference")415 .expect_output("conclude")416 .expect_output("prepare")417 .expect_output("reference")418 .expect_output("conclude")419 .expect_output("cleanup")420 // 1421 .expect_output("setup")422 .expect_output("prepare")423 .expect_output("command1")424 .expect_output("conclude")425 .expect_output("prepare")426 .expect_output("command1")427 .expect_output("conclude")428 .expect_output("prepare")429 .expect_output("command1")430 .expect_output("conclude")431 .expect_output("cleanup")432 // 2433 .expect_output("setup")434 .expect_output("prepare")435 .expect_output("command2")436 .expect_output("conclude")437 .expect_output("prepare")438 .expect_output("command2")439 .expect_output("conclude")440 .expect_output("prepare")441 .expect_output("command2")442 .expect_output("conclude")443 .expect_output("cleanup")444 .run();445}446447#[test]448fn setup_separate_prepare_separate_conclude_cleanup_combined() {449 ExecutionOrderTest::new()450 .arg("--warmup=1")451 .arg("--runs=2")452 .setup("setup")453 .cleanup("cleanup")454 .prepare("prepare1")455 .command("command1")456 .conclude("conclude1")457 .prepare("prepare2")458 .command("command2")459 .conclude("conclude2")460 // 1461 .expect_output("setup")462 .expect_output("prepare1")463 .expect_output("command1")464 .expect_output("conclude1")465 .expect_output("prepare1")466 .expect_output("command1")467 .expect_output("conclude1")468 .expect_output("prepare1")469 .expect_output("command1")470 .expect_output("conclude1")471 .expect_output("cleanup")472 // 2473 .expect_output("setup")474 .expect_output("prepare2")475 .expect_output("command2")476 .expect_output("conclude2")477 .expect_output("prepare2")478 .expect_output("command2")479 .expect_output("conclude2")480 .expect_output("prepare2")481 .expect_output("command2")482 .expect_output("conclude2")483 .expect_output("cleanup")484 .run();485}486487#[test]488fn setup_separate_prepare_reference_separate_conclude_cleanup_combined() {489 ExecutionOrderTest::new()490 .arg("--warmup=1")491 .arg("--runs=2")492 .setup("setup")493 .cleanup("cleanup")494 .prepare("prepareref")495 .command("reference")496 .conclude("concluderef")497 .prepare("prepare1")498 .command("command1")499 .conclude("conclude1")500 .prepare("prepare2")501 .command("command2")502 .conclude("conclude2")503 // reference504 .expect_output("setup")505 .expect_output("prepareref")506 .expect_output("reference")507 .expect_output("concluderef")508 .expect_output("prepareref")509 .expect_output("reference")510 .expect_output("concluderef")511 .expect_output("prepareref")512 .expect_output("reference")513 .expect_output("concluderef")514 .expect_output("cleanup")515 // 1516 .expect_output("setup")517 .expect_output("prepare1")518 .expect_output("command1")519 .expect_output("conclude1")520 .expect_output("prepare1")521 .expect_output("command1")522 .expect_output("conclude1")523 .expect_output("prepare1")524 .expect_output("command1")525 .expect_output("conclude1")526 .expect_output("cleanup")527 // 2528 .expect_output("setup")529 .expect_output("prepare2")530 .expect_output("command2")531 .expect_output("conclude2")532 .expect_output("prepare2")533 .expect_output("command2")534 .expect_output("conclude2")535 .expect_output("prepare2")536 .expect_output("command2")537 .expect_output("conclude2")538 .expect_output("cleanup")539 .run();540}