Skip to content
Myr

shell.rs

This file

Path

src/shell.rs

88 lines · 2.1 KiB · Rust

Last changed by

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

lines 4–5why, one step back
4use crate::timer::get_cpu_timer;
5

Unified error handlingDavid Peterbefore adoption2040810

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 8–20why, one step back
8/// Used to indicate the result of running a command
9#[derive(Debug, Copy, Clone)]10pub struct ExecuteResult {11 /// The amount of user time the process used12 pub user_time: f64,1314 /// The amount of cpu time the process used15 pub system_time: f64,1617 /// The exit status of the process18 pub status: ExitStatus,19}20

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 23–30why, one step back
23}
2425/// Execute the given command and return a timing summary26#[cfg(not(windows))]27pub fn execute_and_time(28 stdout: Stdio,29 stderr: Stdio,30 command: &str,

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

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

Unified error handlingDavid Peterbefore adoption2040810

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

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 33–34why, one step back
33 let cpu_timer = get_cpu_timer();
34

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 47–55why, one step back
47 .with_context(|| format!("Failed to run command '{}'", command))?;
4849 let (user_time, system_time) = cpu_timer.stop();5051 Ok(ExecuteResult {52 user_time,53 system_time,54 status,55 })

Flat directory/module structureDavid Peterbefore adoption30c381f

lines 56–57why, one step back
56}
57

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Parse --shell option value as shell commandrhysdbefore adoptione2fdace

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

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

Flat directory/module structureDavid Peterbefore adoption30c381f

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

Add environment randomization on WindowsDavid Peterbefore adoption197e5df

lines 78–87why, one step back
78 .with_context(|| format!("Failed to run command '{}'", command))?;
79 let cpu_timer = get_cpu_timer(&child);80 let status = child.wait()?;8182 let (user_time, system_time) = cpu_timer.stop();83 Ok(ExecuteResult {84 user_time,85 system_time,86 status,87 })

Flat directory/module structureDavid Peterbefore adoption30c381f

line 88why, one step back
88}