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
Provenance
- Commit
308bf5c- Tree
4495e7f- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f19223b
1 file changed+39 added−27 removed
modified:src/benchmark/executor.rs+39 added-27 removed
1 use std::process::{ExitStatus, Stdio};2 3 use crate::command::Command;4-use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell};4+use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};5 use crate::output::progress_bar::get_progress_bar;6-use crate::timer::execute_and_measure;6+use crate::timer::{execute_and_measure, TimerResult};7 use crate::util::randomized_environment_offset;8 use crate::util::units::Second;9 10 use super::timing_result::TimingResult;21 unmodified lines32 /// of the command.33 fn time_overhead(&self) -> Second;34 }35 36+fn 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);44+45+ command.env(46+ "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",47+ randomized_environment_offset::value(),48+ );49+50+ let result = execute_and_measure(command)51+ .with_context(|| format!("Failed to run command '{}'", command_name))?;52+53+ 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+ }63+64+ Ok(result)65+}66+67 pub struct ShellExecutor<'a> {68 options: &'a Options,69 shell: &'a Shell,70 shell_spawning_time: Option<TimingResult>,14 unmodified lines85 &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();59-89 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());93 72- 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+ )?;100 75- 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- }88-101 // 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