Files
hyperfine/src/main.rs
T

173 lines
4.9 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;
2018-01-13 19:01:52 +01:00
extern crate indicatif;
2018-01-14 21:07:00 +01:00
extern crate statistical;
2018-01-13 16:50:02 +01:00
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-01-14 11:41:27 +13:00
use std::error::Error;
use std::io;
2018-01-13 16:50:02 +01:00
use atty::Stream;
2018-01-24 20:12:17 +01:00
use colored::*;
2018-01-13 16:50:02 +01:00
use clap::{App, AppSettings, Arg};
2018-01-14 20:14:10 +01:00
mod hyperfine;
2018-01-14 10:48:47 +01:00
use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};
2018-01-14 20:14:10 +01:00
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
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<&str>, options: &HyperfineOptions) -> io::Result<()> {
let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;
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-01-18 20:16:36 +01:00
run_benchmark(num, cmd, shell_spawning_time, options)?;
2018-01-14 12:03:34 +01:00
}
Ok(())
}
2018-01-13 16:50:02 +01:00
fn main() {
2018-01-16 21:26:14 +01:00
// Process command line options
let mut options = HyperfineOptions::default();
let clap_color_setting = if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever
};
2018-01-13 16:50:02 +01:00
let matches = App::new("hyperfine")
2018-01-13 20:53:55 +01:00
.version(crate_version!())
.setting(clap_color_setting)
2018-01-13 20:53:55 +01:00
.setting(AppSettings::DeriveDisplayOrder)
2018-01-16 21:36:41 +01:00
.setting(AppSettings::UnifiedHelpMessage)
.max_term_width(90)
2018-01-18 18:17:23 +01:00
.about("A command-line benchmarking tool.")
2018-01-13 16:50:02 +01:00
.arg(
Arg::with_name("command")
.help("Command to benchmark")
.required(true)
2018-01-14 11:41:27 +13:00
.multiple(true)
.empty_values(false),
2018-01-13 16:50:02 +01:00
)
2018-01-13 20:05:31 +01:00
.arg(
Arg::with_name("warmup")
.long("warmup")
.short("w")
.takes_value(true)
.value_name("NUM")
2018-01-16 21:26:14 +01:00
.help(
"Perform NUM warmup runs before the actual benchmark. This can be used \
2018-01-18 18:17:23 +01:00
to fill (disk) caches for I/O-heavy programs.",
2018-01-16 21:26:14 +01:00
),
2018-01-13 20:05:31 +01:00
)
2018-01-13 21:01:30 +01:00
.arg(
Arg::with_name("min-runs")
.long("min-runs")
.short("m")
.takes_value(true)
.value_name("NUM")
2018-01-16 21:26:14 +01:00
.help(&format!(
2018-01-18 18:17:23 +01:00
"Perform at least NUM runs for each command (default: {}).",
2018-01-16 21:26:14 +01:00
options.min_runs
)),
2018-01-14 12:39:58 +01:00
)
2018-01-16 21:36:41 +01:00
.arg(
2018-01-18 18:13:12 +01:00
Arg::with_name("prepare")
.long("prepare")
.short("p")
2018-01-16 21:36:41 +01:00
.takes_value(true)
.value_name("CMD")
.help(
2018-01-18 18:13:12 +01:00
"Execute CMD before each timing run. This is useful for \
2018-01-18 18:17:23 +01:00
clearing disk caches, for example.",
2018-01-16 21:36:41 +01:00
),
)
.arg(
Arg::with_name("style")
.long("style")
.short("s")
.takes_value(true)
.value_name("TYPE")
.possible_values(&["auto", "basic", "full"])
.help(
2018-01-28 18:43:04 +01:00
"Set output style type (default: auto). Set this to 'basic' to disable output \
coloring and interactive elements. Set it to 'full' to enable all effects \
even if no interactive terminal was detected.",
),
)
2018-01-18 18:13:12 +01:00
.arg(
Arg::with_name("ignore-failure")
.long("ignore-failure")
.short("i")
2018-01-18 18:17:23 +01:00
.help("Ignore non-zero exit codes."),
2018-01-18 18:13:12 +01:00
)
2018-01-18 18:17:23 +01:00
.help_message("Print this help message.")
.version_message("Show version information.")
2018-01-13 16:50:02 +01:00
.get_matches();
2018-01-16 21:26:14 +01:00
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
if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {
2018-01-14 21:07:00 +01:00
// we need at least two runs to compute a variance
options.min_runs = cmp::max(2, min_runs);
2018-01-13 21:01:30 +01:00
}
2018-01-18 18:13:12 +01:00
options.preparation_command = matches.value_of("prepare").map(String::from);
options.output_style = match matches.value_of("style") {
Some("full") => OutputStyleOption::Full,
Some("basic") => OutputStyleOption::Basic,
_ => if atty::is(Stream::Stdout) {
OutputStyleOption::Full
} else {
OutputStyleOption::Basic
},
};
2018-02-02 14:42:31 +01:00
if options.output_style == OutputStyleOption::Basic {
colored::control::set_override(false);
}
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-01-14 12:03:34 +01:00
let commands = matches.values_of("command").unwrap().collect();
let res = run(&commands, &options);
2018-01-14 10:48:47 +01:00
2018-01-18 20:16:36 +01:00
if let Err(e) = res {
error(e.description());
2018-01-13 16:50:02 +01:00
}
}