Proper shell overhead computation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 6f51232 · Browse files · 5 files·+40 added−22 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+40 added−22 removed
- tests:+21 added−4 removed
- other:+19 added−18 removed
Provenance
- Commit
6887835- Tree
da74d2d- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6f51232
5 files changed+40 added−22 removed
modified:src/benchmark/executor.rs+8 added-8 removed
163 unmodified lines164 }165 }166 167 #[derive(Clone)]168-pub struct MockExecutor<'a> {169- shell: &'a Shell,168+pub struct MockExecutor {169+ shell: Option<String>,170 }171 172-impl<'a> MockExecutor<'a> {173- pub fn new(shell: &'a Shell) -> Self {172+impl MockExecutor {173+ pub fn new(shell: Option<String>) -> Self {174 MockExecutor { shell }175 }176 177 fn extract_time<S: AsRef<str>>(sleep_command: S) -> Second {5 unmodified lines183 .unwrap()184 }185 }186 187-impl<'a> Executor for MockExecutor<'a> {187+impl Executor for MockExecutor {188 fn time_command(189 &self,190 command: &Command<'_>,191 _command_failure_action: Option<CmdFailureAction>,24 unmodified lines216 Ok(())217 }218 219 fn time_overhead(&self) -> Second {220- match self.shell {221- Shell::Default(_) => 0.0,222- Shell::Custom(shell) => Self::extract_time(&shell[0]),220+ match &self.shell {221+ None => 0.0,222+ Some(shell) => Self::extract_time(shell),223 }224 }225 }226 4 unmodified linesmodified:src/benchmark/mod.rs+7 added-6 removed
139 unmodified lines140 self.command.get_parameters().iter().cloned(),141 )142 });143 let run_preparation_command = || {144- if let Some(ref cmd) = preparation_command {145- self.run_preparation_command(cmd)146- } else {147- Ok(TimingResult::default())148- }144+ preparation_command145+ .as_ref()146+ .map(|cmd| self.run_preparation_command(cmd))147+ .transpose()148 };149 150 self.run_setup_command(self.command.get_parameters().iter().cloned())?;151 32 unmodified lines184 None185 };186 187 let preparation_result = run_preparation_command()?;188+ let preparation_overhead =189+ preparation_result.map_or(0.0, |res| res.time_real + self.executor.time_overhead());190 191 // Initial timing run192 let (res, status) = self.executor.time_command(self.command, None)?;193 let success = status.success();194 195 // Determine number of benchmark runs196 let runs_in_min_time = (self.options.min_benchmarking_time196- / (res.time_real + preparation_result.time_real + self.executor.time_overhead()))197+ / (res.time_real + self.executor.time_overhead() + preparation_overhead))198 as u64;199 200 let count = {201 let min = cmp::max(runs_in_min_time, self.options.run_bounds.min);172 unmodified linesmodified:src/benchmark/scheduler.rs+1 added-1 removed
31 unmodified lines32 }33 34 pub fn run_benchmarks(&mut self) -> Result<()> {35 let mut executor: Box<dyn Executor> = match self.options.executor_kind {36- ExecutorKind::Mock(ref shell) => Box::new(MockExecutor::new(shell)),36+ ExecutorKind::Mock(ref shell) => Box::new(MockExecutor::new(shell.clone())),37 ExecutorKind::Shell(ref shell) => Box::new(ShellExecutor::new(shell, &self.options)),38 };39 40 executor.calibrate()?;56 unmodified linesmodified:src/options.rs+3 added-3 removed
124 unmodified lines125 }126 127 pub enum ExecutorKind {128 Shell(Shell),129- Mock(Shell),129+ Mock(Option<String>),130 }131 132 impl Default for ExecutorKind {133 fn default() -> Self {141 unmodified lines275 options.executor_kind = match (matches.is_present("debug-mode"), matches.value_of("shell"))276 {277 (false, Some(shell)) => ExecutorKind::Shell(Shell::from_str(shell)?),278 (false, None) => ExecutorKind::Shell(Shell::default()),279- (true, Some(shell)) => ExecutorKind::Mock(Shell::from_str(shell)?),280- (true, None) => ExecutorKind::Mock(Shell::default()),279+ (true, Some(shell)) => ExecutorKind::Mock(Some(shell.into())),280+ (true, None) => ExecutorKind::Mock(None),281 };282 283 if matches.is_present("ignore-failure") {284 options.command_failure_action = CmdFailureAction::Ignore;68 unmodified linesmodified:tests/integration_tests.rs+21 added-4 removed
187 unmodified lines188 189 #[test]190 fn performs_ten_runs_for_slow_commands() {191 hyperfine_debug()192- .arg("--time-unit=millisecond")192 .arg("sleep 0.5")193 .assert()194 .success()195 .stdout(predicate::str::contains("10 runs"));1 unmodified line197 198 #[test]199 fn performs_three_seconds_of_benchmarking_for_fast_commands() {200 hyperfine_debug()202- .arg("--time-unit=millisecond")201 .arg("sleep 0.01")202 .assert()203 .success()204 .stdout(predicate::str::contains("300 runs"));205 }206 207 #[test]210-fn takes_time_of_preparation_command_into_account() {208+fn takes_shell_spawning_time_into_account_for_computing_number_of_runs() {209 hyperfine_debug()212- .arg("--time-unit=millisecond")210+ .arg("--shell=sleep 0.02")211+ .arg("sleep 0.01")212+ .assert()213+ .success()214+ .stdout(predicate::str::contains("100 runs"));215+}216+217+#[test]218+fn takes_preparation_command_into_account_for_computing_number_of_runs() {219+ hyperfine_debug()220 .arg("--prepare=sleep 0.02")221 .arg("sleep 0.01")222 .assert()223 .success()224 .stdout(predicate::str::contains("100 runs"));225+226+ // Shell overhead needs to be added to both the prepare command and the actual command,227+ // leading to a total benchmark time of (prepare + shell + cmd + shell = 0.1 s)228+ hyperfine_debug()229+ .arg("--shell=sleep 0.01")230+ .arg("--prepare=sleep 0.03")231+ .arg("sleep 0.05")232+ .assert()233+ .success()234+ .stdout(predicate::str::contains("30 runs"));235 }236 237 #[test]238 fn shows_benchmark_comparison_with_relative_times() {32 unmodified lines