Skip to content
Myr

Extract common functionality

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 f19223b · Browse files · 1 file+39 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+39 added−27 removed

  • other:+39 added−27 removed

1 file+39 added−27 removed

modified:src/benchmark/executor.rs+39 added-27 removed
1use std::process::{ExitStatus, Stdio};23use crate::command::Command;4use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell};4use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};5use crate::output::progress_bar::get_progress_bar;6use crate::timer::execute_and_measure;6use crate::timer::{execute_and_measure, TimerResult};7use crate::util::randomized_environment_offset;8use crate::util::units::Second;910use super::timing_result::TimingResult;
21 unmodified lines
32 /// of the command.33 fn time_overhead(&self) -> Second;34}3536fn run_command_and_measure_common(37 mut command: std::process::Command,38 command_failure_action: CmdFailureAction,39 command_output_policy: CommandOutputPolicy,40 command_name: &str,41) -> Result<TimerResult> {42 let (stdout, stderr) = command_output_policy.get_stdout_stderr();43 command.stdin(Stdio::null()).stdout(stdout).stderr(stderr);4445 command.env(46 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",47 randomized_environment_offset::value(),48 );4950 let result = execute_and_measure(command)51 .with_context(|| format!("Failed to run command '{}'", command_name))?;5253 if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {54 bail!(55 "{}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \56 Alternatively, use the '--show-output' option to debug what went wrong.",57 result.status.code().map_or(58 "The process has been terminated by a signal".into(),59 |c| format!("Command terminated with non-zero exit code: {}", c)60 )61 );62 }6364 Ok(result)65}6667pub struct ShellExecutor<'a> {68 options: &'a Options,69 shell: &'a Shell,70 shell_spawning_time: Option<TimingResult>,
14 unmodified lines
85 &self,86 command: &Command<'_>,87 command_failure_action: Option<CmdFailureAction>,88 ) -> Result<(TimingResult, ExitStatus)> {58 let (stdout, stderr) = self.options.command_output_policy.get_stdout_stderr();5989 let mut command_builder = self.shell.command();90 command_builder62 .stdin(Stdio::null())63 .stdout(stdout)64 .stderr(stderr)65 .env(66 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",67 randomized_environment_offset::value(),68 )91 .arg(if cfg!(windows) { "/C" } else { "-c" })92 .arg(command.get_command_line());9372 let mut result = execute_and_measure(command_builder)73 .with_context(|| format!("Failed to run command '{}'", command.get_command_line()))?;94 let mut result = run_command_and_measure_common(95 command_builder,96 command_failure_action.unwrap_or(self.options.command_failure_action),97 self.options.command_output_policy,98 &command.get_command_line(),99 )?;10075 if command_failure_action.unwrap_or(self.options.command_failure_action)76 == CmdFailureAction::RaiseError77 && !result.status.success()78 {79 bail!(80 "{}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \81 Alternatively, use the '--show-output' option to debug what went wrong.",82 result.status.code().map_or(83 "The process has been terminated by a signal".into(),84 |c| format!("Command terminated with non-zero exit code: {}", c)85 )86 );87 }88101 // Subtract shell spawning time102 if let Some(spawning_time) = self.shell_spawning_time {103 result.time_real = (result.time_real - spawning_time.time_real).max(0.0);104 result.time_user = (result.time_user - spawning_time.time_user).max(0.0);
141 unmodified lines