Skip to content

Commit

Permalink
Add environment randomization on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Feb 22, 2022
1 parent cb6f2f7 commit 197e5df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

- Breaking change: the `-s` short option for `--style` is now used for
the new `--setup` option.
- The environment offset randomization is now also available on Windows,
see #484

## Bugfixes

Expand Down
76 changes: 31 additions & 45 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,8 @@ pub struct ExecuteResult {
pub status: ExitStatus,
}

/// Execute the given command and return a timing summary
#[cfg(windows)]
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &Shell,
) -> Result<ExecuteResult> {
let mut child = run_shell_command(stdout, stderr, command, shell)?;
let cpu_timer = get_cpu_timer(&child);
let status = child.wait()?;

let (user_time, system_time) = cpu_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
status,
})
fn randomized_environment_offset_value() -> String {
"X".repeat(rand::random::<usize>() % 4096usize)
}

/// Execute the given command and return a timing summary
Expand All @@ -48,55 +32,57 @@ pub fn execute_and_time(
) -> Result<ExecuteResult> {
let cpu_timer = get_cpu_timer();

let status = run_shell_command(stdout, stderr, command, shell)?;

let (user_time, system_time) = cpu_timer.stop();

Ok(ExecuteResult {
user_time,
system_time,
status,
})
}

/// Run a standard shell command using `sh -c`
#[cfg(not(windows))]
fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &Shell,
) -> Result<std::process::ExitStatus> {
shell
let status = shell
.command()
.arg("-c")
.arg(command)
.env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
"X".repeat(rand::random::<usize>() % 4096usize),
randomized_environment_offset_value(),
)
.stdin(Stdio::null())
.stdout(stdout)
.stderr(stderr)
.status()
.with_context(|| format!("Failed to run command '{}'", command))
.with_context(|| format!("Failed to run command '{}'", command))?;

let (user_time, system_time) = cpu_timer.stop();

Ok(ExecuteResult {
user_time,
system_time,
status,
})
}

/// Run a Windows shell command using `cmd.exe /C`
/// Execute the given command and return a timing summary
#[cfg(windows)]
fn run_shell_command(
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &Shell,
) -> Result<std::process::Child> {
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))
.with_context(|| format!("Failed to run command '{}'", command))?;
let cpu_timer = get_cpu_timer(&child);
let status = child.wait()?;

let (user_time, system_time) = cpu_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
status,
})
}

0 comments on commit 197e5df

Please sign in to comment.