execution_order_tests.rs
This file
Path
tests/execution_order_tests.rs
278 lines · 7.1 KiB · Rust
Last changed by
Expand execution order testsDavid Peterbefore adoption5df981a
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");2021 ExecutionOrderTest {22 cmd: hyperfine(),23 expected_content: String::new(),24 logfile_path,25 tempdir,26 }27 }2829 fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {30 self.cmd.arg(arg.as_ref());31 self32 }3334 fn get_command(&self, output: &str) -> String {35 format!(36 "echo {output} >> {path}",37 output = output,38 path = self.logfile_path.to_string_lossy()39 )40 }4142 fn command(&mut self, output: &str) -> &mut Self {43 self.arg(self.get_command(output));44 self45 }4647 fn setup(&mut self, output: &str) -> &mut Self {48 self.arg("--setup");49 self.command(output)50 }5152 fn prepare(&mut self, output: &str) -> &mut Self {53 self.arg("--prepare");54 self.command(output)55 }5657 fn cleanup(&mut self, output: &str) -> &mut Self {58 self.arg("--cleanup");59 self.command(output)60 }6162 fn expect_output(&mut self, output: &str) -> &mut Self {63 self.expected_content.push_str(output);6465 #[cfg(windows)]66 {67 self.expected_content.push_str(" \r");68 }6970 self.expected_content.push('\n');71 self72 }7374 fn run(&mut self) {75 self.cmd.assert().success();7677 let mut f = File::open(&self.logfile_path).unwrap();78 let mut content = String::new();79 f.read_to_string(&mut content).unwrap();8081 assert_eq!(content, self.expected_content);82 }83}8485impl Default for ExecutionOrderTest {86 fn default() -> Self {87 Self::new()88 }89}9091#[test]92fn benchmarks_are_executed_sequentially_one() {93 ExecutionOrderTest::new()94 .arg("--runs=1")95 .command("command 1")96 .command("command 2")97 .expect_output("command 1")98 .expect_output("command 2")99 .run();100}101102#[test]103fn benchmarks_are_executed_sequentially() {104 ExecutionOrderTest::new()105 .arg("--runs=2")106 .command("command 1")107 .command("command 2")108 .expect_output("command 1")109 .expect_output("command 1")110 .expect_output("command 2")111 .expect_output("command 2")112 .run();113}114115#[test]116fn warmup_runs_are_executed_before_benchmarking_runs() {117 ExecutionOrderTest::new()118 .arg("--runs=2")119 .arg("--warmup=3")120 .command("command 1")121 .expect_output("command 1")122 .expect_output("command 1")123 .expect_output("command 1")124 .expect_output("command 1")125 .expect_output("command 1")126 .run();127}128129#[test]130fn setup_commands_are_executed_before_each_series_of_timing_runs() {131 ExecutionOrderTest::new()132 .arg("--runs=2")133 .setup("setup")134 .command("command 1")135 .command("command 2")136 .expect_output("setup")137 .expect_output("command 1")138 .expect_output("command 1")139 .expect_output("setup")140 .expect_output("command 2")141 .expect_output("command 2")142 .run();143}144145#[test]146fn prepare_commands_are_executed_before_each_timing_run() {147 ExecutionOrderTest::new()148 .arg("--runs=2")149 .prepare("prepare")150 .command("command 1")151 .command("command 2")152 .expect_output("prepare")153 .expect_output("command 1")154 .expect_output("prepare")155 .expect_output("command 1")156 .expect_output("prepare")157 .expect_output("command 2")158 .expect_output("prepare")159 .expect_output("command 2")160 .run();161}162163#[test]164fn prepare_commands_are_executed_before_each_warmup() {165 ExecutionOrderTest::new()166 .arg("--warmup=2")167 .arg("--runs=1")168 .prepare("prepare")169 .command("command 1")170 .command("command 2")171 // warmup 1172 .expect_output("prepare")173 .expect_output("command 1")174 .expect_output("prepare")175 .expect_output("command 1")176 // benchmark 1177 .expect_output("prepare")178 .expect_output("command 1")179 // warmup 2180 .expect_output("prepare")181 .expect_output("command 2")182 .expect_output("prepare")183 .expect_output("command 2")184 // benchmark 2185 .expect_output("prepare")186 .expect_output("command 2")187 .run();188}189190#[test]191fn cleanup_commands_are_executed_once_after_each_benchmark() {192 ExecutionOrderTest::new()193 .arg("--runs=2")194 .cleanup("cleanup")195 .command("command 1")196 .command("command 2")197 .expect_output("command 1")198 .expect_output("command 1")199 .expect_output("cleanup")200 .expect_output("command 2")201 .expect_output("command 2")202 .expect_output("cleanup")203 .run();204}205206#[test]207fn setup_prepare_cleanup_combined() {208 ExecutionOrderTest::new()209 .arg("--warmup=1")210 .arg("--runs=2")211 .setup("setup")212 .prepare("prepare")213 .command("command1")214 .command("command2")215 .cleanup("cleanup")216 // 1217 .expect_output("setup")218 .expect_output("prepare")219 .expect_output("command1")220 .expect_output("prepare")221 .expect_output("command1")222 .expect_output("prepare")223 .expect_output("command1")224 .expect_output("cleanup")225 // 2226 .expect_output("setup")227 .expect_output("prepare")228 .expect_output("command2")229 .expect_output("prepare")230 .expect_output("command2")231 .expect_output("prepare")232 .expect_output("command2")233 .expect_output("cleanup")234 .run();235}236237#[test]238fn single_parameter_value() {239 ExecutionOrderTest::new()240 .arg("--runs=2")241 .arg("--parameter-list")242 .arg("number")243 .arg("1,2,3")244 .command("command {number}")245 .expect_output("command 1")246 .expect_output("command 1")247 .expect_output("command 2")248 .expect_output("command 2")249 .expect_output("command 3")250 .expect_output("command 3")251 .run();252}253254#[test]255fn multiple_parameter_values() {256 ExecutionOrderTest::new()257 .arg("--runs=2")258 .arg("--parameter-list")259 .arg("number")260 .arg("1,2,3")261 .arg("--parameter-list")262 .arg("letter")263 .arg("a,b")264 .command("command {number} {letter}")265 .expect_output("command 1 a")266 .expect_output("command 1 a")267 .expect_output("command 2 a")268 .expect_output("command 2 a")269 .expect_output("command 3 a")270 .expect_output("command 3 a")271 .expect_output("command 1 b")272 .expect_output("command 1 b")273 .expect_output("command 2 b")274 .expect_output("command 2 b")275 .expect_output("command 3 b")276 .expect_output("command 3 b")277 .run();278}