Skip to content
Myr

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 file

Take it

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

line 1why, one step back
1use std::process::{ExitStatus, Stdio};

Flat directory/module structureDavid Peterbefore adoption30c381f

line 2why, one step back
2

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

line 3why, one step back
3use crate::options::Shell;

Flat directory/module structureDavid Peterbefore adoption30c381f

line 4why, one step back
4

Unified error handlingDavid Peterbefore adoption2040810

lines 5–6why, one step back
5use anyhow::{Context, Result};
6

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 7–19why, one step back
7/// Used to indicate the result of running a command
8#[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}19

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 20–21why, one step back
20fn randomized_environment_offset_value() -> String {
21 "X".repeat(rand::random::<usize>() % 4096usize)

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 22–29why, one step back
22}
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 back
30 shell: &Shell,

Unified error handlingDavid Peterbefore adoption2040810

line 31why, one step back
31) -> Result<ExecuteResult> {

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 32why, one step back
32 let cpu_timer = crate::timer::unix_timer::CPUTimer::start();

Flat directory/module structureDavid Peterbefore adoption30c381f

line 33why, one step back
33

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

line 34why, one step back
34 let status = shell

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

line 35why, one step back
35 .command()

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 36–39why, one step back
36 .arg("-c")
37 .arg(command)38 .env(39 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

line 40why, one step back
40 randomized_environment_offset_value(),

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 41–45why, one step back
41 )
42 .stdin(Stdio::null())43 .stdout(stdout)44 .stderr(stderr)45 .status()

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 46–54why, one step back
46 .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 back
55}
56

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

line 57why, one step back
57/// Execute the given command and return a timing summary

Flat directory/module structureDavid Peterbefore adoption30c381f

line 58why, one step back
58#[cfg(windows)]

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

line 59why, one step back
59pub fn execute_and_time(

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 60–62why, one step back
60 stdout: Stdio,
61 stderr: Stdio,62 command: &str,

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

line 63why, one step back
63 shell: &Shell,

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 64–65why, one step back
64) -> Result<ExecuteResult> {
65 let mut child = shell

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

line 66why, one step back
66 .command()

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 67–68why, one step back
67 .arg("/C")
68 .arg(command)

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 69–72why, one step back
69 .env(
70 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",71 randomized_environment_offset_value(),72 )

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 73–76why, one step back
73 .stdin(Stdio::null())
74 .stdout(stdout)75 .stderr(stderr)76 .spawn()

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

line 77why, one step back
77 .with_context(|| format!("Failed to run command '{}'", command))?;

Refactor timer moduleDavid Peterbefore adoptiondefafdf

line 78why, one step back
78 let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 79–86why, one step back
79 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 back
87}