Move custom functionality to executor
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 f822455 · Browse files · 2 files·+21 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+21 added−44 removed
- other:+21 added−44 removed
Provenance
- Commit
1f3943e- Tree
697454d- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f822455
2 files changed+21 added−44 removed
modified:src/benchmark/executor.rs+15 added-1 removed
3 unmodified lines4 use crate::execute::execute_and_measure;5 use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};6 use crate::output::progress_bar::get_progress_bar;7 use crate::timer::wallclocktimer::WallClockTimer;8+use crate::util::randomized_environment_offset;9 use crate::util::units::Second;10 11 use super::timing_result::TimingResult;12 55 unmodified lines68 CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),69 CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),70 };71 72+ let mut command_builder = self.shell.command();73+ command_builder74+ .arg(if cfg!(windows) { "/C" } else { "-c" })75+ .arg(command.get_command_line())76+ .env(77+ "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",78+ randomized_environment_offset::value(),79+ )80+ .stdin(Stdio::null())81+ .stdout(stdout)82+ .stderr(stderr);83+ let error_message = format!("Failed to run command '{}'", command.get_command_line());84+85 let wallclock_timer = WallClockTimer::start();72- let result = execute_and_measure(stdout, stderr, &command.get_command_line(), self.shell)?;86+ let result = execute_and_measure(command_builder, &error_message)?;87 let mut time_real = wallclock_timer.stop();88 89 let mut time_user = result.user_time;90 let mut time_system = result.system_time;160 unmodified linesmodified:src/execute.rs+6 added-43 removed
1-use std::process::{ExitStatus, Stdio};1+use std::process::{Command, ExitStatus};2 3-use crate::options::Shell;4-use crate::util::randomized_environment_offset;5-3 use anyhow::{Context, Result};4 5 /// Used to indicate the result of running a command6 #[derive(Debug, Copy, Clone)]9 unmodified lines16 }17 18 /// Execute the given command and return a timing summary19 #[cfg(not(windows))]23-pub fn execute_and_measure(24- stdout: Stdio,25- stderr: Stdio,26- command: &str,27- shell: &Shell,28-) -> Result<ExecuteResult> {20+pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {21 let cpu_timer = crate::timer::unix_timer::CPUTimer::start();30-31- let status = shell22+ let status = command32- .command()33- .arg("-c")34- .arg(command)35- .env(36- "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",37- randomized_environment_offset::value(),38- )39- .stdin(Stdio::null())40- .stdout(stdout)41- .stderr(stderr)23 .status()43- .with_context(|| format!("Failed to run command '{}'", command))?;44-24+ .with_context(|| error_message.to_string())?;25 let (user_time, system_time) = cpu_timer.stop();26 27 Ok(ExecuteResult {28 user_time,3 unmodified lines32 }33 34 /// Execute the given command and return a timing summary35 #[cfg(windows)]56-pub fn execute_and_measure(57- stdout: Stdio,58- stderr: Stdio,59- command: &str,60- shell: &Shell,61-) -> Result<ExecuteResult> {62- let mut child = shell63- .command()64- .arg("/C")65- .arg(command)66- .env(67- "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",68- randomized_environment_offset::value(),69- )70- .stdin(Stdio::null())71- .stdout(stdout)72- .stderr(stderr)73- .spawn()74- .with_context(|| format!("Failed to run command '{}'", command))?;36+pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {37+ let mut child = command.spawn().with_context(|| error_message.to_string())?;38 let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);39 let status = child.wait()?;40 41 let (user_time, system_time) = cpu_timer.stop();6 unmodified lines