Files
hyperfine/src/shell.rs
T

102 lines
2.4 KiB
Rust
Raw Normal View History

2021-10-04 22:34:19 +09:00
use std::process::{ExitStatus, Stdio};
2021-10-04 22:34:19 +09:00
use crate::options::Shell;
2021-08-24 21:24:34 +02:00
use crate::timer::get_cpu_timer;
2022-02-06 17:44:28 +01:00
use anyhow::{Context, Result};
/// Used to indicate the result of running a command
#[derive(Debug, Copy, Clone)]
pub struct ExecuteResult {
/// The amount of user time the process used
pub user_time: f64,
/// The amount of cpu time the process used
pub system_time: f64,
/// The exit status of the process
pub status: ExitStatus,
}
/// Execute the given command and return a timing summary
#[cfg(windows)]
2018-11-21 19:55:58 +01:00
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
2021-10-04 22:34:19 +09:00
shell: &Shell,
2022-02-06 17:44:28 +01:00
) -> Result<ExecuteResult> {
2018-10-30 19:30:17 -07:00
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
#[cfg(not(windows))]
2018-11-21 19:55:58 +01:00
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
2021-10-04 22:34:19 +09:00
shell: &Shell,
2022-02-06 17:44:28 +01:00
) -> Result<ExecuteResult> {
let cpu_timer = get_cpu_timer();
2018-10-30 19:30:17 -07:00
let status = run_shell_command(stdout, stderr, command, shell)?;
let (user_time, system_time) = cpu_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
status,
})
}
2018-02-02 17:28:52 +01:00
2018-10-30 19:30:17 -07:00
/// Run a standard shell command using `sh -c`
#[cfg(not(windows))]
2018-05-22 22:57:51 +02:00
fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
2021-10-04 22:34:19 +09:00
shell: &Shell,
2022-02-06 17:44:28 +01:00
) -> Result<std::process::ExitStatus> {
2021-10-04 22:34:19 +09:00
shell
.command()
2018-02-02 17:28:52 +01:00
.arg("-c")
.arg(command)
.env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
"X".repeat(rand::random::<usize>() % 4096usize),
)
2018-02-02 17:28:52 +01:00
.stdin(Stdio::null())
.stdout(stdout)
2018-05-21 21:04:30 +02:00
.stderr(stderr)
2018-02-02 17:28:52 +01:00
.status()
2022-02-06 17:44:28 +01:00
.with_context(|| format!("Failed to run command '{}'", command))
2018-02-02 17:28:52 +01:00
}
2018-10-30 19:30:17 -07:00
/// Run a Windows shell command using `cmd.exe /C`
#[cfg(windows)]
2018-05-22 22:57:51 +02:00
fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
2021-10-04 22:34:19 +09:00
shell: &Shell,
2022-02-06 17:44:28 +01:00
) -> Result<std::process::Child> {
2021-10-04 22:34:19 +09:00
shell
.command()
2018-02-02 23:04:19 +01:00
.arg("/C")
2018-02-02 17:28:52 +01:00
.arg(command)
.stdin(Stdio::null())
.stdout(stdout)
2018-05-21 21:04:30 +02:00
.stderr(stderr)
.spawn()
2022-02-06 18:02:37 +01:00
.with_context(|| format!("Failed to run command '{}'", command))
2018-02-02 17:28:52 +01:00
}