Add environment randomization on Windows

This commit is contained in:
David Peter authored and David Peter committed 2022-02-22 09:42:31 +01:00
1 parent cb6f2f7c4e
commit 197e5df9f0
2 files changed
+34 -46

No files matched your search

+2
View File
@@ -9,6 +9,8 @@
- Breaking change: the `-s` short option for `--style` is now used for - Breaking change: the `-s` short option for `--style` is now used for
the new `--setup` option. the new `--setup` option.
- The environment offset randomization is now also available on Windows,
see #484
## Bugfixes ## Bugfixes
+32 -46
View File
@@ -18,24 +18,8 @@ pub struct ExecuteResult {
pub status: ExitStatus, pub status: ExitStatus,
} }
/// Execute the given command and return a timing summary fn randomized_environment_offset_value() -> String {
#[cfg(windows)] "X".repeat(rand::random::<usize>() % 4096usize)
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,
})
} }
/// Execute the given command and return a timing summary /// Execute the given command and return a timing summary
@@ -48,7 +32,19 @@ pub fn execute_and_time(
) -> Result<ExecuteResult> { ) -> Result<ExecuteResult> {
let cpu_timer = get_cpu_timer(); let cpu_timer = get_cpu_timer();
let status = run_shell_command(stdout, stderr, command, shell)?; let status = shell
.command()
.arg("-c")
.arg(command)
.env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
randomized_environment_offset_value(),
)
.stdin(Stdio::null())
.stdout(stdout)
.stderr(stderr)
.status()
.with_context(|| format!("Failed to run command '{}'", command))?;
let (user_time, system_time) = cpu_timer.stop(); let (user_time, system_time) = cpu_timer.stop();
@@ -59,44 +55,34 @@ pub fn execute_and_time(
}) })
} }
/// Run a standard shell command using `sh -c` /// Execute the given command and return a timing summary
#[cfg(not(windows))] #[cfg(windows)]
fn run_shell_command( pub fn execute_and_time(
stdout: Stdio, stdout: Stdio,
stderr: Stdio, stderr: Stdio,
command: &str, command: &str,
shell: &Shell, shell: &Shell,
) -> Result<std::process::ExitStatus> { ) -> Result<ExecuteResult> {
shell let mut child = shell
.command() .command()
.arg("-c") .arg("/C")
.arg(command) .arg(command)
.env( .env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
"X".repeat(rand::random::<usize>() % 4096usize), randomized_environment_offset_value(),
) )
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(stdout) .stdout(stdout)
.stderr(stderr) .stderr(stderr)
.status()
.with_context(|| format!("Failed to run command '{}'", command))
}
/// Run a Windows shell command using `cmd.exe /C`
#[cfg(windows)]
fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &Shell,
) -> Result<std::process::Child> {
shell
.command()
.arg("/C")
.arg(command)
.stdin(Stdio::null())
.stdout(stdout)
.stderr(stderr)
.spawn() .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,
})
} }