Skip to content
Myr

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

5 files+40 added−22 removed

modified:src/benchmark/executor.rs+8 added-8 removed
163 unmodified lines
164 }165}166167#[derive(Clone)]168pub struct MockExecutor<'a> {169 shell: &'a Shell,168pub struct MockExecutor {169 shell: Option<String>,170}171172impl<'a> MockExecutor<'a> {173 pub fn new(shell: &'a Shell) -> Self {172impl MockExecutor {173 pub fn new(shell: Option<String>) -> Self {174 MockExecutor { shell }175 }176177 fn extract_time<S: AsRef<str>>(sleep_command: S) -> Second {
5 unmodified lines
183 .unwrap()184 }185}186187impl<'a> Executor for MockExecutor<'a> {187impl Executor for MockExecutor {188 fn time_command(189 &self,190 command: &Command<'_>,191 _command_failure_action: Option<CmdFailureAction>,
24 unmodified lines
216 Ok(())217 }218219 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 lines
modified:src/benchmark/mod.rs+7 added-6 removed
139 unmodified lines
140 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 };149150 self.run_setup_command(self.command.get_parameters().iter().cloned())?;151
32 unmodified lines
184 None185 };186187 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());190191 // Initial timing run192 let (res, status) = self.executor.time_command(self.command, None)?;193 let success = status.success();194195 // 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;199200 let count = {201 let min = cmp::max(runs_in_min_time, self.options.run_bounds.min);
172 unmodified lines
modified:src/benchmark/scheduler.rs+1 added-1 removed
31 unmodified lines
32 }3334 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 };3940 executor.calibrate()?;
56 unmodified lines
modified:src/options.rs+3 added-3 removed
124 unmodified lines
125}126127pub enum ExecutorKind {128 Shell(Shell),129 Mock(Shell),129 Mock(Option<String>),130}131132impl Default for ExecutorKind {133 fn default() -> Self {
141 unmodified lines
275 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 };282283 if matches.is_present("ignore-failure") {284 options.command_failure_action = CmdFailureAction::Ignore;
68 unmodified lines
modified:tests/integration_tests.rs+21 added-4 removed
187 unmodified lines
188189#[test]190fn 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 line
197198#[test]199fn 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}206207#[test]210fn takes_time_of_preparation_command_into_account() {208fn 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}216217#[test]218fn 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"));225226 // 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}236237#[test]238fn shows_benchmark_comparison_with_relative_times() {
32 unmodified lines