2021-05-13 05:54:25 +00:00
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
2019-08-31 12:48:26 +05:30
|
|
|
use rust_decimal::Decimal;
|
2018-03-24 15:46:28 +01:00
|
|
|
/// This module contains common internal types.
|
2019-05-07 11:55:38 +02:00
|
|
|
use serde::*;
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2021-05-13 05:54:25 +00:00
|
|
|
use std::convert::TryFrom;
|
2018-03-24 15:46:28 +01:00
|
|
|
use std::fmt;
|
2018-03-22 21:53:36 -04:00
|
|
|
|
2019-05-07 11:55:38 +02:00
|
|
|
use crate::hyperfine::units::{Second, Unit};
|
2018-11-01 20:52:25 -07:00
|
|
|
|
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";
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]
|
2019-08-31 12:48:26 +05:30
|
|
|
#[serde(untagged)]
|
|
|
|
|
pub enum NumericType {
|
|
|
|
|
Int(i32),
|
|
|
|
|
Decimal(Decimal),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for NumericType {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {
|
|
|
|
|
NumericType::Int(i) => fmt::Display::fmt(&i, f),
|
|
|
|
|
NumericType::Decimal(i) => fmt::Display::fmt(&i, f),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 16:05:03 +02:00
|
|
|
impl From<i32> for NumericType {
|
|
|
|
|
fn from(x: i32) -> NumericType {
|
|
|
|
|
NumericType::Int(x)
|
2019-08-31 12:48:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 16:05:03 +02:00
|
|
|
impl From<Decimal> for NumericType {
|
|
|
|
|
fn from(x: Decimal) -> NumericType {
|
|
|
|
|
NumericType::Decimal(x)
|
2019-08-31 12:48:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 05:54:25 +00:00
|
|
|
impl TryFrom<NumericType> for usize {
|
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
|
|
fn try_from(numeric: NumericType) -> Result<Self, Self::Error> {
|
|
|
|
|
match numeric {
|
|
|
|
|
NumericType::Int(i) => usize::try_from(i).map_err(|_| ()),
|
|
|
|
|
NumericType::Decimal(d) => match d.to_u64() {
|
|
|
|
|
Some(u) => usize::try_from(u).map_err(|_| ()),
|
|
|
|
|
None => Err(()),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-10-20 13:18:00 +02:00
|
|
|
pub enum ParameterValue {
|
|
|
|
|
Text(String),
|
2019-10-20 09:54:47 +02:00
|
|
|
Numeric(NumericType),
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 13:18:00 +02:00
|
|
|
impl<'a> ToString for ParameterValue {
|
2019-10-20 09:54:47 +02:00
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
match self {
|
2019-10-20 13:18:00 +02:00
|
|
|
ParameterValue::Text(ref value) => value.clone(),
|
2019-10-20 09:54:47 +02:00
|
|
|
ParameterValue::Numeric(value) => value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 15:46:28 +01:00
|
|
|
/// A command that should be benchmarked.
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2018-03-24 15:46:28 +01:00
|
|
|
pub struct Command<'a> {
|
2020-12-15 13:43:53 +08:00
|
|
|
/// The command name (without parameter substitution)
|
|
|
|
|
name: Option<&'a str>,
|
|
|
|
|
|
2018-03-24 15:46:28 +01:00
|
|
|
/// The command that should be executed (without parameter substitution)
|
|
|
|
|
expression: &'a str,
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
/// Zero or more parameter values.
|
|
|
|
|
parameters: Vec<(&'a str, ParameterValue)>,
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Command<'a> {
|
2020-12-15 13:43:53 +08:00
|
|
|
pub fn new(name: Option<&'a str>, expression: &'a str) -> Command<'a> {
|
2018-03-24 15:46:28 +01:00
|
|
|
Command {
|
2020-12-15 13:43:53 +08:00
|
|
|
name,
|
2019-07-13 10:06:25 +02:00
|
|
|
expression,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters: Vec::new(),
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 12:48:26 +05:30
|
|
|
pub fn new_parametrized(
|
2020-12-15 13:43:53 +08:00
|
|
|
name: Option<&'a str>,
|
2019-08-31 12:48:26 +05:30
|
|
|
expression: &'a str,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters: Vec<(&'a str, ParameterValue)>,
|
2019-08-31 12:48:26 +05:30
|
|
|
) -> Command<'a> {
|
2018-03-24 15:46:28 +01:00
|
|
|
Command {
|
2020-12-15 13:43:53 +08:00
|
|
|
name,
|
2019-07-13 10:06:25 +02:00
|
|
|
expression,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters,
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 13:43:53 +08:00
|
|
|
pub fn get_name(&self) -> String {
|
|
|
|
|
self.name.map_or_else(
|
|
|
|
|
|| self.get_shell_command(),
|
|
|
|
|
|name| self.replace_parameters_in(name),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 15:46:28 +01:00
|
|
|
pub fn get_shell_command(&self) -> String {
|
2020-12-15 13:43:53 +08:00
|
|
|
self.replace_parameters_in(self.expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_parameters(&self) -> &Vec<(&'a str, ParameterValue)> {
|
|
|
|
|
&self.parameters
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_parameters_in(&self, original: &str) -> String {
|
2020-09-14 20:28:02 -07:00
|
|
|
let mut result = String::new();
|
|
|
|
|
let mut replacements = BTreeMap::<String, String>::new();
|
|
|
|
|
for (param_name, param_value) in &self.parameters {
|
|
|
|
|
replacements.insert(
|
|
|
|
|
format!("{{{param_name}}}", param_name = param_name),
|
|
|
|
|
param_value.to_string(),
|
|
|
|
|
);
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
2020-12-15 13:43:53 +08:00
|
|
|
let mut remaining = original;
|
2020-09-14 20:28:02 -07:00
|
|
|
// Manually replace consecutive occurrences to avoid double-replacing: e.g.,
|
|
|
|
|
//
|
|
|
|
|
// hyperfine -L foo 'a,{bar}' -L bar 'baz,quux' 'echo {foo} {bar}'
|
|
|
|
|
//
|
|
|
|
|
// 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()) {
|
2021-08-22 15:59:56 +02:00
|
|
|
result.push_str(v);
|
2020-09-14 20:28:02 -07:00
|
|
|
remaining = &remaining[k.len()..];
|
|
|
|
|
continue 'outer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.push(head);
|
|
|
|
|
remaining = &remaining[head.len_utf8()..];
|
|
|
|
|
}
|
|
|
|
|
result
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_get_shell_command_nonoverlapping() {
|
|
|
|
|
let cmd = Command::new_parametrized(
|
2020-12-15 13:43:53 +08:00
|
|
|
None,
|
2020-09-14 20:28:02 -07:00
|
|
|
"echo {foo} {bar}",
|
|
|
|
|
vec![
|
|
|
|
|
("foo", ParameterValue::Text("{bar} baz".into())),
|
|
|
|
|
("bar", ParameterValue::Text("quux".into())),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(cmd.get_shell_command(), "echo {bar} baz quux");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 13:43:53 +08:00
|
|
|
#[test]
|
|
|
|
|
fn test_get_parameterized_command_name() {
|
|
|
|
|
let cmd = Command::new_parametrized(
|
|
|
|
|
Some("name-{bar}-{foo}"),
|
|
|
|
|
"echo {foo} {bar}",
|
|
|
|
|
vec![
|
|
|
|
|
("foo", ParameterValue::Text("baz".into())),
|
|
|
|
|
("bar", ParameterValue::Text("quux".into())),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(cmd.get_name(), "name-quux-baz");
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 15:46:28 +01:00
|
|
|
impl<'a> fmt::Display for Command<'a> {
|
2019-05-07 11:55:38 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-03-24 15:46:28 +01:00
|
|
|
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,
|
2019-10-12 11:17:14 +03:00
|
|
|
|
|
|
|
|
/// Disable all the output
|
2019-10-14 00:03:29 +03:00
|
|
|
Disabled,
|
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
|
2019-10-02 18:56:55 +05:45
|
|
|
pub preparation_command: Option<Vec<String>>,
|
2018-03-22 21:53:36 -04:00
|
|
|
|
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>,
|
2020-10-09 21:06:55 +02:00
|
|
|
|
2020-10-13 14:14:39 +02:00
|
|
|
/// A list of custom command names that, if defined,
|
|
|
|
|
/// will be used instead of the command itself in
|
|
|
|
|
/// benchmark outputs.
|
2020-10-09 21:06:55 +02:00
|
|
|
pub names: Option<Vec<String>>,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for HyperfineOptions {
|
|
|
|
|
fn default() -> HyperfineOptions {
|
|
|
|
|
HyperfineOptions {
|
2020-10-09 21:06:55 +02:00
|
|
|
names: None,
|
2018-03-22 21:53:36 -04:00
|
|
|
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.
|
2020-09-14 20:28:02 -07:00
|
|
|
// NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the
|
|
|
|
|
// `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate.
|
2019-10-13 15:40:17 +02:00
|
|
|
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
|
2018-03-22 21:53:36 -04:00
|
|
|
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,
|
|
|
|
|
|
2019-06-08 13:04:39 +02:00
|
|
|
/// The median run time
|
|
|
|
|
pub median: Second,
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// 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>>,
|
2019-01-16 12:42:15 +01:00
|
|
|
|
2021-02-28 10:22:21 +01:00
|
|
|
/// All run exit codes
|
2021-05-09 18:20:59 +02:00
|
|
|
pub exit_codes: Vec<Option<i32>>,
|
2021-02-28 10:22:21 +01:00
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
/// Any parameter values used
|
|
|
|
|
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
|
|
|
|
pub parameters: BTreeMap<String, String>,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BenchmarkResult {
|
|
|
|
|
/// Create a new entry with the given values.
|
2021-08-22 16:05:03 +02:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2018-03-22 21:53:36 -04:00
|
|
|
pub fn new(
|
|
|
|
|
command: String,
|
|
|
|
|
mean: Second,
|
|
|
|
|
stddev: Second,
|
2019-06-08 13:04:39 +02:00
|
|
|
median: Second,
|
2018-03-22 21:53:36 -04:00
|
|
|
user: Second,
|
|
|
|
|
system: Second,
|
|
|
|
|
min: Second,
|
|
|
|
|
max: Second,
|
|
|
|
|
times: Vec<Second>,
|
2021-05-09 18:20:59 +02:00
|
|
|
exit_codes: Vec<Option<i32>>,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters: BTreeMap<String, String>,
|
2018-03-22 21:53:36 -04:00
|
|
|
) -> Self {
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command,
|
|
|
|
|
mean,
|
|
|
|
|
stddev,
|
2019-06-08 13:04:39 +02:00
|
|
|
median,
|
2018-03-22 21:53:36 -04:00
|
|
|
user,
|
|
|
|
|
system,
|
|
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
times: Some(times),
|
2021-02-28 10:22:21 +01:00
|
|
|
exit_codes,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|