2018-01-14 20:14:10 +01:00
|
|
|
|
use std::io;
|
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
use ansi_term::Colour::{Blue, Cyan, Green, Purple, White, Yellow};
|
2018-01-14 21:07:00 +01:00
|
|
|
|
use statistical::{mean, standard_deviation};
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-18 19:00:04 +01:00
|
|
|
|
use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,
|
|
|
|
|
|
Warnings, MIN_EXECUTION_TIME};
|
2018-01-18 18:55:27 +01:00
|
|
|
|
use hyperfine::format::{format_duration, format_duration_unit};
|
2018-01-20 12:55:16 +01:00
|
|
|
|
use hyperfine::cputime::{cpu_time_interval, get_cpu_times};
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
/// Results from timing a single shell command
|
2018-01-19 08:44:33 +01:00
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
2018-01-14 20:14:10 +01:00
|
|
|
|
pub struct TimingResult {
|
2018-01-19 08:44:33 +01:00
|
|
|
|
/// Wall clock time
|
|
|
|
|
|
pub time_real: Second,
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
/// Time spent in user mode
|
|
|
|
|
|
pub time_user: Second,
|
|
|
|
|
|
|
|
|
|
|
|
/// Time spent in kernel mode
|
|
|
|
|
|
pub time_system: Second,
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
/// Correct for shell spawning time
|
|
|
|
|
|
fn subtract_shell_spawning_time(time: Second, shell_spawning_time: Second) -> Second {
|
|
|
|
|
|
if time < shell_spawning_time {
|
|
|
|
|
|
0.0
|
|
|
|
|
|
} else {
|
|
|
|
|
|
time - shell_spawning_time
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the given shell command and measure the execution time
|
2018-01-14 21:07:00 +01:00
|
|
|
|
pub fn time_shell_command(
|
|
|
|
|
|
shell_cmd: &str,
|
2018-01-18 18:39:17 +01:00
|
|
|
|
failure_action: CmdFailureAction,
|
2018-01-19 08:44:33 +01:00
|
|
|
|
shell_spawning_time: Option<TimingResult>,
|
|
|
|
|
|
) -> io::Result<(TimingResult, bool)> {
|
2018-01-14 20:14:10 +01:00
|
|
|
|
let start = Instant::now();
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let start_cpu = get_cpu_times();
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
let status = Command::new("sh")
|
|
|
|
|
|
.arg("-c")
|
|
|
|
|
|
.arg(shell_cmd)
|
|
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
|
|
.status()?;
|
|
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let end_cpu = get_cpu_times();
|
|
|
|
|
|
let duration = start.elapsed();
|
|
|
|
|
|
|
2018-01-18 18:39:17 +01:00
|
|
|
|
if failure_action == CmdFailureAction::RaiseError && !status.success() {
|
2018-01-14 20:14:10 +01:00
|
|
|
|
return Err(io::Error::new(
|
|
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
|
|
"Command terminated with non-zero exit code. \
|
|
|
|
|
|
Use the '-i'/'--ignore-failure' option if you want to ignore this.",
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
// Real time
|
|
|
|
|
|
let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
// User and system time
|
2018-01-20 12:55:16 +01:00
|
|
|
|
let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let mut time_user = cpu_interval.user;
|
|
|
|
|
|
let mut time_system = cpu_interval.system;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-14 21:07:00 +01:00
|
|
|
|
// Correct for shell spawning time
|
2018-01-19 08:44:33 +01:00
|
|
|
|
if let Some(spawning_time) = shell_spawning_time {
|
|
|
|
|
|
time_real = subtract_shell_spawning_time(time_real, spawning_time.time_real);
|
|
|
|
|
|
time_user = subtract_shell_spawning_time(cpu_interval.user, spawning_time.time_user);
|
|
|
|
|
|
time_system = subtract_shell_spawning_time(cpu_interval.system, spawning_time.time_system);
|
|
|
|
|
|
}
|
2018-01-14 21:07:00 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
Ok((
|
|
|
|
|
|
TimingResult {
|
|
|
|
|
|
time_real,
|
|
|
|
|
|
time_user,
|
|
|
|
|
|
time_system,
|
|
|
|
|
|
},
|
2018-01-14 21:07:00 +01:00
|
|
|
|
status.success(),
|
|
|
|
|
|
))
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Measure the average shell spawning time
|
2018-01-19 08:44:33 +01:00
|
|
|
|
pub fn mean_shell_spawning_time() -> io::Result<TimingResult> {
|
2018-01-14 20:14:10 +01:00
|
|
|
|
const COUNT: u64 = 200;
|
2018-01-18 20:16:36 +01:00
|
|
|
|
let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time");
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let mut times_real: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_user: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_system: Vec<Second> = vec![];
|
|
|
|
|
|
|
2018-01-14 20:14:10 +01:00
|
|
|
|
for _ in 0..COUNT {
|
2018-01-14 21:07:00 +01:00
|
|
|
|
// Just run the shell without any command
|
2018-01-18 18:39:17 +01:00
|
|
|
|
let res = time_shell_command("", CmdFailureAction::RaiseError, None);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
|
Err(_) => {
|
|
|
|
|
|
return Err(io::Error::new(
|
|
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
|
|
"Could not measure shell execution time. \
|
|
|
|
|
|
Make sure you can run 'sh -c \"\"'.",
|
|
|
|
|
|
))
|
|
|
|
|
|
}
|
2018-01-19 08:44:33 +01:00
|
|
|
|
Ok((r, _)) => {
|
|
|
|
|
|
times_real.push(r.time_real);
|
|
|
|
|
|
times_user.push(r.time_user);
|
|
|
|
|
|
times_system.push(r.time_system);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.inc(1);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.finish_and_clear();
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
Ok(TimingResult {
|
|
|
|
|
|
time_real: mean(×_real),
|
|
|
|
|
|
time_user: mean(×_user),
|
|
|
|
|
|
time_system: mean(×_system),
|
|
|
|
|
|
})
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the benchmark for a single shell command
|
|
|
|
|
|
pub fn run_benchmark(
|
|
|
|
|
|
num: usize,
|
|
|
|
|
|
cmd: &str,
|
2018-01-19 08:44:33 +01:00
|
|
|
|
shell_spawning_time: TimingResult,
|
2018-01-14 20:14:10 +01:00
|
|
|
|
options: &HyperfineOptions,
|
|
|
|
|
|
) -> io::Result<()> {
|
|
|
|
|
|
println!(
|
|
|
|
|
|
"{}{}: {}",
|
|
|
|
|
|
White.bold().paint("Benchmark #"),
|
|
|
|
|
|
White.bold().paint((num + 1).to_string()),
|
2018-01-14 22:01:22 +01:00
|
|
|
|
cmd
|
2018-01-14 20:14:10 +01:00
|
|
|
|
);
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let mut times_real: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_user: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_system: Vec<Second> = vec![];
|
2018-01-14 21:07:00 +01:00
|
|
|
|
let mut all_succeeded = true;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Warmup phase
|
|
|
|
|
|
if options.warmup_count > 0 {
|
2018-01-18 20:16:36 +01:00
|
|
|
|
let progress_bar = get_progress_bar(options.warmup_count, "Performing warmup runs");
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
for _ in 0..options.warmup_count {
|
2018-01-18 18:39:17 +01:00
|
|
|
|
let _ = time_shell_command(cmd, options.failure_action, None)?;
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.inc(1);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.finish_and_clear();
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set up progress bar (and spinner for initial measurement)
|
2018-01-18 20:16:36 +01:00
|
|
|
|
let progress_bar = get_progress_bar(options.min_runs, "Initial time measurement");
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-18 18:13:12 +01:00
|
|
|
|
// Run init / cleanup command
|
|
|
|
|
|
let run_preparation_command = || {
|
|
|
|
|
|
if let Some(ref preparation_command) = options.preparation_command {
|
2018-01-18 18:39:17 +01:00
|
|
|
|
let _ = time_shell_command(preparation_command, options.failure_action, None);
|
2018-01-16 21:26:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2018-01-18 18:13:12 +01:00
|
|
|
|
run_preparation_command();
|
2018-01-16 21:26:14 +01:00
|
|
|
|
|
2018-01-14 20:14:10 +01:00
|
|
|
|
// Initial timing run
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let (res, success) =
|
|
|
|
|
|
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Determine number of benchmark runs
|
2018-01-14 21:07:00 +01:00
|
|
|
|
let runs_in_min_time =
|
2018-01-19 08:44:33 +01:00
|
|
|
|
(options.min_time_sec / (res.time_real + shell_spawning_time.time_real)) as u64;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
let count = if runs_in_min_time >= options.min_runs {
|
|
|
|
|
|
runs_in_min_time
|
|
|
|
|
|
} else {
|
|
|
|
|
|
options.min_runs
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let count_remaining = count - 1;
|
|
|
|
|
|
|
|
|
|
|
|
// Save the first result
|
2018-01-19 08:44:33 +01:00
|
|
|
|
times_real.push(res.time_real);
|
|
|
|
|
|
times_user.push(res.time_user);
|
|
|
|
|
|
times_system.push(res.time_system);
|
|
|
|
|
|
|
|
|
|
|
|
all_succeeded = all_succeeded && success;
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Re-configure the progress bar
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.set_length(count_remaining);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Gather statistics
|
|
|
|
|
|
for _ in 0..count_remaining {
|
2018-01-18 18:13:12 +01:00
|
|
|
|
run_preparation_command();
|
2018-01-16 21:26:14 +01:00
|
|
|
|
|
2018-01-14 20:14:10 +01:00
|
|
|
|
let msg = {
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let mean = format_duration(mean(×_real), None);
|
2018-01-14 20:31:18 +01:00
|
|
|
|
format!("Current estimate: {}", Green.paint(mean))
|
2018-01-14 20:14:10 +01:00
|
|
|
|
};
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.set_message(&msg);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let (res, success) =
|
|
|
|
|
|
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
2018-01-14 21:07:00 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
times_real.push(res.time_real);
|
|
|
|
|
|
times_user.push(res.time_user);
|
|
|
|
|
|
times_system.push(res.time_system);
|
|
|
|
|
|
|
|
|
|
|
|
all_succeeded = all_succeeded && success;
|
2018-01-14 21:07:00 +01:00
|
|
|
|
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.inc(1);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
}
|
2018-01-18 20:16:36 +01:00
|
|
|
|
progress_bar.finish_and_clear();
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Compute statistical quantities
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let t_mean = mean(×_real);
|
|
|
|
|
|
let t_stddev = standard_deviation(×_real, Some(t_mean));
|
|
|
|
|
|
let t_min = min(×_real);
|
|
|
|
|
|
let t_max = max(×_real);
|
|
|
|
|
|
|
|
|
|
|
|
let user_mean = mean(×_user);
|
|
|
|
|
|
let system_mean = mean(×_system);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Formatting and console output
|
2018-01-18 18:55:27 +01:00
|
|
|
|
let (mean_str, unit_mean) = format_duration_unit(t_mean, None);
|
|
|
|
|
|
let stddev_str = format_duration(t_stddev, Some(unit_mean));
|
|
|
|
|
|
let min_str = format_duration(t_min, Some(unit_mean));
|
|
|
|
|
|
let max_str = format_duration(t_max, Some(unit_mean));
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
2018-01-19 08:44:33 +01:00
|
|
|
|
let (user_str, user_unit) = format_duration_unit(user_mean, None);
|
|
|
|
|
|
let system_str = format_duration(system_mean, Some(user_unit));
|
|
|
|
|
|
|
2018-01-14 21:43:29 +01:00
|
|
|
|
println!(
|
2018-01-19 08:44:33 +01:00
|
|
|
|
" Time ({} ± {}): {} ± {} [User: {}, System: {}]",
|
2018-01-14 22:01:22 +01:00
|
|
|
|
Green.bold().paint("mean"),
|
|
|
|
|
|
Green.paint("σ"),
|
|
|
|
|
|
Green.bold().paint(mean_str),
|
2018-01-19 08:44:33 +01:00
|
|
|
|
Green.paint(stddev_str),
|
|
|
|
|
|
Blue.paint(user_str),
|
|
|
|
|
|
Blue.paint(system_str)
|
2018-01-14 21:43:29 +01:00
|
|
|
|
);
|
2018-01-14 22:07:19 +01:00
|
|
|
|
println!(" ");
|
2018-01-14 21:43:29 +01:00
|
|
|
|
|
|
|
|
|
|
println!(
|
2018-01-14 22:01:22 +01:00
|
|
|
|
" Range ({} … {}): {} … {}",
|
|
|
|
|
|
Cyan.paint("min"),
|
|
|
|
|
|
Purple.paint("max"),
|
|
|
|
|
|
Cyan.paint(min_str),
|
|
|
|
|
|
Purple.paint(max_str)
|
2018-01-14 21:43:29 +01:00
|
|
|
|
);
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Warnings
|
|
|
|
|
|
let mut warnings = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
// Check execution time
|
2018-01-19 08:44:33 +01:00
|
|
|
|
if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {
|
2018-01-14 20:14:10 +01:00
|
|
|
|
warnings.push(Warnings::FastExecutionTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check programm exit codes
|
2018-01-14 21:07:00 +01:00
|
|
|
|
if !all_succeeded {
|
2018-01-14 20:14:10 +01:00
|
|
|
|
warnings.push(Warnings::NonZeroExitCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-18 20:16:36 +01:00
|
|
|
|
if !warnings.is_empty() {
|
2018-01-14 22:07:19 +01:00
|
|
|
|
eprintln!(" ");
|
2018-01-14 20:14:10 +01:00
|
|
|
|
for warning in &warnings {
|
|
|
|
|
|
eprintln!(" {}: {}", Yellow.paint("Warning"), warning);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-14 22:07:19 +01:00
|
|
|
|
println!(" ");
|
2018-01-14 20:14:10 +01:00
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|