From 1f3943e7d12b50e48123ea826b2b8bc7ad8682d5 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 22 Feb 2022 11:23:53 +0100 Subject: [PATCH] Move custom functionality to executor --- src/benchmark/executor.rs | 16 ++++++++++++- src/execute.rs | 49 +++++---------------------------------- 2 files changed, 21 insertions(+), 44 deletions(-) diff --git a/src/benchmark/executor.rs b/src/benchmark/executor.rs index ec89ace..2780df0 100644 --- a/src/benchmark/executor.rs +++ b/src/benchmark/executor.rs @@ -5,6 +5,7 @@ use crate::execute::execute_and_measure; use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell}; use crate::output::progress_bar::get_progress_bar; use crate::timer::wallclocktimer::WallClockTimer; +use crate::util::randomized_environment_offset; use crate::util::units::Second; use super::timing_result::TimingResult; @@ -68,8 +69,21 @@ impl<'a> Executor for ShellExecutor<'a> { CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()), }; + let mut command_builder = self.shell.command(); + command_builder + .arg(if cfg!(windows) { "/C" } else { "-c" }) + .arg(command.get_command_line()) + .env( + "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", + randomized_environment_offset::value(), + ) + .stdin(Stdio::null()) + .stdout(stdout) + .stderr(stderr); + let error_message = format!("Failed to run command '{}'", command.get_command_line()); + let wallclock_timer = WallClockTimer::start(); - let result = execute_and_measure(stdout, stderr, &command.get_command_line(), self.shell)?; + let result = execute_and_measure(command_builder, &error_message)?; let mut time_real = wallclock_timer.stop(); let mut time_user = result.user_time; diff --git a/src/execute.rs b/src/execute.rs index 7c1bf8f..7d8487e 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -1,7 +1,4 @@ -use std::process::{ExitStatus, Stdio}; - -use crate::options::Shell; -use crate::util::randomized_environment_offset; +use std::process::{Command, ExitStatus}; use anyhow::{Context, Result}; @@ -20,28 +17,11 @@ pub struct ExecuteResult { /// Execute the given command and return a timing summary #[cfg(not(windows))] -pub fn execute_and_measure( - stdout: Stdio, - stderr: Stdio, - command: &str, - shell: &Shell, -) -> Result { +pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result { let cpu_timer = crate::timer::unix_timer::CPUTimer::start(); - - let status = shell - .command() - .arg("-c") - .arg(command) - .env( - "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", - randomized_environment_offset::value(), - ) - .stdin(Stdio::null()) - .stdout(stdout) - .stderr(stderr) + let status = command .status() - .with_context(|| format!("Failed to run command '{}'", command))?; - + .with_context(|| error_message.to_string())?; let (user_time, system_time) = cpu_timer.stop(); Ok(ExecuteResult { @@ -53,25 +33,8 @@ pub fn execute_and_measure( /// Execute the given command and return a timing summary #[cfg(windows)] -pub fn execute_and_measure( - stdout: Stdio, - stderr: Stdio, - command: &str, - shell: &Shell, -) -> Result { - let mut child = shell - .command() - .arg("/C") - .arg(command) - .env( - "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", - randomized_environment_offset::value(), - ) - .stdin(Stdio::null()) - .stdout(stdout) - .stderr(stderr) - .spawn() - .with_context(|| format!("Failed to run command '{}'", command))?; +pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result { + let mut child = command.spawn().with_context(|| error_message.to_string())?; let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child); let status = child.wait()?;