Compare commits

...
9 Commits
Author SHA1 Message Date
sharkdp b10afcf8ae Bump version 2018-06-11 20:44:03 +02:00
sharkdp 22491e2385 Run 'cargo fmt' 2018-05-22 22:57:51 +02:00
sharkdp e6d31f5914 Add comment about --show-output option 2018-05-22 22:57:16 +02:00
Christian Duerr 556788c160 Print stderr with --show-output flag
The `--print-stdout` flag has been changed to `--show-output` and now
both stderr and stdout are forwarded when this option is specified.
2018-05-22 22:54:08 +02:00
Christian Duerr e53726d3f7 Fix windows 2018-05-22 22:54:08 +02:00
Christian Duerr 4be435436e Allow forwarding benchmark's stdout to hyperfine
This change makes it possible to forward the stdout of the command the
user tries to benchmark to hyperfine's stdout using the `--print-stdout`
flag.

This should be irrelevant for most use-cases, however it makes it
possible to get some crude benchmarks of terminal emulators similar to
what's currently often done with `time cat file`.
2018-05-22 22:54:08 +02:00
sharkdp 1011eef7f3 Indentation 2018-05-14 18:43:17 +02:00
Steve Pentland 6b19af9415 All parsing of arguments now split out. 2018-04-24 21:20:05 +02:00
David Peter 6063b405ec Update install instructions 2018-03-24 22:06:03 +01:00
15 changed files with 294 additions and 202 deletions

No files matched your search

Generated
+1 -1
View File
@@ -125,7 +125,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "hyperfine" name = "hyperfine"
version = "1.0.0" version = "1.1.0"
dependencies = [ dependencies = [
"approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+1 -1
View File
@@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
name = "hyperfine" name = "hyperfine"
readme = "README.md" readme = "README.md"
repository = "https://github.com/sharkdp/hyperfine" repository = "https://github.com/sharkdp/hyperfine"
version = "1.0.0" version = "1.1.0"
[dependencies] [dependencies]
colored = "1.6" colored = "1.6"
+20 -14
View File
@@ -84,19 +84,7 @@ option that will create tables like this:
## Installation ## Installation
Hyperfine can be installed via [cargo](https://doc.rust-lang.org/cargo/): ### On Ubuntu
```
cargo install hyperfine
```
### Arch Linux
On Arch Linux, hyperfine can be installed [from the AUR](https://aur.archlinux.org/packages/hyperfine):
```
yaourt -S hyperfine
```
### Ubuntu
Download the appropriate `.deb` package from the [Release page](https://github.com/sharkdp/hyperfine/releases) Download the appropriate `.deb` package from the [Release page](https://github.com/sharkdp/hyperfine/releases)
and install it via `dpkg`: and install it via `dpkg`:
@@ -105,7 +93,14 @@ wget https://github.com/sharkdp/hyperfine/releases/download/v1.0.0/hyperfine_1.0
sudo dpkg -i hyperfine_1.0.0_amd64.deb sudo dpkg -i hyperfine_1.0.0_amd64.deb
``` ```
### Void Linux ### On Arch Linux
On Arch Linux, hyperfine can be installed [from the AUR](https://aur.archlinux.org/packages/hyperfine):
```
yaourt -S hyperfine
```
### On Void Linux
Hyperfine can be installed via xbps Hyperfine can be installed via xbps
@@ -113,6 +108,17 @@ Hyperfine can be installed via xbps
xbps-install -S hyperfine xbps-install -S hyperfine
``` ```
### With cargo (Linux, macOS, Windows)
Hyperfine can be installed via [cargo](https://doc.rust-lang.org/cargo/):
```
cargo install hyperfine
```
### From binaries (Linux, macOS)
Download the corresponding archive from the [Release page](https://github.com/sharkdp/hyperfine/releases).
## Origin of the name ## Origin of the name
The name *hyperfine* was chosen in reference to the hyperfine levels of caesium 133 which play a crucial role in the The name *hyperfine* was chosen in reference to the hyperfine levels of caesium 133 which play a crucial role in the
+134
View File
@@ -0,0 +1,134 @@
use atty;
use atty::Stream;
use clap::{App, AppSettings, Arg, ArgMatches};
use std::ffi::OsString;
pub fn get_arg_matches<T>(args: T) -> ArgMatches<'static>
where
T: Iterator<Item = OsString>,
{
let app = build_app();
app.get_matches_from(args)
}
/// Build the clap app for parsing command line arguments
fn build_app() -> App<'static, 'static> {
let clap_color_setting = if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever
};
App::new("hyperfine")
.version(crate_version!())
.setting(clap_color_setting)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::UnifiedHelpMessage)
.setting(AppSettings::NextLineHelp)
.setting(AppSettings::HidePossibleValuesInHelp)
.max_term_width(90)
.about("A command-line benchmarking tool.")
.arg(
Arg::with_name("command")
.help("Command to benchmark")
.required(true)
.multiple(true)
.empty_values(false),
)
.arg(
Arg::with_name("warmup")
.long("warmup")
.short("w")
.takes_value(true)
.value_name("NUM")
.help(
"Perform NUM warmup runs before the actual benchmark. This can be used \
to fill (disk) caches for I/O-heavy programs.",
),
)
.arg(
Arg::with_name("min-runs")
.long("min-runs")
.short("m")
.takes_value(true)
.value_name("NUM")
.help("Perform at least NUM runs for each command (default: 10)."),
)
.arg(
Arg::with_name("prepare")
.long("prepare")
.short("p")
.takes_value(true)
.value_name("CMD")
.help(
"Execute CMD before each timing run. This is useful for \
clearing disk caches, for example.",
),
)
.arg(
Arg::with_name("parameter-scan")
.long("parameter-scan")
.short("P")
.help(
"Perform benchmark runs for each value in the range MIN..MAX. Replaces the \
string '{VAR}' in each command by the current parameter value.",
)
.takes_value(true)
.allow_hyphen_values(true)
.value_names(&["VAR", "MIN", "MAX"]),
)
.arg(
Arg::with_name("style")
.long("style")
.short("s")
.takes_value(true)
.value_name("TYPE")
.possible_values(&["auto", "basic", "full", "nocolor"])
.help(
"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. Set this to 'nocolor' to \
keep the interactive output without any colors.",
),
)
.arg(
Arg::with_name("ignore-failure")
.long("ignore-failure")
.short("i")
.help("Ignore non-zero exit codes."),
)
.arg(
Arg::with_name("export-csv")
.long("export-csv")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as CSV to the given FILE."),
)
.arg(
Arg::with_name("export-json")
.long("export-json")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as JSON to the given FILE."),
)
.arg(
Arg::with_name("export-markdown")
.long("export-markdown")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as a Markdown table to the given FILE."),
)
.arg(
Arg::with_name("show-output")
.long("show-output")
.conflicts_with("style")
.help(
"Print the stdout and stderr of the benchmark instead of suppressing it. \
This will increase the time it takes for benchmarks to run, \
so it should only be used for debugging purposes or \
when trying to benchmark output speed.",
),
)
.help_message("Print this help message.")
.version_message("Show version information.")
}
+46 -18
View File
@@ -1,17 +1,18 @@
use std::io; use std::io;
use std::process::Stdio;
use colored::*; use colored::*;
use statistical::{mean, standard_deviation}; use statistical::{mean, standard_deviation};
use hyperfine::format::{format_duration, format_duration_unit};
use hyperfine::internal::{get_progress_bar, max, min, MIN_EXECUTION_TIME}; use hyperfine::internal::{get_progress_bar, max, min, MIN_EXECUTION_TIME};
use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
use hyperfine::shell::execute_and_time;
use hyperfine::timer::wallclocktimer::WallClockTimer;
use hyperfine::timer::{TimerStart, TimerStop};
use hyperfine::types::{BenchmarkResult, Command};
use hyperfine::types::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Second}; use hyperfine::types::{CmdFailureAction, HyperfineOptions, OutputStyleOption, Second};
use hyperfine::warnings::Warnings; use hyperfine::warnings::Warnings;
use hyperfine::format::{format_duration, format_duration_unit};
use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
use hyperfine::timer::{TimerStart, TimerStop};
use hyperfine::timer::wallclocktimer::WallClockTimer;
use hyperfine::shell::execute_and_time;
use hyperfine::types::{BenchmarkResult, Command};
/// Results from timing a single shell command /// Results from timing a single shell command
#[derive(Debug, Default, Copy, Clone)] #[derive(Debug, Default, Copy, Clone)]
@@ -38,12 +39,18 @@ fn subtract_shell_spawning_time(time: Second, shell_spawning_time: Second) -> Se
/// Run the given shell command and measure the execution time /// Run the given shell command and measure the execution time
pub fn time_shell_command( pub fn time_shell_command(
command: &Command, command: &Command,
show_output: bool,
failure_action: CmdFailureAction, failure_action: CmdFailureAction,
shell_spawning_time: Option<TimingResult>, shell_spawning_time: Option<TimingResult>,
) -> io::Result<(TimingResult, bool)> { ) -> io::Result<(TimingResult, bool)> {
let wallclock_timer = WallClockTimer::start(); let wallclock_timer = WallClockTimer::start();
let result = execute_and_time(&command.get_shell_command())?; let (stdout, stderr) = if show_output {
(Stdio::inherit(), Stdio::inherit())
} else {
(Stdio::null(), Stdio::null())
};
let result = execute_and_time(stdout, stderr, &command.get_shell_command())?;
let mut time_user = result.user_time; let mut time_user = result.user_time;
let mut time_system = result.system_time; let mut time_system = result.system_time;
@@ -54,7 +61,8 @@ pub fn time_shell_command(
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::Other, io::ErrorKind::Other,
"Command terminated with non-zero exit code. \ "Command terminated with non-zero exit code. \
Use the '-i'/'--ignore-failure' option if you want to ignore this.", Use the '-i'/'--ignore-failure' option if you want to ignore this. \
Alternatively, use the '--show-output' option to debug what went wrong.",
)); ));
} }
@@ -76,7 +84,10 @@ pub fn time_shell_command(
} }
/// Measure the average shell spawning time /// Measure the average shell spawning time
pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingResult> { pub fn mean_shell_spawning_time(
style: &OutputStyleOption,
show_output: bool,
) -> io::Result<TimingResult> {
const COUNT: u64 = 200; const COUNT: u64 = 200;
let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style); let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style);
@@ -86,7 +97,12 @@ pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingR
for _ in 0..COUNT { for _ in 0..COUNT {
// Just run the shell without any command // Just run the shell without any command
let res = time_shell_command(&Command::new(""), CmdFailureAction::RaiseError, None); let res = time_shell_command(
&Command::new(""),
show_output,
CmdFailureAction::RaiseError,
None,
);
match res { match res {
Err(_) => { Err(_) => {
@@ -114,10 +130,14 @@ pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingR
} }
/// Run the command specified by `--prepare`. /// Run the command specified by `--prepare`.
fn run_preparation_command(command: &Option<String>) -> io::Result<TimingResult> { fn run_preparation_command(
command: &Option<String>,
show_output: bool,
) -> io::Result<TimingResult> {
if let &Some(ref preparation_command) = command { if let &Some(ref preparation_command) = command {
let res = time_shell_command( let res = time_shell_command(
&Command::new(preparation_command), &Command::new(preparation_command),
show_output,
CmdFailureAction::RaiseError, CmdFailureAction::RaiseError,
None, None,
); );
@@ -164,7 +184,7 @@ pub fn run_benchmark(
); );
for _ in 0..options.warmup_count { for _ in 0..options.warmup_count {
let _ = time_shell_command(cmd, options.failure_action, None)?; let _ = time_shell_command(cmd, options.show_output, options.failure_action, None)?;
progress_bar.inc(1); progress_bar.inc(1);
} }
progress_bar.finish_and_clear(); progress_bar.finish_and_clear();
@@ -178,11 +198,15 @@ pub fn run_benchmark(
); );
// Run init / cleanup command // Run init / cleanup command
let prepare_res = run_preparation_command(&options.preparation_command)?; let prepare_res = run_preparation_command(&options.preparation_command, options.show_output)?;
// Initial timing run // Initial timing run
let (res, success) = let (res, success) = time_shell_command(
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?; cmd,
options.show_output,
options.failure_action,
Some(shell_spawning_time),
)?;
// Determine number of benchmark runs // Determine number of benchmark runs
let runs_in_min_time = (options.min_time_sec let runs_in_min_time = (options.min_time_sec
@@ -209,7 +233,7 @@ pub fn run_benchmark(
// Gather statistics // Gather statistics
for _ in 0..count_remaining { for _ in 0..count_remaining {
run_preparation_command(&options.preparation_command)?; run_preparation_command(&options.preparation_command, options.show_output)?;
let msg = { let msg = {
let mean = format_duration(mean(&times_real), None); let mean = format_duration(mean(&times_real), None);
@@ -217,8 +241,12 @@ pub fn run_benchmark(
}; };
progress_bar.set_message(&msg); progress_bar.set_message(&msg);
let (res, success) = let (res, success) = time_shell_command(
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?; cmd,
options.show_output,
options.failure_action,
Some(shell_spawning_time),
)?;
times_real.push(res.time_real); times_real.push(res.time_real);
times_user.push(res.time_user); times_user.push(res.time_user);
+1 -1
View File
@@ -1,6 +1,6 @@
use std::num;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::num;
#[derive(Debug)] #[derive(Debug)]
pub enum ParameterScanError { pub enum ParameterScanError {
+1 -1
View File
@@ -6,8 +6,8 @@ use self::csv::CsvExporter;
use self::json::JsonExporter; use self::json::JsonExporter;
use self::markdown::MarkdownExporter; use self::markdown::MarkdownExporter;
use std::io::{Result, Write};
use std::fs::File; use std::fs::File;
use std::io::{Result, Write};
use hyperfine::types::BenchmarkResult; use hyperfine::types::BenchmarkResult;
+5 -3
View File
@@ -31,14 +31,16 @@ pub fn get_progress_bar(length: u64, msg: &str, option: &OutputStyleOption) -> P
/// A max function for f64's without NaNs /// A max function for f64's without NaNs
pub fn max(vals: &[f64]) -> f64 { pub fn max(vals: &[f64]) -> f64 {
*vals.iter() *vals
.iter()
.max_by(|a, b| a.partial_cmp(b).unwrap()) .max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap() .unwrap()
} }
/// A min function for f64's without NaNs /// A min function for f64's without NaNs
pub fn min(vals: &[f64]) -> f64 { pub fn min(vals: &[f64]) -> f64 {
*vals.iter() *vals
.iter()
.min_by(|a, b| a.partial_cmp(b).unwrap()) .min_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap() .unwrap()
} }
@@ -62,7 +64,7 @@ pub fn write_benchmark_comparison(results: &Vec<BenchmarkResult>) {
} }
println!("{}\n", "Summary".bold()); println!("{}\n", "Summary".bold());
println!("'{}' ran", fastest_item.command.cyan()); println!(" '{}' ran", fastest_item.command.cyan());
longer_items.sort_by(|l, r| l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)); longer_items.sort_by(|l, r| l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal));
for item in longer_items { for item in longer_items {
+2 -1
View File
@@ -1,4 +1,6 @@
pub mod app;
pub mod benchmark; pub mod benchmark;
pub mod error;
pub mod export; pub mod export;
pub mod format; pub mod format;
pub mod internal; pub mod internal;
@@ -7,4 +9,3 @@ pub mod shell;
pub mod timer; pub mod timer;
pub mod types; pub mod types;
pub mod warnings; pub mod warnings;
pub mod error;
-1
View File
@@ -4,7 +4,6 @@
/// - Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and Handle Outliers", /// - Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and Handle Outliers",
/// The ASQC Basic References in Quality Control: Statistical Techniques, Edward F. Mykytka, /// The ASQC Basic References in Quality Control: Statistical Techniques, Edward F. Mykytka,
/// Ph.D., Editor. /// Ph.D., Editor.
use statistical::median; use statistical::median;
/// Minimum modified Z-score for a datapoint to be an outlier. Here, 1.4826 is a factor that /// Minimum modified Z-score for a datapoint to be an outlier. Here, 1.4826 is a factor that
+18 -10
View File
@@ -19,8 +19,8 @@ pub struct ExecuteResult {
/// Execute the given command and return a timing summary /// Execute the given command and return a timing summary
#[cfg(windows)] #[cfg(windows)]
pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> { pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {
let mut child = run_shell_command(command)?; let mut child = run_shell_command(stdout, stderr, command)?;
let cpu_timer = get_cpu_timer(&child); let cpu_timer = get_cpu_timer(&child);
let status = child.wait()?; let status = child.wait()?;
@@ -34,10 +34,10 @@ pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {
/// Execute the given command and return a timing summary /// Execute the given command and return a timing summary
#[cfg(not(windows))] #[cfg(not(windows))]
pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> { pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {
let cpu_timer = get_cpu_timer(); let cpu_timer = get_cpu_timer();
let status = run_shell_command(command)?; let status = run_shell_command(stdout, stderr, command)?;
let (user_time, system_time) = cpu_timer.stop(); let (user_time, system_time) = cpu_timer.stop();
@@ -50,24 +50,32 @@ pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {
/// Run a standard shell command /// Run a standard shell command
#[cfg(not(windows))] #[cfg(not(windows))]
fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> { fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
) -> io::Result<std::process::ExitStatus> {
Command::new("sh") Command::new("sh")
.arg("-c") .arg("-c")
.arg(command) .arg(command)
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::null()) .stdout(stdout)
.stderr(Stdio::null()) .stderr(stderr)
.status() .status()
} }
/// Run a Windows shell command using cmd.exe /// Run a Windows shell command using cmd.exe
#[cfg(windows)] #[cfg(windows)]
fn run_shell_command(command: &str) -> io::Result<std::process::Child> { fn run_shell_command(
stdout: Stdio,
stderr: Stdio,
command: &str,
) -> io::Result<std::process::Child> {
Command::new("cmd") Command::new("cmd")
.arg("/C") .arg("/C")
.arg(command) .arg(command)
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::null()) .stdout(stdout)
.stderr(Stdio::null()) .stderr(stderr)
.spawn() .spawn()
} }
+1 -1
View File
@@ -1,8 +1,8 @@
use std::process::Child; use std::process::Child;
use std::time::Instant; use std::time::Instant;
use hyperfine::types::Second;
use hyperfine::timer::{TimerStart, TimerStop}; use hyperfine::timer::{TimerStart, TimerStop};
use hyperfine::types::Second;
pub struct WallClockTimer { pub struct WallClockTimer {
start: Instant, start: Instant,
+4 -1
View File
@@ -1,5 +1,4 @@
/// This module contains common internal types. /// This module contains common internal types.
use std::fmt; use std::fmt;
/// Type alias for unit of time /// Type alias for unit of time
@@ -89,6 +88,9 @@ pub struct HyperfineOptions {
/// What color mode to use for output /// What color mode to use for output
pub output_style: OutputStyleOption, pub output_style: OutputStyleOption,
/// Forward benchmark's stdout to hyperfine's stdout
pub show_output: bool,
} }
impl Default for HyperfineOptions { impl Default for HyperfineOptions {
@@ -100,6 +102,7 @@ impl Default for HyperfineOptions {
failure_action: CmdFailureAction::RaiseError, failure_action: CmdFailureAction::RaiseError,
preparation_command: None, preparation_command: None,
output_style: OutputStyleOption::Full, output_style: OutputStyleOption::Full,
show_output: false,
} }
} }
} }
+1 -1
View File
@@ -1,8 +1,8 @@
use std::fmt; use std::fmt;
use hyperfine::format::format_duration;
use hyperfine::internal::MIN_EXECUTION_TIME; use hyperfine::internal::MIN_EXECUTION_TIME;
use hyperfine::types::Second; use hyperfine::types::Second;
use hyperfine::format::format_duration;
/// A list of all possible warnings /// A list of all possible warnings
pub enum Warnings { pub enum Warnings {
+59 -148
View File
@@ -15,6 +15,7 @@ extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
extern crate statistical; extern crate statistical;
// Os-specific dependencies
cfg_if! { cfg_if! {
if #[cfg(windows)] { if #[cfg(windows)] {
extern crate winapi; extern crate winapi;
@@ -28,22 +29,25 @@ cfg_if! {
extern crate approx; extern crate approx;
use std::cmp; use std::cmp;
use std::env;
use std::error::Error; use std::error::Error;
use std::io; use std::io;
use std::ops::Range; use std::ops::Range;
use atty::Stream; use atty::Stream;
use clap::ArgMatches;
use colored::*; use colored::*;
use clap::{App, AppSettings, Arg};
mod hyperfine; mod hyperfine;
use hyperfine::internal::write_benchmark_comparison; use hyperfine::app::get_arg_matches;
use hyperfine::types::{BenchmarkResult, CmdFailureAction, Command, HyperfineOptions,
OutputStyleOption};
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark}; use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
use hyperfine::export::{ExportManager, ExportType};
use hyperfine::error::ParameterScanError; use hyperfine::error::ParameterScanError;
use hyperfine::export::{ExportManager, ExportType};
use hyperfine::internal::write_benchmark_comparison;
use hyperfine::types::{
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
};
/// Print error message to stderr and terminate /// Print error message to stderr and terminate
pub fn error(message: &str) -> ! { pub fn error(message: &str) -> ! {
@@ -53,7 +57,7 @@ pub fn error(message: &str) -> ! {
/// Runs the benchmark for the given commands /// Runs the benchmark for the given commands
fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> { fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {
let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?; let shell_spawning_time = mean_shell_spawning_time(&options.output_style, options.show_output)?;
let mut timing_results = vec![]; let mut timing_results = vec![];
@@ -86,121 +90,31 @@ fn parse_parameter_scan_args<'a>(
} }
fn main() { fn main() {
// Process command line options 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 = run(&commands, &options);
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!(
"The following error occured while exporting: {}",
e.description()
));
}
}
Err(e) => error(e.description()),
}
}
/// Build the HyperfineOptions that correspond to the given ArgMatches
fn build_hyperfine_options(matches: &ArgMatches) -> HyperfineOptions {
let mut options = HyperfineOptions::default(); let mut options = HyperfineOptions::default();
let clap_color_setting = if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever
};
let matches = App::new("hyperfine")
.version(crate_version!())
.setting(clap_color_setting)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::UnifiedHelpMessage)
.setting(AppSettings::NextLineHelp)
.setting(AppSettings::HidePossibleValuesInHelp)
.max_term_width(90)
.about("A command-line benchmarking tool.")
.arg(
Arg::with_name("command")
.help("Command to benchmark")
.required(true)
.multiple(true)
.empty_values(false),
)
.arg(
Arg::with_name("warmup")
.long("warmup")
.short("w")
.takes_value(true)
.value_name("NUM")
.help(
"Perform NUM warmup runs before the actual benchmark. This can be used \
to fill (disk) caches for I/O-heavy programs.",
),
)
.arg(
Arg::with_name("min-runs")
.long("min-runs")
.short("m")
.takes_value(true)
.value_name("NUM")
.help(&format!(
"Perform at least NUM runs for each command (default: {}).",
options.min_runs
)),
)
.arg(
Arg::with_name("prepare")
.long("prepare")
.short("p")
.takes_value(true)
.value_name("CMD")
.help(
"Execute CMD before each timing run. This is useful for \
clearing disk caches, for example.",
),
)
.arg(
Arg::with_name("parameter-scan")
.long("parameter-scan")
.short("P")
.help(
"Perform benchmark runs for each value in the range MIN..MAX. Replaces the \
string '{VAR}' in each command by the current parameter value.",
)
.takes_value(true)
.allow_hyphen_values(true)
.value_names(&["VAR", "MIN", "MAX"]),
)
.arg(
Arg::with_name("style")
.long("style")
.short("s")
.takes_value(true)
.value_name("TYPE")
.possible_values(&["auto", "basic", "full", "nocolor"])
.help(
"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. Set this to 'nocolor' to \
keep the interactive output without any colors.",
),
)
.arg(
Arg::with_name("ignore-failure")
.long("ignore-failure")
.short("i")
.help("Ignore non-zero exit codes."),
)
.arg(
Arg::with_name("export-csv")
.long("export-csv")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as CSV to the given FILE."),
)
.arg(
Arg::with_name("export-json")
.long("export-json")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as JSON to the given FILE."),
)
.arg(
Arg::with_name("export-markdown")
.long("export-markdown")
.takes_value(true)
.value_name("FILE")
.help("Export the timing results as a Markdown table to the given FILE."),
)
.help_message("Print this help message.")
.version_message("Show version information.")
.get_matches();
let str_to_u64 = |n| u64::from_str_radix(n, 10).ok(); let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();
options.warmup_count = matches options.warmup_count = matches
@@ -215,28 +129,19 @@ fn main() {
options.preparation_command = matches.value_of("prepare").map(String::from); options.preparation_command = matches.value_of("prepare").map(String::from);
options.show_output = matches.is_present("show-output");
options.output_style = match matches.value_of("style") { options.output_style = match matches.value_of("style") {
Some("full") => OutputStyleOption::Full, Some("full") => OutputStyleOption::Full,
Some("basic") => OutputStyleOption::Basic, Some("basic") => OutputStyleOption::Basic,
Some("nocolor") => OutputStyleOption::NoColor, Some("nocolor") => OutputStyleOption::NoColor,
_ => if atty::is(Stream::Stdout) { _ => if !options.show_output && atty::is(Stream::Stdout) {
OutputStyleOption::Full OutputStyleOption::Full
} else { } else {
OutputStyleOption::Basic OutputStyleOption::Basic
}, },
}; };
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);
}
// We default Windows to NoColor if full had been specified. // We default Windows to NoColor if full had been specified.
if cfg!(windows) && options.output_style == OutputStyleOption::Full { if cfg!(windows) && options.output_style == OutputStyleOption::Full {
options.output_style = OutputStyleOption::NoColor; options.output_style = OutputStyleOption::NoColor;
@@ -250,6 +155,27 @@ fn main() {
options.failure_action = CmdFailureAction::Ignore; options.failure_action = CmdFailureAction::Ignore;
} }
options
}
/// 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(); let command_strings = matches.values_of("command").unwrap();
let commands = if let Some(args) = matches.values_of("parameter-scan") { let commands = if let Some(args) = matches.values_of("parameter-scan") {
@@ -269,20 +195,5 @@ fn main() {
} else { } else {
command_strings.map(|c| Command::new(c)).collect() command_strings.map(|c| Command::new(c)).collect()
}; };
commands
let res = run(&commands, &options);
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!(
"The following error occured while exporting: {}",
e.description()
));
}
}
Err(e) => error(e.description()),
}
} }