Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment randomization on Windows #484

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
})
}