2018-03-24 15:46:28 +01:00
|
|
|
/// This module contains common internal types.
|
|
|
|
|
use std::fmt;
|
2018-03-22 21:53:36 -04:00
|
|
|
|
2018-11-01 20:52:25 -07:00
|
|
|
use hyperfine::units::{Second, Unit};
|
|
|
|
|
|
2018-10-30 19:30:17 -07:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
pub const DEFAULT_SHELL: &str = "sh";
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub const DEFAULT_SHELL: &str = "cmd.exe";
|
|
|
|
|
|
2018-03-24 15:46:28 +01:00
|
|
|
/// A command that should be benchmarked.
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Command<'a> {
|
|
|
|
|
/// The command that should be executed (without parameter substitution)
|
|
|
|
|
expression: &'a str,
|
|
|
|
|
|
|
|
|
|
/// A possible parameter value.
|
|
|
|
|
parameter: Option<(&'a str, i32)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Command<'a> {
|
|
|
|
|
pub fn new(expression: &'a str) -> Command<'a> {
|
|
|
|
|
Command {
|
|
|
|
|
expression: expression,
|
|
|
|
|
parameter: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_parametrized(expression: &'a str, parameter: &'a str, value: i32) -> Command<'a> {
|
|
|
|
|
Command {
|
|
|
|
|
expression: expression,
|
|
|
|
|
parameter: Some((parameter, value)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_shell_command(&self) -> String {
|
|
|
|
|
match self.parameter {
|
|
|
|
|
Some((param_name, param_value)) => self.expression.replace(
|
|
|
|
|
&format!("{{{param_name}}}", param_name = param_name),
|
|
|
|
|
¶m_value.to_string(),
|
|
|
|
|
),
|
|
|
|
|
None => self.expression.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-04 00:13:06 +03:00
|
|
|
|
|
|
|
|
pub fn get_parameter(&self) -> Option<(&'a str, i32)> {
|
|
|
|
|
self.parameter
|
|
|
|
|
}
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for Command<'a> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(f, "{}", self.get_shell_command())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// Action to take when an executed command fails.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub enum CmdFailureAction {
|
|
|
|
|
/// Exit with an error message
|
|
|
|
|
RaiseError,
|
|
|
|
|
|
|
|
|
|
/// Simply ignore the non-zero exit code
|
|
|
|
|
Ignore,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Output style type option
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub enum OutputStyleOption {
|
|
|
|
|
/// Do not output with colors or any special formatting
|
|
|
|
|
Basic,
|
|
|
|
|
|
|
|
|
|
/// Output with full color and formatting
|
|
|
|
|
Full,
|
|
|
|
|
|
|
|
|
|
/// Keep elements such as progress bar, but use no coloring
|
|
|
|
|
NoColor,
|
2018-09-15 10:11:35 -07:00
|
|
|
|
|
|
|
|
/// Keep coloring, but use no progress bar
|
|
|
|
|
Color,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-09 14:10:36 +01:00
|
|
|
/// Number of runs for a benchmark
|
|
|
|
|
pub struct Runs {
|
|
|
|
|
/// Minimum number of benchmark runs
|
|
|
|
|
pub min: u64,
|
|
|
|
|
|
|
|
|
|
/// Maximum number of benchmark runs
|
2018-09-09 21:52:58 +01:00
|
|
|
pub max: Option<u64>,
|
2018-09-09 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Runs {
|
|
|
|
|
fn default() -> Runs {
|
2018-09-27 23:07:07 +02:00
|
|
|
Runs { min: 10, max: None }
|
2018-09-09 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// A set of options for hyperfine
|
|
|
|
|
pub struct HyperfineOptions {
|
|
|
|
|
/// Number of warmup runs
|
|
|
|
|
pub warmup_count: u64,
|
|
|
|
|
|
2018-09-09 14:10:36 +01:00
|
|
|
/// Number of benchmark runs
|
|
|
|
|
pub runs: Runs,
|
2018-03-22 21:53:36 -04:00
|
|
|
|
|
|
|
|
/// Minimum benchmarking time
|
|
|
|
|
pub min_time_sec: Second,
|
|
|
|
|
|
|
|
|
|
/// Whether or not to ignore non-zero exit codes
|
|
|
|
|
pub failure_action: CmdFailureAction,
|
|
|
|
|
|
|
|
|
|
/// Command to run before each timing run
|
|
|
|
|
pub preparation_command: Option<String>,
|
|
|
|
|
|
2019-01-07 23:12:30 -08:00
|
|
|
/// Command to run after each benchmark
|
|
|
|
|
pub cleanup_command: Option<String>,
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// What color mode to use for output
|
|
|
|
|
pub output_style: OutputStyleOption,
|
2018-05-21 18:14:53 +02:00
|
|
|
|
2018-10-30 19:30:17 -07:00
|
|
|
/// The shell to use for executing commands.
|
|
|
|
|
pub shell: String,
|
|
|
|
|
|
2018-05-21 18:14:53 +02:00
|
|
|
/// Forward benchmark's stdout to hyperfine's stdout
|
2018-05-21 21:04:30 +02:00
|
|
|
pub show_output: bool,
|
2018-10-29 18:44:44 -07:00
|
|
|
|
|
|
|
|
/// Which time unit to use for CLI & Markdown output
|
|
|
|
|
pub time_unit: Option<Unit>,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for HyperfineOptions {
|
|
|
|
|
fn default() -> HyperfineOptions {
|
|
|
|
|
HyperfineOptions {
|
|
|
|
|
warmup_count: 0,
|
2018-09-09 14:10:36 +01:00
|
|
|
runs: Runs::default(),
|
2018-03-22 21:53:36 -04:00
|
|
|
min_time_sec: 3.0,
|
|
|
|
|
failure_action: CmdFailureAction::RaiseError,
|
|
|
|
|
preparation_command: None,
|
2019-01-07 23:12:30 -08:00
|
|
|
cleanup_command: None,
|
2018-03-22 21:53:36 -04:00
|
|
|
output_style: OutputStyleOption::Full,
|
2018-10-30 19:30:17 -07:00
|
|
|
shell: DEFAULT_SHELL.to_string(),
|
2018-05-21 21:04:30 +02:00
|
|
|
show_output: false,
|
2018-10-29 18:44:44 -07:00
|
|
|
time_unit: None,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set of values that will be exported.
|
|
|
|
|
#[derive(Debug, Default, Clone, Serialize)]
|
|
|
|
|
pub struct BenchmarkResult {
|
|
|
|
|
/// The command that was run
|
|
|
|
|
pub command: String,
|
|
|
|
|
|
|
|
|
|
/// The mean run time
|
|
|
|
|
pub mean: Second,
|
|
|
|
|
|
|
|
|
|
/// The standard deviation of all run times
|
|
|
|
|
pub stddev: Second,
|
|
|
|
|
|
|
|
|
|
/// Time spend in user space
|
|
|
|
|
pub user: Second,
|
|
|
|
|
|
|
|
|
|
/// Time spent in system space
|
|
|
|
|
pub system: Second,
|
|
|
|
|
|
|
|
|
|
/// Min time measured
|
|
|
|
|
pub min: Second,
|
|
|
|
|
|
|
|
|
|
/// Max time measured
|
|
|
|
|
pub max: Second,
|
|
|
|
|
|
|
|
|
|
/// All run time measurements
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub times: Option<Vec<Second>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BenchmarkResult {
|
|
|
|
|
/// Create a new entry with the given values.
|
|
|
|
|
pub fn new(
|
|
|
|
|
command: String,
|
|
|
|
|
mean: Second,
|
|
|
|
|
stddev: Second,
|
|
|
|
|
user: Second,
|
|
|
|
|
system: Second,
|
|
|
|
|
min: Second,
|
|
|
|
|
max: Second,
|
|
|
|
|
times: Vec<Second>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command,
|
|
|
|
|
mean,
|
|
|
|
|
stddev,
|
|
|
|
|
user,
|
|
|
|
|
system,
|
|
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
times: Some(times),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|