Move custom functionality to executor

This commit is contained in:
David Peter 2022-02-22 11:23:53 +01:00 committed by David Peter
parent f822455d99
commit 1f3943e7d1
2 changed files with 21 additions and 44 deletions

View File

@ -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;

View File

@ -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<ExecuteResult> {
pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {
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<ExecuteResult> {
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<ExecuteResult> {
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()?;