2018-01-13 21:01:30 +01:00
|
|
|
use std::cmp;
|
2018-03-24 18:30:52 -04:00
|
|
|
use std::env;
|
2018-05-22 22:57:51 +02:00
|
|
|
use std::error::Error;
|
2018-01-14 11:41:27 +13:00
|
|
|
use std::io;
|
2019-08-22 00:50:20 +05:30
|
|
|
use std::iter::StepBy;
|
2018-03-24 15:46:28 +01:00
|
|
|
use std::ops::Range;
|
2018-01-13 16:50:02 +01:00
|
|
|
|
2018-01-28 08:35:52 -05:00
|
|
|
use atty::Stream;
|
2018-03-24 18:30:52 -04:00
|
|
|
use clap::ArgMatches;
|
2018-05-22 22:57:51 +02:00
|
|
|
use colored::*;
|
2018-01-13 16:50:02 +01:00
|
|
|
|
2018-01-14 20:14:10 +01:00
|
|
|
mod hyperfine;
|
2018-01-14 10:48:47 +01:00
|
|
|
|
2019-05-07 11:55:38 +02:00
|
|
|
use crate::hyperfine::app::get_arg_matches;
|
|
|
|
|
use crate::hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
|
|
|
|
|
use crate::hyperfine::error::{OptionsError, ParameterScanError};
|
|
|
|
|
use crate::hyperfine::export::{ExportManager, ExportType};
|
|
|
|
|
use crate::hyperfine::internal::write_benchmark_comparison;
|
|
|
|
|
use crate::hyperfine::types::{
|
2018-11-01 20:52:25 -07:00
|
|
|
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
|
2018-05-22 22:57:51 +02:00
|
|
|
};
|
2019-05-07 11:55:38 +02:00
|
|
|
use crate::hyperfine::units::Unit;
|
2018-01-14 10:48:47 +01:00
|
|
|
|
2018-01-14 11:41:27 +13:00
|
|
|
/// Print error message to stderr and terminate
|
|
|
|
|
pub fn error(message: &str) -> ! {
|
2018-01-24 20:12:17 +01:00
|
|
|
eprintln!("{} {}", "Error:".red(), message);
|
2018-01-14 11:41:27 +13:00
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 12:03:34 +01:00
|
|
|
/// Runs the benchmark for the given commands
|
2019-06-08 11:47:50 +02:00
|
|
|
fn run(
|
2019-07-13 10:29:05 +02:00
|
|
|
commands: &[Command<'_>],
|
2019-06-08 11:47:50 +02:00
|
|
|
options: &HyperfineOptions,
|
|
|
|
|
) -> io::Result<Vec<BenchmarkResult>> {
|
2018-11-21 19:55:58 +01:00
|
|
|
let shell_spawning_time =
|
2019-07-13 10:38:51 +02:00
|
|
|
mean_shell_spawning_time(&options.shell, options.output_style, options.show_output)?;
|
2018-01-14 12:03:34 +01:00
|
|
|
|
2018-03-18 15:13:24 -04:00
|
|
|
let mut timing_results = vec![];
|
|
|
|
|
|
2018-01-14 12:03:34 +01:00
|
|
|
// Run the benchmarks
|
2018-01-14 13:34:22 +01:00
|
|
|
for (num, cmd) in commands.iter().enumerate() {
|
2018-03-18 15:13:24 -04:00
|
|
|
timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);
|
2018-01-14 12:03:34 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-18 15:13:24 -04:00
|
|
|
Ok(timing_results)
|
2018-01-14 12:03:34 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-24 20:17:17 +01:00
|
|
|
/// A function to read the `--parameter-scan` arguments
|
|
|
|
|
fn parse_parameter_scan_args<'a>(
|
2019-08-22 00:50:20 +05:30
|
|
|
mut vals: clap::Values<'a>, step_size: &str
|
|
|
|
|
) -> Result<(&'a str, StepBy<Range<i32>>), ParameterScanError> {
|
2018-03-24 15:46:28 +01:00
|
|
|
let param_name = vals.next().unwrap();
|
|
|
|
|
let param_min: i32 = vals.next().unwrap().parse()?;
|
|
|
|
|
let param_max: i32 = vals.next().unwrap().parse()?;
|
2019-08-22 00:50:20 +05:30
|
|
|
let step_size = step_size.parse()?;
|
2018-03-24 15:46:28 +01:00
|
|
|
|
2019-07-13 10:08:40 +02:00
|
|
|
const MAX_PARAMETERS: i32 = 100_000;
|
2018-03-24 15:46:28 +01:00
|
|
|
if param_max - param_min > MAX_PARAMETERS {
|
2018-03-24 20:17:17 +01:00
|
|
|
return Err(ParameterScanError::TooLarge);
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if param_max < param_min {
|
2018-03-24 20:17:17 +01:00
|
|
|
return Err(ParameterScanError::EmptyRange);
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 00:50:20 +05:30
|
|
|
if step_size == 0 {
|
|
|
|
|
// Iterator::step_by panics for zero step
|
|
|
|
|
return Err(ParameterScanError::ZeroStep);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let param_range = (param_min..(param_max + 1)).step_by(step_size);
|
|
|
|
|
|
|
|
|
|
Ok((param_name, param_range))
|
2018-03-24 15:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-13 16:50:02 +01:00
|
|
|
fn main() {
|
2018-03-24 18:30:52 -04:00
|
|
|
let matches = get_arg_matches(env::args_os());
|
|
|
|
|
let options = build_hyperfine_options(&matches);
|
|
|
|
|
let export_manager = build_export_manager(&matches);
|
|
|
|
|
let commands = build_commands(&matches);
|
|
|
|
|
|
2018-11-01 20:52:25 -07:00
|
|
|
let res = match options {
|
|
|
|
|
Ok(ref opts) => run(&commands, &opts),
|
|
|
|
|
Err(ref e) => error(e.description()),
|
2018-09-09 14:10:36 +01:00
|
|
|
};
|
2018-03-24 18:30:52 -04:00
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
Ok(timing_results) => {
|
|
|
|
|
write_benchmark_comparison(&timing_results);
|
2018-10-29 18:44:44 -07:00
|
|
|
let ans = export_manager.write_results(timing_results, options.unwrap().time_unit);
|
2018-03-24 18:30:52 -04:00
|
|
|
if let Err(e) = ans {
|
|
|
|
|
error(&format!(
|
2018-06-16 02:20:31 +08:00
|
|
|
"The following error occurred while exporting: {}",
|
2018-03-24 18:30:52 -04:00
|
|
|
e.description()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => error(e.description()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build the HyperfineOptions that correspond to the given ArgMatches
|
2019-05-07 11:55:38 +02:00
|
|
|
fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions, OptionsError> {
|
2018-01-16 21:26:14 +01:00
|
|
|
let mut options = HyperfineOptions::default();
|
2019-07-12 21:58:40 +02:00
|
|
|
let param_to_u64 = |param| matches.value_of(param).and_then(|n| u64::from_str_radix(n, 10).ok());
|
2018-01-14 12:39:58 +01:00
|
|
|
|
2019-07-12 21:58:40 +02:00
|
|
|
options.warmup_count = param_to_u64("warmup")
|
2018-10-30 19:30:17 -07:00
|
|
|
.unwrap_or(options.warmup_count);
|
2018-01-13 21:01:30 +01:00
|
|
|
|
2019-07-12 21:58:40 +02:00
|
|
|
let mut min_runs = param_to_u64("min-runs");
|
|
|
|
|
let mut max_runs = param_to_u64("max-runs");
|
2018-09-09 14:10:36 +01:00
|
|
|
|
2019-07-12 21:58:40 +02:00
|
|
|
if let Some(runs) = param_to_u64("runs") {
|
2018-09-09 14:10:36 +01:00
|
|
|
min_runs = Some(runs);
|
|
|
|
|
max_runs = Some(runs);
|
2018-01-13 21:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-09 14:10:36 +01:00
|
|
|
match (min_runs, max_runs) {
|
|
|
|
|
(Some(min), _) if min < 2 => {
|
|
|
|
|
// We need at least two runs to compute a variance.
|
|
|
|
|
return Err(OptionsError::RunsBelowTwo);
|
|
|
|
|
}
|
|
|
|
|
(Some(min), None) => {
|
|
|
|
|
options.runs.min = min;
|
|
|
|
|
}
|
|
|
|
|
(_, Some(max)) if max < 2 => {
|
|
|
|
|
// We need at least two runs to compute a variance.
|
|
|
|
|
return Err(OptionsError::RunsBelowTwo);
|
|
|
|
|
}
|
|
|
|
|
(None, Some(max)) => {
|
|
|
|
|
// Since the minimum was not explicit we lower it if max is below the default min.
|
|
|
|
|
options.runs.min = cmp::min(options.runs.min, max);
|
2018-09-09 21:52:58 +01:00
|
|
|
options.runs.max = Some(max);
|
2018-09-09 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
(Some(min), Some(max)) if min > max => {
|
|
|
|
|
return Err(OptionsError::EmptyRunsRange);
|
|
|
|
|
}
|
|
|
|
|
(Some(min), Some(max)) => {
|
|
|
|
|
options.runs.min = min;
|
2018-09-09 21:52:58 +01:00
|
|
|
options.runs.max = Some(max);
|
2018-09-09 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
(None, None) => {}
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-18 18:13:12 +01:00
|
|
|
options.preparation_command = matches.value_of("prepare").map(String::from);
|
|
|
|
|
|
2019-01-07 23:12:30 -08:00
|
|
|
options.cleanup_command = matches.value_of("cleanup").map(String::from);
|
|
|
|
|
|
2018-05-21 21:04:30 +02:00
|
|
|
options.show_output = matches.is_present("show-output");
|
2018-05-21 18:14:53 +02:00
|
|
|
|
2018-01-28 08:35:52 -05:00
|
|
|
options.output_style = match matches.value_of("style") {
|
|
|
|
|
Some("full") => OutputStyleOption::Full,
|
|
|
|
|
Some("basic") => OutputStyleOption::Basic,
|
2018-02-13 20:40:43 -05:00
|
|
|
Some("nocolor") => OutputStyleOption::NoColor,
|
2018-09-15 10:11:35 -07:00
|
|
|
Some("color") => OutputStyleOption::Color,
|
2018-09-27 23:07:07 +02:00
|
|
|
_ => {
|
|
|
|
|
if !options.show_output && atty::is(Stream::Stdout) {
|
|
|
|
|
OutputStyleOption::Full
|
|
|
|
|
} else {
|
|
|
|
|
OutputStyleOption::Basic
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-28 08:35:52 -05:00
|
|
|
};
|
2018-02-04 20:19:24 -05:00
|
|
|
|
2018-02-13 21:00:22 -05:00
|
|
|
// We default Windows to NoColor if full had been specified.
|
2018-02-13 20:40:43 -05:00
|
|
|
if cfg!(windows) && options.output_style == OutputStyleOption::Full {
|
|
|
|
|
options.output_style = OutputStyleOption::NoColor;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 10:11:35 -07:00
|
|
|
match options.output_style {
|
2018-09-28 21:25:18 +02:00
|
|
|
OutputStyleOption::Basic | OutputStyleOption::NoColor => {
|
|
|
|
|
colored::control::set_override(false)
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2018-10-30 19:30:17 -07:00
|
|
|
};
|
|
|
|
|
|
2018-11-21 19:55:58 +01:00
|
|
|
options.shell = matches
|
|
|
|
|
.value_of("shell")
|
|
|
|
|
.unwrap_or(&options.shell)
|
|
|
|
|
.to_string();
|
2018-02-02 14:42:31 +01:00
|
|
|
|
2018-01-18 18:39:17 +01:00
|
|
|
if matches.is_present("ignore-failure") {
|
|
|
|
|
options.failure_action = CmdFailureAction::Ignore;
|
|
|
|
|
}
|
2018-01-16 21:36:41 +01:00
|
|
|
|
2018-10-29 18:44:44 -07:00
|
|
|
options.time_unit = match matches.value_of("time-unit") {
|
|
|
|
|
Some("millisecond") => Some(Unit::MilliSecond),
|
|
|
|
|
Some("second") => Some(Unit::Second),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-09 14:10:36 +01:00
|
|
|
Ok(options)
|
2018-03-24 18:30:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build the ExportManager that will export the results specified
|
|
|
|
|
/// in the given ArgMatches
|
2019-05-07 11:55:38 +02:00
|
|
|
fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {
|
2018-03-24 18:30:52 -04:00
|
|
|
let mut export_manager = ExportManager::new();
|
2019-02-24 18:57:33 +01:00
|
|
|
{
|
|
|
|
|
let mut add_exporter = |flag, exporttype| {
|
|
|
|
|
if let Some(filename) = matches.value_of(flag) {
|
|
|
|
|
export_manager.add_exporter(exporttype, filename);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
add_exporter("export-asciidoc", ExportType::Asciidoc);
|
|
|
|
|
add_exporter("export-json", ExportType::Json);
|
|
|
|
|
add_exporter("export-csv", ExportType::Csv);
|
|
|
|
|
add_exporter("export-markdown", ExportType::Markdown);
|
2018-03-24 18:30:52 -04:00
|
|
|
}
|
|
|
|
|
export_manager
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Build the commands to benchmark
|
2019-05-07 11:55:38 +02:00
|
|
|
fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {
|
2018-03-24 15:46:28 +01:00
|
|
|
let command_strings = matches.values_of("command").unwrap();
|
|
|
|
|
|
2019-07-12 21:51:08 +02:00
|
|
|
if let Some(args) = matches.values_of("parameter-scan") {
|
2019-08-22 00:50:20 +05:30
|
|
|
let step_size = matches.value_of("parameter-step-size").unwrap_or("1");
|
|
|
|
|
match parse_parameter_scan_args(args, step_size) {
|
2018-03-24 15:46:28 +01:00
|
|
|
Ok((param_name, param_range)) => {
|
|
|
|
|
let mut commands = vec![];
|
|
|
|
|
let command_strings = command_strings.collect::<Vec<&str>>();
|
2019-08-22 00:50:20 +05:30
|
|
|
for value in param_range {
|
2019-07-13 10:19:57 +02:00
|
|
|
for cmd in &command_strings {
|
2018-03-24 15:46:28 +01:00
|
|
|
commands.push(Command::new_parametrized(cmd, param_name, value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
commands
|
|
|
|
|
}
|
|
|
|
|
Err(e) => error(e.description()),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-07-12 21:55:32 +02:00
|
|
|
command_strings.map(Command::new).collect()
|
2019-07-12 21:51:08 +02:00
|
|
|
}
|
2018-01-13 16:50:02 +01:00
|
|
|
}
|