2021-10-04 22:34:19 +09:00
|
|
|
use std::fmt;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
use crate::error::OptionsError;
|
2021-08-24 21:33:48 +02:00
|
|
|
use crate::units::{Second, Unit};
|
2021-08-22 19:04:22 +02:00
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
pub const DEFAULT_SHELL: &str = "sh";
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
pub const DEFAULT_SHELL: &str = "cmd.exe";
|
2021-08-22 17:53:08 +02:00
|
|
|
|
2021-10-04 22:34:19 +09:00
|
|
|
/// Shell to use for executing benchmarked commands
|
|
|
|
|
pub enum Shell {
|
|
|
|
|
/// Default shell command
|
|
|
|
|
Default(&'static str),
|
|
|
|
|
/// Custom shell command specified via --shell
|
|
|
|
|
Custom(Vec<String>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Shell {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Shell::Default(DEFAULT_SHELL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Shell {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Shell::Default(cmd) => write!(f, "{}", cmd),
|
|
|
|
|
Shell::Custom(cmdline) => {
|
|
|
|
|
let mut first = true;
|
|
|
|
|
for s in cmdline.iter() {
|
|
|
|
|
if first {
|
|
|
|
|
first = false;
|
|
|
|
|
write!(f, "{}", s)?;
|
|
|
|
|
} else {
|
|
|
|
|
write!(f, " {}", s)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Shell {
|
|
|
|
|
/// Parse given string as shell command line
|
|
|
|
|
pub fn parse<'a>(s: &str) -> Result<Self, OptionsError<'a>> {
|
|
|
|
|
let v = shell_words::split(s.as_ref()).map_err(OptionsError::ShellParseError)?;
|
|
|
|
|
if v.is_empty() {
|
|
|
|
|
return Err(OptionsError::EmptyShell);
|
|
|
|
|
}
|
|
|
|
|
Ok(Shell::Custom(v))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn command(&self) -> Command {
|
|
|
|
|
match self {
|
|
|
|
|
Shell::Default(cmd) => Command::new(cmd),
|
|
|
|
|
Shell::Custom(cmdline) => {
|
|
|
|
|
let mut c = Command::new(&cmdline[0]);
|
|
|
|
|
c.args(&cmdline[1..]);
|
|
|
|
|
c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 17:53:08 +02: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,
|
|
|
|
|
|
|
|
|
|
/// Keep coloring, but use no progress bar
|
|
|
|
|
Color,
|
|
|
|
|
|
|
|
|
|
/// Disable all the output
|
|
|
|
|
Disabled,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Number of runs for a benchmark
|
|
|
|
|
pub struct Runs {
|
|
|
|
|
/// Minimum number of benchmark runs
|
|
|
|
|
pub min: u64,
|
|
|
|
|
|
|
|
|
|
/// Maximum number of benchmark runs
|
|
|
|
|
pub max: Option<u64>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Runs {
|
|
|
|
|
fn default() -> Runs {
|
|
|
|
|
Runs { min: 10, max: None }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A set of options for hyperfine
|
|
|
|
|
pub struct HyperfineOptions {
|
|
|
|
|
/// Number of warmup runs
|
|
|
|
|
pub warmup_count: u64,
|
|
|
|
|
|
|
|
|
|
/// Number of benchmark runs
|
|
|
|
|
pub runs: Runs,
|
|
|
|
|
|
|
|
|
|
/// 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<Vec<String>>,
|
|
|
|
|
|
|
|
|
|
/// Command to run after each benchmark
|
|
|
|
|
pub cleanup_command: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// What color mode to use for output
|
|
|
|
|
pub output_style: OutputStyleOption,
|
|
|
|
|
|
|
|
|
|
/// The shell to use for executing commands.
|
2021-10-04 22:34:19 +09:00
|
|
|
pub shell: Shell,
|
2021-08-22 17:53:08 +02:00
|
|
|
|
|
|
|
|
/// Forward benchmark's stdout to hyperfine's stdout
|
|
|
|
|
pub show_output: bool,
|
|
|
|
|
|
|
|
|
|
/// Which time unit to use for CLI & Markdown output
|
|
|
|
|
pub time_unit: Option<Unit>,
|
|
|
|
|
|
|
|
|
|
/// A list of custom command names that, if defined,
|
|
|
|
|
/// will be used instead of the command itself in
|
|
|
|
|
/// benchmark outputs.
|
|
|
|
|
pub names: Option<Vec<String>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for HyperfineOptions {
|
|
|
|
|
fn default() -> HyperfineOptions {
|
|
|
|
|
HyperfineOptions {
|
|
|
|
|
names: None,
|
|
|
|
|
warmup_count: 0,
|
|
|
|
|
runs: Runs::default(),
|
|
|
|
|
min_time_sec: 3.0,
|
|
|
|
|
failure_action: CmdFailureAction::RaiseError,
|
|
|
|
|
preparation_command: None,
|
|
|
|
|
cleanup_command: None,
|
|
|
|
|
output_style: OutputStyleOption::Full,
|
2021-10-04 22:34:19 +09:00
|
|
|
shell: Shell::default(),
|
2021-08-22 17:53:08 +02:00
|
|
|
show_output: false,
|
|
|
|
|
time_unit: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|