Files
hyperfine/src/main.rs
T

236 lines
7.2 KiB
Rust
Raw Normal View History

extern crate atty;
2018-02-08 17:13:04 -05:00
#[macro_use]
extern crate cfg_if;
2018-01-13 20:53:55 +01:00
#[macro_use]
2018-01-13 16:50:02 +01:00
extern crate clap;
2018-01-24 20:12:17 +01:00
extern crate colored;
extern crate csv;
2018-01-13 19:01:52 +01:00
extern crate indicatif;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
2018-01-14 21:07:00 +01:00
extern crate statistical;
2018-01-13 16:50:02 +01:00
2018-03-24 18:30:52 -04:00
// Os-specific dependencies
2018-02-08 17:13:04 -05:00
cfg_if! {
if #[cfg(windows)] {
extern crate winapi;
} else {
extern crate libc;
}
}
2018-02-03 16:23:41 -05:00
2018-01-20 12:55:16 +01:00
#[cfg(test)]
#[macro_use]
extern crate approx;
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;
use std::ops::Range;
2018-01-13 16:50:02 +01: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
2018-03-24 18:30:52 -04:00
use hyperfine::app::get_arg_matches;
2018-05-22 22:57:51 +02:00
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
2018-09-27 23:07:07 +02:00
use hyperfine::error::{OptionsError, ParameterScanError};
2018-05-22 22:57:51 +02:00
use hyperfine::export::{ExportManager, ExportType};
use hyperfine::internal::write_benchmark_comparison;
use hyperfine::types::{
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
};
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
fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {
2018-05-22 22:57:51 +02:00
let shell_spawning_time = mean_shell_spawning_time(&options.output_style, options.show_output)?;
2018-01-14 12:03:34 +01: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() {
timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);
2018-01-14 12:03:34 +01: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>(
mut vals: clap::Values<'a>,
2018-03-24 20:17:17 +01:00
) -> Result<(&'a str, Range<i32>), ParameterScanError> {
let param_name = vals.next().unwrap();
let param_min: i32 = vals.next().unwrap().parse()?;
let param_max: i32 = vals.next().unwrap().parse()?;
const MAX_PARAMETERS: i32 = 100000;
if param_max - param_min > MAX_PARAMETERS {
2018-03-24 20:17:17 +01:00
return Err(ParameterScanError::TooLarge);
}
if param_max < param_min {
2018-03-24 20:17:17 +01:00
return Err(ParameterScanError::EmptyRange);
}
return Ok((param_name, param_min..(param_max + 1)));
}
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);
let res = match options {
Ok(opts) => run(&commands, &opts),
Err(e) => error(e.description()),
};
2018-03-24 18:30:52 -04:00
match res {
Ok(timing_results) => {
write_benchmark_comparison(&timing_results);
let ans = export_manager.write_results(timing_results);
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
fn build_hyperfine_options(matches: &ArgMatches) -> Result<HyperfineOptions, OptionsError> {
2018-01-16 21:26:14 +01:00
let mut options = HyperfineOptions::default();
let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();
2018-01-14 12:39:58 +01:00
2018-01-14 12:22:29 +01:00
options.warmup_count = matches
.value_of("warmup")
.and_then(&str_to_u64)
.unwrap_or(0);
2018-01-13 21:01:30 +01:00
let mut min_runs = matches.value_of("min-runs").and_then(&str_to_u64);
let mut max_runs = matches.value_of("max-runs").and_then(&str_to_u64);
if let Some(runs) = matches.value_of("runs").and_then(&str_to_u64) {
min_runs = Some(runs);
max_runs = Some(runs);
2018-01-13 21:01:30 +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);
}
(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);
}
(None, None) => {}
};
2018-01-18 18:13:12 +01:00
options.preparation_command = matches.value_of("prepare").map(String::from);
2018-05-21 21:04:30 +02:00
options.show_output = matches.is_present("show-output");
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,
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-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;
}
match options.output_style {
OutputStyleOption::Full | OutputStyleOption::Color => colored::control::unset_override(),
_ => colored::control::set_override(false),
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
Ok(options)
2018-03-24 18:30:52 -04:00
}
/// Build the ExportManager that will export the results specified
/// in the given ArgMatches
fn build_export_manager(matches: &ArgMatches) -> ExportManager {
let mut export_manager = ExportManager::new();
if let Some(filename) = matches.value_of("export-json") {
export_manager.add_exporter(ExportType::Json, filename);
}
if let Some(filename) = matches.value_of("export-csv") {
export_manager.add_exporter(ExportType::Csv, filename);
}
if let Some(filename) = matches.value_of("export-markdown") {
export_manager.add_exporter(ExportType::Markdown, filename);
}
export_manager
}
/// Build the commands to benchmark
fn build_commands<'a>(matches: &'a ArgMatches) -> Vec<Command<'a>> {
let command_strings = matches.values_of("command").unwrap();
2018-03-24 20:17:17 +01:00
let commands = if let Some(args) = matches.values_of("parameter-scan") {
match parse_parameter_scan_args(args) {
Ok((param_name, param_range)) => {
let mut commands = vec![];
let command_strings = command_strings.collect::<Vec<&str>>();
for value in param_range.start..param_range.end {
for ref cmd in &command_strings {
commands.push(Command::new_parametrized(cmd, param_name, value));
}
}
commands
}
Err(e) => error(e.description()),
}
} else {
command_strings.map(|c| Command::new(c)).collect()
};
2018-03-24 18:30:52 -04:00
commands
2018-01-13 16:50:02 +01:00
}