shell.rs
This file
Path
src/shell.rs
87 lines · 2.1 KiB · Rust
Last changed by
Refactor timer moduleDavid Peterbefore adoptiondefafdf
History of this fileTake it
Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 1why, one step back1use std::process::{ExitStatus, Stdio};Flat directory/module structureDavid Peterbefore adoption30c381f
line 2why, one step back2Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 3why, one step back3use crate::options::Shell;Flat directory/module structureDavid Peterbefore adoption30c381f
line 4why, one step back4Unified error handlingDavid Peterbefore adoption2040810
lines 5–6why, one step back5use anyhow::{Context, Result};6Flat directory/module structureDavid Peterbefore adoption30c381f
lines 7–19why, one step back7/// Used to indicate the result of running a command8#[derive(Debug, Copy, Clone)]9pub struct ExecuteResult {10 /// The amount of user time the process used11 pub user_time: f64,1213 /// The amount of cpu time the process used14 pub system_time: f64,1516 /// The exit status of the process17 pub status: ExitStatus,18}19Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 20–21why, one step back20fn randomized_environment_offset_value() -> String {21 "X".repeat(rand::random::<usize>() % 4096usize)Flat directory/module structureDavid Peterbefore adoption30c381f
lines 22–29why, one step back22}2324/// Execute the given command and return a timing summary25#[cfg(not(windows))]26pub fn execute_and_time(27 stdout: Stdio,28 stderr: Stdio,29 command: &str,Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 30why, one step back30 shell: &Shell,Unified error handlingDavid Peterbefore adoption2040810
line 31why, one step back31) -> Result<ExecuteResult> {Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 32why, one step back32 let cpu_timer = crate::timer::unix_timer::CPUTimer::start();Flat directory/module structureDavid Peterbefore adoption30c381f
line 33why, one step back33Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 34why, one step back34 let status = shellParse --shell option value as shell commandrhysdbefore adoptione2fdace
line 35why, one step back35 .command()Flat directory/module structureDavid Peterbefore adoption30c381f
lines 36–39why, one step back36 .arg("-c")37 .arg(command)38 .env(39 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 40why, one step back40 randomized_environment_offset_value(),Flat directory/module structureDavid Peterbefore adoption30c381f
lines 41–45why, one step back41 )42 .stdin(Stdio::null())43 .stdout(stdout)44 .stderr(stderr)45 .status()Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 46–54why, one step back46 .with_context(|| format!("Failed to run command '{}'", command))?;4748 let (user_time, system_time) = cpu_timer.stop();4950 Ok(ExecuteResult {51 user_time,52 system_time,53 status,54 })Flat directory/module structureDavid Peterbefore adoption30c381f
lines 55–56why, one step back55}56Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 57why, one step back57/// Execute the given command and return a timing summaryFlat directory/module structureDavid Peterbefore adoption30c381f
line 58why, one step back58#[cfg(windows)]Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 59why, one step back59pub fn execute_and_time(Flat directory/module structureDavid Peterbefore adoption30c381f
lines 60–62why, one step back60 stdout: Stdio,61 stderr: Stdio,62 command: &str,Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 63why, one step back63 shell: &Shell,Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 64–65why, one step back64) -> Result<ExecuteResult> {65 let mut child = shellParse --shell option value as shell commandrhysdbefore adoptione2fdace
line 66why, one step back66 .command()Flat directory/module structureDavid Peterbefore adoption30c381f
lines 67–68why, one step back67 .arg("/C")68 .arg(command)Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 69–72why, one step back69 .env(70 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",71 randomized_environment_offset_value(),72 )Flat directory/module structureDavid Peterbefore adoption30c381f
lines 73–76why, one step back73 .stdin(Stdio::null())74 .stdout(stdout)75 .stderr(stderr)76 .spawn()Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 77why, one step back77 .with_context(|| format!("Failed to run command '{}'", command))?;Refactor timer moduleDavid Peterbefore adoptiondefafdf
line 78why, one step back78 let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 79–86why, one step back79 let status = child.wait()?;8081 let (user_time, system_time) = cpu_timer.stop();82 Ok(ExecuteResult {83 user_time,84 system_time,85 status,86 })Flat directory/module structureDavid Peterbefore adoption30c381f
line 87why, one step back87}