2022-02-20 13:44:34 +01:00
|
|
|
|
pub mod benchmark_result;
|
2022-02-22 08:36:56 +01:00
|
|
|
|
pub mod executor;
|
2022-02-06 19:54:59 +01:00
|
|
|
|
pub mod relative_speed;
|
2022-02-07 21:03:23 +01:00
|
|
|
|
pub mod scheduler;
|
2022-02-20 13:44:34 +01:00
|
|
|
|
pub mod timing_result;
|
2022-02-06 19:37:15 +01:00
|
|
|
|
|
2022-02-21 08:46:10 +01:00
|
|
|
|
use std::cmp;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::command::Command;
|
2022-02-22 08:36:56 +01:00
|
|
|
|
use crate::options::{CmdFailureAction, Options, OutputStyleOption};
|
2022-02-21 08:46:10 +01:00
|
|
|
|
use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
|
|
|
|
|
|
use crate::output::format::{format_duration, format_duration_unit};
|
|
|
|
|
|
use crate::output::progress_bar::get_progress_bar;
|
|
|
|
|
|
use crate::output::warnings::Warnings;
|
|
|
|
|
|
use crate::parameter::ParameterNameAndValue;
|
|
|
|
|
|
use crate::util::exit_code::extract_exit_code;
|
|
|
|
|
|
use crate::util::min_max::{max, min};
|
2022-02-06 20:01:41 +01:00
|
|
|
|
use crate::util::units::Second;
|
2022-02-21 08:46:10 +01:00
|
|
|
|
use benchmark_result::BenchmarkResult;
|
|
|
|
|
|
use timing_result::TimingResult;
|
|
|
|
|
|
|
2022-02-22 08:36:56 +01:00
|
|
|
|
use anyhow::{anyhow, Result};
|
2022-02-21 08:46:10 +01:00
|
|
|
|
use colored::*;
|
|
|
|
|
|
use statistical::{mean, median, standard_deviation};
|
2018-01-31 17:51:07 -05:00
|
|
|
|
|
2022-02-22 08:36:56 +01:00
|
|
|
|
use self::executor::Executor;
|
|
|
|
|
|
|
2021-08-24 22:16:40 +02:00
|
|
|
|
/// Threshold for warning about fast execution time
|
|
|
|
|
|
pub const MIN_EXECUTION_TIME: Second = 5e-3;
|
2022-02-21 08:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
pub struct Benchmark<'a> {
|
|
|
|
|
|
number: usize,
|
|
|
|
|
|
command: &'a Command<'a>,
|
|
|
|
|
|
options: &'a Options,
|
2022-02-22 08:36:56 +01:00
|
|
|
|
executor: &'a dyn Executor,
|
2022-02-21 08:46:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Benchmark<'a> {
|
|
|
|
|
|
pub fn new(
|
|
|
|
|
|
number: usize,
|
|
|
|
|
|
command: &'a Command<'a>,
|
|
|
|
|
|
options: &'a Options,
|
2022-02-22 08:36:56 +01:00
|
|
|
|
executor: &'a dyn Executor,
|
2022-02-21 08:46:10 +01:00
|
|
|
|
) -> Self {
|
|
|
|
|
|
Benchmark {
|
|
|
|
|
|
number,
|
|
|
|
|
|
command,
|
|
|
|
|
|
options,
|
2022-02-22 08:36:56 +01:00
|
|
|
|
executor,
|
2022-02-21 08:46:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run setup, cleanup, or preparation commands
|
|
|
|
|
|
fn run_intermediate_command(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
command: &Command<'_>,
|
|
|
|
|
|
error_output: &'static str,
|
|
|
|
|
|
) -> Result<TimingResult> {
|
2022-02-22 08:36:56 +01:00
|
|
|
|
self.executor
|
|
|
|
|
|
.time_command(command, Some(CmdFailureAction::RaiseError))
|
|
|
|
|
|
.map(|r| r.0)
|
|
|
|
|
|
.map_err(|_| anyhow!(error_output))
|
2022-02-21 08:46:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the command specified by `--setup`.
|
|
|
|
|
|
fn run_setup_command(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,
|
|
|
|
|
|
) -> Result<TimingResult> {
|
|
|
|
|
|
let command = self
|
|
|
|
|
|
.options
|
|
|
|
|
|
.setup_command
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|setup_command| Command::new_parametrized(None, setup_command, parameters));
|
|
|
|
|
|
|
|
|
|
|
|
let error_output = "The setup command terminated with a non-zero exit code. \
|
|
|
|
|
|
Append ' || true' to the command if you are sure that this can be ignored.";
|
|
|
|
|
|
|
|
|
|
|
|
Ok(command
|
|
|
|
|
|
.map(|cmd| self.run_intermediate_command(&cmd, error_output))
|
|
|
|
|
|
.transpose()?
|
|
|
|
|
|
.unwrap_or_default())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the command specified by `--cleanup`.
|
|
|
|
|
|
fn run_cleanup_command(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,
|
|
|
|
|
|
) -> Result<TimingResult> {
|
|
|
|
|
|
let command = self
|
|
|
|
|
|
.options
|
|
|
|
|
|
.cleanup_command
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|cleanup_command| Command::new_parametrized(None, cleanup_command, parameters));
|
|
|
|
|
|
|
|
|
|
|
|
let error_output = "The cleanup command terminated with a non-zero exit code. \
|
|
|
|
|
|
Append ' || true' to the command if you are sure that this can be ignored.";
|
|
|
|
|
|
|
|
|
|
|
|
Ok(command
|
|
|
|
|
|
.map(|cmd| self.run_intermediate_command(&cmd, error_output))
|
|
|
|
|
|
.transpose()?
|
|
|
|
|
|
.unwrap_or_default())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the command specified by `--prepare`.
|
|
|
|
|
|
fn run_preparation_command(&self, command: &Command<'_>) -> Result<TimingResult> {
|
|
|
|
|
|
let error_output = "The preparation command terminated with a non-zero exit code. \
|
|
|
|
|
|
Append ' || true' to the command if you are sure that this can be ignored.";
|
|
|
|
|
|
|
|
|
|
|
|
self.run_intermediate_command(command, error_output)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Run the benchmark for a single command
|
|
|
|
|
|
pub fn run(&self) -> Result<BenchmarkResult> {
|
|
|
|
|
|
let command_name = self.command.get_name();
|
|
|
|
|
|
if self.options.output_style != OutputStyleOption::Disabled {
|
|
|
|
|
|
println!(
|
|
|
|
|
|
"{}{}: {}",
|
|
|
|
|
|
"Benchmark ".bold(),
|
|
|
|
|
|
(self.number + 1).to_string().bold(),
|
|
|
|
|
|
command_name,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let mut times_real: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_user: Vec<Second> = vec![];
|
|
|
|
|
|
let mut times_system: Vec<Second> = vec![];
|
|
|
|
|
|
let mut exit_codes: Vec<Option<i32>> = vec![];
|
|
|
|
|
|
let mut all_succeeded = true;
|
|
|
|
|
|
|
|
|
|
|
|
let preparation_command = self.options.preparation_command.as_ref().map(|values| {
|
|
|
|
|
|
let preparation_command = if values.len() == 1 {
|
|
|
|
|
|
&values[0]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
&values[self.number]
|
|
|
|
|
|
};
|
|
|
|
|
|
Command::new_parametrized(
|
|
|
|
|
|
None,
|
|
|
|
|
|
preparation_command,
|
|
|
|
|
|
self.command.get_parameters().iter().cloned(),
|
|
|
|
|
|
)
|
|
|
|
|
|
});
|
|
|
|
|
|
let run_preparation_command = || {
|
2022-02-22 08:58:57 +01:00
|
|
|
|
preparation_command
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|cmd| self.run_preparation_command(cmd))
|
|
|
|
|
|
.transpose()
|
2022-02-21 08:46:10 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.run_setup_command(self.command.get_parameters().iter().cloned())?;
|
|
|
|
|
|
|
|
|
|
|
|
// Warmup phase
|
|
|
|
|
|
if self.options.warmup_count > 0 {
|
|
|
|
|
|
let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {
|
|
|
|
|
|
Some(get_progress_bar(
|
|
|
|
|
|
self.options.warmup_count,
|
|
|
|
|
|
"Performing warmup runs",
|
|
|
|
|
|
self.options.output_style,
|
|
|
|
|
|
))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
for _ in 0..self.options.warmup_count {
|
|
|
|
|
|
let _ = run_preparation_command()?;
|
2022-02-22 08:36:56 +01:00
|
|
|
|
let _ = self.executor.time_command(self.command, None)?;
|
2022-02-21 08:46:10 +01:00
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.inc(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.finish_and_clear()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set up progress bar (and spinner for initial measurement)
|
|
|
|
|
|
let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {
|
|
|
|
|
|
Some(get_progress_bar(
|
|
|
|
|
|
self.options.run_bounds.min,
|
|
|
|
|
|
"Initial time measurement",
|
|
|
|
|
|
self.options.output_style,
|
|
|
|
|
|
))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let preparation_result = run_preparation_command()?;
|
2022-02-22 08:58:57 +01:00
|
|
|
|
let preparation_overhead =
|
|
|
|
|
|
preparation_result.map_or(0.0, |res| res.time_real + self.executor.time_overhead());
|
2022-02-21 08:46:10 +01:00
|
|
|
|
|
|
|
|
|
|
// Initial timing run
|
2022-02-22 08:36:56 +01:00
|
|
|
|
let (res, status) = self.executor.time_command(self.command, None)?;
|
2022-02-21 08:46:10 +01:00
|
|
|
|
let success = status.success();
|
|
|
|
|
|
|
|
|
|
|
|
// Determine number of benchmark runs
|
|
|
|
|
|
let runs_in_min_time = (self.options.min_benchmarking_time
|
2022-02-22 08:58:57 +01:00
|
|
|
|
/ (res.time_real + self.executor.time_overhead() + preparation_overhead))
|
2022-02-21 08:46:10 +01:00
|
|
|
|
as u64;
|
|
|
|
|
|
|
|
|
|
|
|
let count = {
|
|
|
|
|
|
let min = cmp::max(runs_in_min_time, self.options.run_bounds.min);
|
|
|
|
|
|
|
|
|
|
|
|
self.options
|
|
|
|
|
|
.run_bounds
|
|
|
|
|
|
.max
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|max| cmp::min(min, *max))
|
|
|
|
|
|
.unwrap_or(min)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let count_remaining = count - 1;
|
|
|
|
|
|
|
|
|
|
|
|
// Save the first result
|
|
|
|
|
|
times_real.push(res.time_real);
|
|
|
|
|
|
times_user.push(res.time_user);
|
|
|
|
|
|
times_system.push(res.time_system);
|
|
|
|
|
|
exit_codes.push(extract_exit_code(status));
|
|
|
|
|
|
|
|
|
|
|
|
all_succeeded = all_succeeded && success;
|
|
|
|
|
|
|
|
|
|
|
|
// Re-configure the progress bar
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.set_length(count)
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.inc(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Gather statistics (perform the actual benchmark)
|
|
|
|
|
|
for _ in 0..count_remaining {
|
|
|
|
|
|
run_preparation_command()?;
|
|
|
|
|
|
|
|
|
|
|
|
let msg = {
|
|
|
|
|
|
let mean = format_duration(mean(×_real), self.options.time_unit);
|
|
|
|
|
|
format!("Current estimate: {}", mean.to_string().green())
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.set_message(msg.to_owned())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-22 08:36:56 +01:00
|
|
|
|
let (res, status) = self.executor.time_command(self.command, None)?;
|
2022-02-21 08:46:10 +01:00
|
|
|
|
let success = status.success();
|
|
|
|
|
|
|
|
|
|
|
|
times_real.push(res.time_real);
|
|
|
|
|
|
times_user.push(res.time_user);
|
|
|
|
|
|
times_system.push(res.time_system);
|
|
|
|
|
|
exit_codes.push(extract_exit_code(status));
|
|
|
|
|
|
|
|
|
|
|
|
all_succeeded = all_succeeded && success;
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.inc(1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(bar) = progress_bar.as_ref() {
|
|
|
|
|
|
bar.finish_and_clear()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compute statistical quantities
|
|
|
|
|
|
let t_num = times_real.len();
|
|
|
|
|
|
let t_mean = mean(×_real);
|
|
|
|
|
|
let t_stddev = if times_real.len() > 1 {
|
|
|
|
|
|
Some(standard_deviation(×_real, Some(t_mean)))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
|
|
|
|
|
let t_median = median(×_real);
|
|
|
|
|
|
let t_min = min(×_real);
|
|
|
|
|
|
let t_max = max(×_real);
|
|
|
|
|
|
|
|
|
|
|
|
let user_mean = mean(×_user);
|
|
|
|
|
|
let system_mean = mean(×_system);
|
|
|
|
|
|
|
|
|
|
|
|
// Formatting and console output
|
|
|
|
|
|
let (mean_str, time_unit) = format_duration_unit(t_mean, self.options.time_unit);
|
|
|
|
|
|
let min_str = format_duration(t_min, Some(time_unit));
|
|
|
|
|
|
let max_str = format_duration(t_max, Some(time_unit));
|
|
|
|
|
|
let num_str = format!("{} runs", t_num);
|
|
|
|
|
|
|
|
|
|
|
|
let user_str = format_duration(user_mean, Some(time_unit));
|
|
|
|
|
|
let system_str = format_duration(system_mean, Some(time_unit));
|
|
|
|
|
|
|
|
|
|
|
|
if self.options.output_style != OutputStyleOption::Disabled {
|
|
|
|
|
|
if times_real.len() == 1 {
|
|
|
|
|
|
println!(
|
|
|
|
|
|
" Time ({} ≡): {:>8} {:>8} [User: {}, System: {}]",
|
|
|
|
|
|
"abs".green().bold(),
|
|
|
|
|
|
mean_str.green().bold(),
|
|
|
|
|
|
" ".to_string(), // alignment
|
|
|
|
|
|
user_str.blue(),
|
|
|
|
|
|
system_str.blue()
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let stddev_str = format_duration(t_stddev.unwrap(), Some(time_unit));
|
|
|
|
|
|
|
|
|
|
|
|
println!(
|
|
|
|
|
|
" Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",
|
|
|
|
|
|
"mean".green().bold(),
|
|
|
|
|
|
"σ".green(),
|
|
|
|
|
|
mean_str.green().bold(),
|
|
|
|
|
|
stddev_str.green(),
|
|
|
|
|
|
user_str.blue(),
|
|
|
|
|
|
system_str.blue()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
println!(
|
|
|
|
|
|
" Range ({} … {}): {:>8} … {:>8} {}",
|
|
|
|
|
|
"min".cyan(),
|
|
|
|
|
|
"max".purple(),
|
|
|
|
|
|
min_str.cyan(),
|
|
|
|
|
|
max_str.purple(),
|
|
|
|
|
|
num_str.dimmed()
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Warnings
|
|
|
|
|
|
let mut warnings = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
// Check execution time
|
|
|
|
|
|
if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {
|
|
|
|
|
|
warnings.push(Warnings::FastExecutionTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check programm exit codes
|
|
|
|
|
|
if !all_succeeded {
|
|
|
|
|
|
warnings.push(Warnings::NonZeroExitCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Run outlier detection
|
|
|
|
|
|
let scores = modified_zscores(×_real);
|
|
|
|
|
|
if scores[0] > OUTLIER_THRESHOLD {
|
|
|
|
|
|
warnings.push(Warnings::SlowInitialRun(times_real[0]));
|
|
|
|
|
|
} else if scores.iter().any(|&s| s.abs() > OUTLIER_THRESHOLD) {
|
|
|
|
|
|
warnings.push(Warnings::OutliersDetected);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !warnings.is_empty() {
|
|
|
|
|
|
eprintln!(" ");
|
|
|
|
|
|
|
|
|
|
|
|
for warning in &warnings {
|
|
|
|
|
|
eprintln!(" {}: {}", "Warning".yellow(), warning);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if self.options.output_style != OutputStyleOption::Disabled {
|
|
|
|
|
|
println!(" ");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.run_cleanup_command(self.command.get_parameters().iter().cloned())?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(BenchmarkResult {
|
|
|
|
|
|
command: command_name,
|
|
|
|
|
|
mean: t_mean,
|
|
|
|
|
|
stddev: t_stddev,
|
|
|
|
|
|
median: t_median,
|
|
|
|
|
|
user: user_mean,
|
|
|
|
|
|
system: system_mean,
|
|
|
|
|
|
min: t_min,
|
|
|
|
|
|
max: t_max,
|
|
|
|
|
|
times: Some(times_real),
|
|
|
|
|
|
exit_codes,
|
|
|
|
|
|
parameters: self
|
|
|
|
|
|
.command
|
|
|
|
|
|
.get_parameters()
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|(name, value)| (name.to_string(), value.to_string()))
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|