Compare commits

..
Author SHA1 Message Date
David Peter 436bff7664 Proper shell overhead computation 2022-02-22 08:58:57 +01:00
David Peter a1885ac746 Add debug/mock mode 2022-02-22 08:36:56 +01:00
David Peter 319cfac4ab Faster tests 2022-02-21 20:45:04 +01:00
David Peter 0a83ea6b96 Add integration test for '--shell' 2022-02-21 16:47:32 +01:00
David Peter e4da76ec29 New Benchmark struct 2022-02-21 08:46:10 +01:00
David Peter bfb7a15d67 Improve error message 2022-02-20 20:20:47 +01:00
David Peter 665fab6d6e Formatting 2022-02-20 17:34:01 +01:00
David Peter 5a872d81fe Better error handling in run_intermediate_command 2022-02-20 17:31:54 +01:00
David Peter 8533d4fb4b Restructure run_intermediate_command 2022-02-20 17:26:53 +01:00
David Peter 6365e3dc0c Update BenchmarkResult documentation 2022-02-20 17:08:39 +01:00
David Peter 3c772009c4 Simplify code 2022-02-20 17:04:50 +01:00
David Peter b78c77ec95 Rename to command 2022-02-20 17:03:29 +01:00
David Peter 9a8115831f Rename num 2022-02-20 14:18:57 +01:00
David Peter 241a72a47c Move functionality into run_cleanup_command 2022-02-20 14:15:22 +01:00
David Peter d5c0ffc2fb Remove more function arguments 2022-02-20 14:12:12 +01:00
David Peter fdbdd9485c Move functions into Scheduler 2022-02-20 14:04:37 +01:00
David Peter 455717a393 Move functionality to scheduler 2022-02-20 13:44:34 +01:00
David Peter 4e8b506c3c Move exit code function to separate file 2022-02-20 13:36:30 +01:00
David Peter 90d9ea9d80 Rename test 2022-02-20 13:31:49 +01:00
David Peter 85f9aaafa2 Use get_program 2022-02-20 13:25:50 +01:00
David Peter 24be84e025 Remove unused option 2022-02-20 13:23:54 +01:00
David Peter 136ab1b625 Rename parse to from_str 2022-02-20 13:19:23 +01:00
David Peter 1034ffbaa9 Turn show_output into enum 2022-02-20 13:15:54 +01:00
David Peter dde26cf1cb Rename _res to _result 2022-02-19 20:03:49 +01:00
David Peter f25c7290a4 Rename Runs to RunBounds 2022-02-19 20:01:49 +01:00
David Peter d229c52671 Fix clippy warning 2022-02-19 19:58:14 +01:00
16 changed files with 250 additions and 190 deletions

No files matched your search

-5
View File
@@ -9,16 +9,11 @@
- 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
## Other
- Improved documentation and test coverage, cleaned up code base for
future improvements.
## Packaging
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018-2022 David Peter, and all hyperfine contributors
Copyright (c) 2018-2020 The hyperfine developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+49 -64
View File
@@ -3,67 +3,28 @@ use std::process::{ExitStatus, Stdio};
use crate::command::Command;
use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};
use crate::output::progress_bar::get_progress_bar;
use crate::timer::{execute_and_measure, TimerResult};
use crate::util::randomized_environment_offset;
use crate::shell::execute_and_time;
use crate::timer::wallclocktimer::WallClockTimer;
use crate::timer::{TimerStart, TimerStop};
use crate::util::units::Second;
use super::timing_result::TimingResult;
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use statistical::mean;
pub trait Executor {
/// Run the given command and measure the execution time
fn run_command_and_measure(
fn time_command(
&self,
command: &Command<'_>,
command_failure_action: Option<CmdFailureAction>,
) -> Result<(TimingResult, ExitStatus)>;
/// Perform a calibration of this executor. For example,
/// when running commands through a shell, we need to
/// measure the shell spawning time separately in order
/// to subtract it from the full runtime later.
fn calibrate(&mut self) -> Result<()>;
/// Return the time overhead for this executor when
/// performing a measurement. This should return the time
/// that is being used in addition to the actual runtime
/// of the command.
fn time_overhead(&self) -> Second;
}
fn run_command_and_measure_common(
mut command: std::process::Command,
command_failure_action: CmdFailureAction,
command_output_policy: CommandOutputPolicy,
command_name: &str,
) -> Result<TimerResult> {
let (stdout, stderr) = command_output_policy.get_stdout_stderr();
command.stdin(Stdio::null()).stdout(stdout).stderr(stderr);
command.env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
randomized_environment_offset::value(),
);
let result = execute_and_measure(command)
.with_context(|| format!("Failed to run command '{}'", command_name))?;
if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {
bail!(
"{}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \
Alternatively, use the '--show-output' option to debug what went wrong.",
result.status.code().map_or(
"The process has been terminated by a signal".into(),
|c| format!("Command terminated with non-zero exit code: {}", c)
)
);
}
Ok(result)
}
pub struct ShellExecutor<'a> {
options: &'a Options,
shell: &'a Shell,
@@ -71,6 +32,15 @@ pub struct ShellExecutor<'a> {
}
impl<'a> ShellExecutor<'a> {
/// Correct for shell spawning time
fn subtract_shell_spawning_time(&self, time: Second, shell_spawning_time: Second) -> Second {
if time < shell_spawning_time {
0.0
} else {
time - shell_spawning_time
}
}
pub fn new(shell: &'a Shell, options: &'a Options) -> Self {
ShellExecutor {
shell,
@@ -81,35 +51,50 @@ impl<'a> ShellExecutor<'a> {
}
impl<'a> Executor for ShellExecutor<'a> {
fn run_command_and_measure(
/// Run the given shell command and measure the execution time
fn time_command(
&self,
command: &Command<'_>,
command_failure_action: Option<CmdFailureAction>,
) -> Result<(TimingResult, ExitStatus)> {
let mut command_builder = self.shell.command();
command_builder
.arg(if cfg!(windows) { "/C" } else { "-c" })
.arg(command.get_command_line());
let (stdout, stderr) = match self.options.command_output_policy {
CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),
CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
};
let mut result = run_command_and_measure_common(
command_builder,
command_failure_action.unwrap_or(self.options.command_failure_action),
self.options.command_output_policy,
&command.get_command_line(),
)?;
let wallclock_timer = WallClockTimer::start();
let result = execute_and_time(stdout, stderr, &command.get_shell_command(), &self.shell)?;
let mut time_real = wallclock_timer.stop();
let mut time_user = result.user_time;
let mut time_system = result.system_time;
if command_failure_action.unwrap_or(self.options.command_failure_action)
== CmdFailureAction::RaiseError
&& !result.status.success()
{
bail!(
"{}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \
Alternatively, use the '--show-output' option to debug what went wrong.",
result.status.code().map_or(
"The process has been terminated by a signal".into(),
|c| format!("Command terminated with non-zero exit code: {}", c)
)
);
}
// Subtract shell spawning time
if let Some(spawning_time) = self.shell_spawning_time {
result.time_real = (result.time_real - spawning_time.time_real).max(0.0);
result.time_user = (result.time_user - spawning_time.time_user).max(0.0);
result.time_system = (result.time_system - spawning_time.time_system).max(0.0);
time_real = self.subtract_shell_spawning_time(time_real, spawning_time.time_real);
time_user = self.subtract_shell_spawning_time(time_user, spawning_time.time_user);
time_system = self.subtract_shell_spawning_time(time_system, spawning_time.time_system);
}
Ok((
TimingResult {
time_real: result.time_real,
time_user: result.time_user,
time_system: result.time_system,
time_real,
time_user,
time_system,
},
result.status,
))
@@ -134,7 +119,7 @@ impl<'a> Executor for ShellExecutor<'a> {
for _ in 0..COUNT {
// Just run the shell without any command
let res = self.run_command_and_measure(&Command::new(None, ""), None);
let res = self.time_command(&Command::new(None, ""), None);
match res {
Err(_) => {
@@ -200,7 +185,7 @@ impl MockExecutor {
}
impl Executor for MockExecutor {
fn run_command_and_measure(
fn time_command(
&self,
command: &Command<'_>,
_command_failure_action: Option<CmdFailureAction>,
@@ -219,7 +204,7 @@ impl Executor for MockExecutor {
Ok((
TimingResult {
time_real: Self::extract_time(command.get_command_line()),
time_real: Self::extract_time(command.get_shell_command()),
time_user: 0.0,
time_system: 0.0,
},
+4 -4
View File
@@ -57,7 +57,7 @@ impl<'a> Benchmark<'a> {
error_output: &'static str,
) -> Result<TimingResult> {
self.executor
.run_command_and_measure(command, Some(CmdFailureAction::RaiseError))
.time_command(command, Some(CmdFailureAction::RaiseError))
.map(|r| r.0)
.map_err(|_| anyhow!(error_output))
}
@@ -163,7 +163,7 @@ impl<'a> Benchmark<'a> {
for _ in 0..self.options.warmup_count {
let _ = run_preparation_command()?;
let _ = self.executor.run_command_and_measure(self.command, None)?;
let _ = self.executor.time_command(self.command, None)?;
if let Some(bar) = progress_bar.as_ref() {
bar.inc(1)
}
@@ -189,7 +189,7 @@ impl<'a> Benchmark<'a> {
preparation_result.map_or(0.0, |res| res.time_real + self.executor.time_overhead());
// Initial timing run
let (res, status) = self.executor.run_command_and_measure(self.command, None)?;
let (res, status) = self.executor.time_command(self.command, None)?;
let success = status.success();
// Determine number of benchmark runs
@@ -239,7 +239,7 @@ impl<'a> Benchmark<'a> {
bar.set_message(msg.to_owned())
}
let (res, status) = self.executor.run_command_and_measure(self.command, None)?;
let (res, status) = self.executor.time_command(self.command, None)?;
let success = status.success();
times_real.push(res.time_real);
+1 -1
View File
@@ -34,7 +34,7 @@ impl<'a> Scheduler<'a> {
pub fn run_benchmarks(&mut self) -> Result<()> {
let mut executor: Box<dyn Executor> = match self.options.executor_kind {
ExecutorKind::Mock(ref shell) => Box::new(MockExecutor::new(shell.clone())),
ExecutorKind::Shell(ref shell) => Box::new(ShellExecutor::new(shell, self.options)),
ExecutorKind::Shell(ref shell) => Box::new(ShellExecutor::new(shell, &self.options)),
};
executor.calibrate()?;
+12 -12
View File
@@ -55,12 +55,12 @@ impl<'a> Command<'a> {
pub fn get_name(&self) -> String {
self.name.map_or_else(
|| self.get_command_line(),
|| self.get_shell_command(),
|name| self.replace_parameters_in(name),
)
}
pub fn get_command_line(&self) -> String {
pub fn get_shell_command(&self) -> String {
self.replace_parameters_in(self.expression)
}
@@ -82,7 +82,7 @@ impl<'a> Command<'a> {
//
// hyperfine -L foo 'a,{bar}' -L bar 'baz,quux' 'echo {foo} {bar}'
//
// should not ever run 'echo baz baz'. See `test_get_command_line_nonoverlapping`.
// should not ever run 'echo baz baz'. See `test_get_shell_command_nonoverlapping`.
'outer: while let Some(head) = remaining.chars().next() {
for (k, v) in &replacements {
if remaining.starts_with(k.as_str()) {
@@ -318,7 +318,7 @@ impl<'a> Commands<'a> {
}
#[test]
fn test_get_command_line_nonoverlapping() {
fn test_get_shell_command_nonoverlapping() {
let cmd = Command::new_parametrized(
None,
"echo {foo} {bar}",
@@ -327,7 +327,7 @@ fn test_get_command_line_nonoverlapping() {
("bar", ParameterValue::Text("quux".into())),
],
);
assert_eq!(cmd.get_command_line(), "echo {bar} baz quux");
assert_eq!(cmd.get_shell_command(), "echo {bar} baz quux");
}
#[test]
@@ -345,7 +345,7 @@ fn test_get_parameterized_command_name() {
impl<'a> fmt::Display for Command<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get_command_line())
write!(f, "{}", self.get_shell_command())
}
}
@@ -404,8 +404,8 @@ fn test_build_parameter_list_commands() {
assert_eq!(commands.len(), 2);
assert_eq!(commands[0].get_name(), "name-1");
assert_eq!(commands[1].get_name(), "name-2");
assert_eq!(commands[0].get_command_line(), "echo 1");
assert_eq!(commands[1].get_command_line(), "echo 2");
assert_eq!(commands[0].get_shell_command(), "echo 1");
assert_eq!(commands[1].get_shell_command(), "echo 2");
}
#[test]
@@ -427,8 +427,8 @@ fn test_build_parameter_scan_commands() {
assert_eq!(commands.len(), 2);
assert_eq!(commands[0].get_name(), "name-1");
assert_eq!(commands[1].get_name(), "name-2");
assert_eq!(commands[0].get_command_line(), "echo 1");
assert_eq!(commands[1].get_command_line(), "echo 2");
assert_eq!(commands[0].get_shell_command(), "echo 1");
assert_eq!(commands[1].get_shell_command(), "echo 2");
}
#[test]
@@ -444,7 +444,7 @@ fn test_parameter_scan_commands_int() {
.unwrap();
assert_eq!(commands.len(), 3);
assert_eq!(commands[2].get_name(), "echo 7");
assert_eq!(commands[2].get_command_line(), "echo 7");
assert_eq!(commands[2].get_shell_command(), "echo 7");
}
#[test]
@@ -464,7 +464,7 @@ fn test_parameter_scan_commands_decimal() {
.unwrap();
assert_eq!(commands.len(), 4);
assert_eq!(commands[3].get_name(), "echo 0.99");
assert_eq!(commands[3].get_command_line(), "echo 0.99");
assert_eq!(commands[3].get_shell_command(), "echo 0.99");
}
#[test]
+1
View File
@@ -18,6 +18,7 @@ pub mod options;
pub mod outlier_detection;
pub mod output;
pub mod parameter;
pub mod shell;
pub mod timer;
pub mod util;
+7 -16
View File
@@ -1,4 +1,4 @@
use std::process::{Command, Stdio};
use std::process::Command;
use std::{cmp, fmt};
use anyhow::ensure;
@@ -44,7 +44,7 @@ impl fmt::Display for Shell {
impl Shell {
/// Parse given string as shell command line
pub fn parse_from_str<'a>(s: &str) -> Result<Self, OptionsError<'a>> {
pub fn from_str<'a>(s: &str) -> Result<Self, OptionsError<'a>> {
let v = shell_words::split(s).map_err(OptionsError::ShellParseError)?;
if v.is_empty() || v[0].is_empty() {
return Err(OptionsError::EmptyShell);
@@ -124,15 +124,6 @@ impl Default for CommandOutputPolicy {
}
}
impl CommandOutputPolicy {
pub fn get_stdout_stderr(&self) -> (Stdio, Stdio) {
match self {
CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),
CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
}
}
}
pub enum ExecutorKind {
Shell(Shell),
Mock(Option<String>),
@@ -283,7 +274,7 @@ impl Options {
options.executor_kind = match (matches.is_present("debug-mode"), matches.value_of("shell"))
{
(false, Some(shell)) => ExecutorKind::Shell(Shell::parse_from_str(shell)?),
(false, Some(shell)) => ExecutorKind::Shell(Shell::from_str(shell)?),
(false, None) => ExecutorKind::Shell(Shell::default()),
(true, Some(shell)) => ExecutorKind::Mock(Some(shell.into())),
(true, None) => ExecutorKind::Mock(None),
@@ -329,7 +320,7 @@ fn test_default_shell() {
#[test]
fn test_can_parse_shell_command_line_from_str() {
let shell = Shell::parse_from_str("shell -x 'aaa bbb'").unwrap();
let shell = Shell::from_str("shell -x 'aaa bbb'").unwrap();
let s = format!("{}", shell);
assert_eq!(&s, "shell -x 'aaa bbb'");
@@ -345,17 +336,17 @@ fn test_can_parse_shell_command_line_from_str() {
// Error cases
assert!(matches!(
Shell::parse_from_str("shell 'foo").unwrap_err(),
Shell::from_str("shell 'foo").unwrap_err(),
OptionsError::ShellParseError(_)
));
assert!(matches!(
Shell::parse_from_str("").unwrap_err(),
Shell::from_str("").unwrap_err(),
OptionsError::EmptyShell
));
assert!(matches!(
Shell::parse_from_str("''").unwrap_err(),
Shell::from_str("''").unwrap_err(),
OptionsError::EmptyShell
));
}
+102
View File
@@ -0,0 +1,102 @@
use std::process::{ExitStatus, Stdio};
use crate::options::Shell;
use crate::timer::get_cpu_timer;
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)]
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
#[cfg(not(windows))]
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &Shell,
) -> 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
.command()
.arg("-c")
.arg(command)
.env(
"HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",
"X".repeat(rand::random::<usize>() % 4096usize),
)
.stdin(Stdio::null())
.stdout(stdout)
.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()
.with_context(|| format!("Failed to run command '{}'", command))
}
+25 -44
View File
@@ -1,20 +1,33 @@
mod wall_clock_timer;
use std::process::Child;
use crate::util::units::Second;
pub mod wallclocktimer;
#[cfg(windows)]
mod windows_timer;
#[cfg(windows)]
pub use self::windows_timer::get_cpu_timer;
#[cfg(not(windows))]
mod unix_timer;
#[cfg(not(windows))]
pub use self::unix_timer::get_cpu_timer;
use crate::util::units::Second;
use wall_clock_timer::WallClockTimer;
/// Defines start functionality of a timer.
pub trait TimerStart {
fn start() -> Self;
fn start_for_process(process: &Child) -> Self;
}
use std::process::{Command, ExitStatus};
use anyhow::Result;
/// Defines stop functionality of a timer.
pub trait TimerStop {
type Result;
fn stop(&self) -> Self::Result;
}
#[derive(Debug, Copy, Clone)]
struct CPUTimes {
pub struct CPUTimes {
/// Total amount of time spent executing in user mode
pub user_usec: i64,
@@ -22,43 +35,11 @@ struct CPUTimes {
pub system_usec: i64,
}
/// Used to indicate the result of running a command
#[derive(Debug, Copy, Clone)]
pub struct TimerResult {
pub time_real: Second,
pub time_user: Second,
pub time_system: Second,
pub struct CPUInterval {
/// Total amount of time spent executing in user mode
pub user: Second,
/// The exit status of the process
pub status: ExitStatus,
}
/// Execute the given command and return a timing summary
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
let wallclock_timer = WallClockTimer::start();
#[cfg(not(windows))]
let ((time_user, time_system), status) = {
let cpu_timer = self::unix_timer::CPUTimer::start();
let status = command.status()?;
(cpu_timer.stop(), status)
};
#[cfg(windows)]
let ((time_user, time_system), status) = {
let mut child = command.spawn()?;
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let status = child.wait()?;
(cpu_timer.stop(), status)
};
let time_real = wallclock_timer.stop();
Ok(TimerResult {
time_real,
time_user,
time_system,
status,
})
/// Total amount of time spent executing in kernel mode
pub system: Second,
}
+16 -12
View File
@@ -1,31 +1,35 @@
#![cfg(not(windows))]
use std::mem;
use std::process::Child;
use crate::timer::CPUTimes;
use crate::timer::{CPUInterval, CPUTimes, TimerStart, TimerStop};
use crate::util::units::Second;
#[derive(Debug, Copy, Clone)]
pub struct CPUInterval {
/// Total amount of time spent executing in user mode
pub user: Second,
/// Total amount of time spent executing in kernel mode
pub system: Second,
pub fn get_cpu_timer() -> Box<dyn TimerStop<Result = (Second, Second)>> {
Box::new(CPUTimer::start())
}
pub struct CPUTimer {
struct CPUTimer {
start_cpu: CPUTimes,
}
impl CPUTimer {
pub fn start() -> Self {
impl TimerStart for CPUTimer {
fn start() -> Self {
CPUTimer {
start_cpu: get_cpu_times(),
}
}
pub fn stop(&self) -> (Second, Second) {
fn start_for_process(_: &Child) -> Self {
Self::start()
}
}
impl TimerStop for CPUTimer {
type Result = (Second, Second);
fn stop(&self) -> Self::Result {
let end_cpu = get_cpu_times();
let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);
(cpu_interval.user, cpu_interval.system)
@@ -1,19 +1,29 @@
use std::process::Child;
use std::time::Instant;
use crate::timer::{TimerStart, TimerStop};
use crate::util::units::Second;
pub struct WallClockTimer {
start: Instant,
}
impl WallClockTimer {
pub fn start() -> WallClockTimer {
impl TimerStart for WallClockTimer {
fn start() -> WallClockTimer {
WallClockTimer {
start: Instant::now(),
}
}
pub fn stop(&self) -> Second {
fn start_for_process(_: &Child) -> Self {
Self::start()
}
}
impl TimerStop for WallClockTimer {
type Result = Second;
fn stop(&self) -> Second {
let duration = self.start.elapsed();
duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-9
}
+19 -7
View File
@@ -7,23 +7,35 @@ use std::process::Child;
use winapi::um::processthreadsapi::GetProcessTimes;
use winapi::um::winnt::HANDLE;
use crate::timer::CPUTimes;
use crate::timer::{CPUTimes, TimerStart, TimerStop};
use crate::util::units::Second;
const HUNDRED_NS_PER_MS: i64 = 10;
pub struct CPUTimer {
pub fn get_cpu_timer(process: &Child) -> Box<dyn TimerStop<Result = (Second, Second)>> {
Box::new(CPUTimer::start_for_process(process))
}
struct CPUTimer {
handle: RawHandle,
}
impl CPUTimer {
pub fn start_for_process(process: &Child) -> Self {
impl TimerStart for CPUTimer {
fn start() -> Self {
panic!()
}
fn start_for_process(process: &Child) -> Self {
CPUTimer {
handle: process.as_raw_handle(),
}
}
}
pub fn stop(&self) -> (Second, Second) {
impl TimerStop for CPUTimer {
type Result = (Second, Second);
fn stop(&self) -> Self::Result {
let times = get_cpu_times(self.handle);
(
times.user_usec as f64 * 1e-6,
@@ -32,7 +44,7 @@ impl CPUTimer {
}
}
/// Read CPU execution times
/// Read CPU execution times (dummy for now)
fn get_cpu_times(handle: RawHandle) -> CPUTimes {
let (user_usec, system_usec) = unsafe {
let mut _ctime = mem::zeroed();
@@ -50,7 +62,7 @@ fn get_cpu_times(handle: RawHandle) -> CPUTimes {
// GetProcessTimes will exit with non-zero if success as per: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx
if res != 0 {
// Extract times as laid out here: https://support.microsoft.com/en-us/help/188768/info-working-with-the-filetime-structure
// Both user_time and kernel_time are spans that the process spent in either.
// Both user_time and kernel_time are spans that the proces spent in either.
let user: i64 = (((user_time.dwHighDateTime as i64) << 32)
+ user_time.dwLowDateTime as i64)
/ HUNDRED_NS_PER_MS;
-1
View File
@@ -1,5 +1,4 @@
pub mod exit_code;
pub mod min_max;
pub mod number;
pub mod randomized_environment_offset;
pub mod units;
@@ -1,8 +0,0 @@
/// Returns a string with a random length. This value will be set as an environment
/// variable to account for offset effects. See [1] for more details.
///
/// [1] Mytkowicz, 2009. Producing Wrong Data Without Doing Anything Obviously Wrong!.
/// Sigplan Notices - SIGPLAN. 44. 265-276. 10.1145/1508284.1508275.
pub fn value() -> String {
"X".repeat(rand::random::<usize>() % 4096usize)
}
-12
View File
@@ -147,18 +147,6 @@ fn can_run_failing_commands_with_ignore_failure_option() {
.success();
}
#[test]
fn shows_output_of_benchmarked_command() {
hyperfine()
.arg("--runs=2")
.arg("--command-name=dummy")
.arg("--show-output")
.arg("echo 4fd47015")
.assert()
.success()
.stdout(predicate::str::contains("4fd47015").count(2));
}
#[test]
fn runs_commands_using_user_defined_shell() {
hyperfine()