mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-20 19:31:30 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b10afcf8ae | ||
|
|
22491e2385 | ||
|
|
e6d31f5914 | ||
|
|
556788c160 | ||
|
|
e53726d3f7 | ||
|
|
4be435436e | ||
|
|
1011eef7f3 | ||
|
|
6b19af9415 | ||
|
|
6063b405ec |
No files matched your search
Generated
+1
-1
@@ -125,7 +125,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "hyperfine"
|
||||
version = "1.0.0"
|
||||
version = "1.1.0"
|
||||
dependencies = [
|
||||
"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)",
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
|
||||
name = "hyperfine"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/sharkdp/hyperfine"
|
||||
version = "1.0.0"
|
||||
version = "1.1.0"
|
||||
|
||||
[dependencies]
|
||||
colored = "1.6"
|
||||
|
||||
@@ -84,19 +84,7 @@ option that will create tables like this:
|
||||
|
||||
## Installation
|
||||
|
||||
Hyperfine can be installed via [cargo](https://doc.rust-lang.org/cargo/):
|
||||
```
|
||||
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
|
||||
### On Ubuntu
|
||||
|
||||
Download the appropriate `.deb` package from the [Release page](https://github.com/sharkdp/hyperfine/releases)
|
||||
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
|
||||
```
|
||||
|
||||
### 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
|
||||
|
||||
@@ -113,6 +108,17 @@ Hyperfine can be installed via xbps
|
||||
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
|
||||
|
||||
The name *hyperfine* was chosen in reference to the hyperfine levels of caesium 133 which play a crucial role in the
|
||||
|
||||
@@ -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
@@ -1,17 +1,18 @@
|
||||
use std::io;
|
||||
use std::process::Stdio;
|
||||
|
||||
use colored::*;
|
||||
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::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::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
|
||||
#[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
|
||||
pub fn time_shell_command(
|
||||
command: &Command,
|
||||
show_output: bool,
|
||||
failure_action: CmdFailureAction,
|
||||
shell_spawning_time: Option<TimingResult>,
|
||||
) -> io::Result<(TimingResult, bool)> {
|
||||
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_system = result.system_time;
|
||||
@@ -54,7 +61,8 @@ pub fn time_shell_command(
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"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
|
||||
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;
|
||||
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 {
|
||||
// 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 {
|
||||
Err(_) => {
|
||||
@@ -114,10 +130,14 @@ pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingR
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
let res = time_shell_command(
|
||||
&Command::new(preparation_command),
|
||||
show_output,
|
||||
CmdFailureAction::RaiseError,
|
||||
None,
|
||||
);
|
||||
@@ -164,7 +184,7 @@ pub fn run_benchmark(
|
||||
);
|
||||
|
||||
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.finish_and_clear();
|
||||
@@ -178,11 +198,15 @@ pub fn run_benchmark(
|
||||
);
|
||||
|
||||
// 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
|
||||
let (res, success) =
|
||||
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||
let (res, success) = time_shell_command(
|
||||
cmd,
|
||||
options.show_output,
|
||||
options.failure_action,
|
||||
Some(shell_spawning_time),
|
||||
)?;
|
||||
|
||||
// Determine number of benchmark runs
|
||||
let runs_in_min_time = (options.min_time_sec
|
||||
@@ -209,7 +233,7 @@ pub fn run_benchmark(
|
||||
|
||||
// Gather statistics
|
||||
for _ in 0..count_remaining {
|
||||
run_preparation_command(&options.preparation_command)?;
|
||||
run_preparation_command(&options.preparation_command, options.show_output)?;
|
||||
|
||||
let msg = {
|
||||
let mean = format_duration(mean(×_real), None);
|
||||
@@ -217,8 +241,12 @@ pub fn run_benchmark(
|
||||
};
|
||||
progress_bar.set_message(&msg);
|
||||
|
||||
let (res, success) =
|
||||
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||
let (res, success) = time_shell_command(
|
||||
cmd,
|
||||
options.show_output,
|
||||
options.failure_action,
|
||||
Some(shell_spawning_time),
|
||||
)?;
|
||||
|
||||
times_real.push(res.time_real);
|
||||
times_user.push(res.time_user);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::num;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::num;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParameterScanError {
|
||||
|
||||
@@ -6,8 +6,8 @@ use self::csv::CsvExporter;
|
||||
use self::json::JsonExporter;
|
||||
use self::markdown::MarkdownExporter;
|
||||
|
||||
use std::io::{Result, Write};
|
||||
use std::fs::File;
|
||||
use std::io::{Result, Write};
|
||||
|
||||
use hyperfine::types::BenchmarkResult;
|
||||
|
||||
|
||||
@@ -31,14 +31,16 @@ pub fn get_progress_bar(length: u64, msg: &str, option: &OutputStyleOption) -> P
|
||||
|
||||
/// A max function for f64's without NaNs
|
||||
pub fn max(vals: &[f64]) -> f64 {
|
||||
*vals.iter()
|
||||
*vals
|
||||
.iter()
|
||||
.max_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A min function for f64's without NaNs
|
||||
pub fn min(vals: &[f64]) -> f64 {
|
||||
*vals.iter()
|
||||
*vals
|
||||
.iter()
|
||||
.min_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
@@ -62,7 +64,7 @@ pub fn write_benchmark_comparison(results: &Vec<BenchmarkResult>) {
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
for item in longer_items {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
pub mod app;
|
||||
pub mod benchmark;
|
||||
pub mod error;
|
||||
pub mod export;
|
||||
pub mod format;
|
||||
pub mod internal;
|
||||
@@ -7,4 +9,3 @@ pub mod shell;
|
||||
pub mod timer;
|
||||
pub mod types;
|
||||
pub mod warnings;
|
||||
pub mod error;
|
||||
@@ -4,7 +4,6 @@
|
||||
/// - 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,
|
||||
/// Ph.D., Editor.
|
||||
|
||||
use statistical::median;
|
||||
|
||||
/// Minimum modified Z-score for a datapoint to be an outlier. Here, 1.4826 is a factor that
|
||||
|
||||
+18
-10
@@ -19,8 +19,8 @@ pub struct ExecuteResult {
|
||||
|
||||
/// Execute the given command and return a timing summary
|
||||
#[cfg(windows)]
|
||||
pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {
|
||||
let mut child = run_shell_command(command)?;
|
||||
pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result<ExecuteResult> {
|
||||
let mut child = run_shell_command(stdout, stderr, command)?;
|
||||
let cpu_timer = get_cpu_timer(&child);
|
||||
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
|
||||
#[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 status = run_shell_command(command)?;
|
||||
let status = run_shell_command(stdout, stderr, command)?;
|
||||
|
||||
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
|
||||
#[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")
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdout(stdout)
|
||||
.stderr(stderr)
|
||||
.status()
|
||||
}
|
||||
|
||||
/// Run a Windows shell command using cmd.exe
|
||||
#[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")
|
||||
.arg("/C")
|
||||
.arg(command)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdout(stdout)
|
||||
.stderr(stderr)
|
||||
.spawn()
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::process::Child;
|
||||
use std::time::Instant;
|
||||
|
||||
use hyperfine::types::Second;
|
||||
use hyperfine::timer::{TimerStart, TimerStop};
|
||||
use hyperfine::types::Second;
|
||||
|
||||
pub struct WallClockTimer {
|
||||
start: Instant,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/// This module contains common internal types.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Type alias for unit of time
|
||||
@@ -89,6 +88,9 @@ pub struct HyperfineOptions {
|
||||
|
||||
/// What color mode to use for output
|
||||
pub output_style: OutputStyleOption,
|
||||
|
||||
/// Forward benchmark's stdout to hyperfine's stdout
|
||||
pub show_output: bool,
|
||||
}
|
||||
|
||||
impl Default for HyperfineOptions {
|
||||
@@ -100,6 +102,7 @@ impl Default for HyperfineOptions {
|
||||
failure_action: CmdFailureAction::RaiseError,
|
||||
preparation_command: None,
|
||||
output_style: OutputStyleOption::Full,
|
||||
show_output: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::fmt;
|
||||
|
||||
use hyperfine::format::format_duration;
|
||||
use hyperfine::internal::MIN_EXECUTION_TIME;
|
||||
use hyperfine::types::Second;
|
||||
use hyperfine::format::format_duration;
|
||||
|
||||
/// A list of all possible warnings
|
||||
pub enum Warnings {
|
||||
|
||||
+59
-148
@@ -15,6 +15,7 @@ extern crate serde_derive;
|
||||
extern crate serde_json;
|
||||
extern crate statistical;
|
||||
|
||||
// Os-specific dependencies
|
||||
cfg_if! {
|
||||
if #[cfg(windows)] {
|
||||
extern crate winapi;
|
||||
@@ -28,22 +29,25 @@ cfg_if! {
|
||||
extern crate approx;
|
||||
|
||||
use std::cmp;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::ops::Range;
|
||||
|
||||
use atty::Stream;
|
||||
use clap::ArgMatches;
|
||||
use colored::*;
|
||||
use clap::{App, AppSettings, Arg};
|
||||
|
||||
mod hyperfine;
|
||||
|
||||
use hyperfine::internal::write_benchmark_comparison;
|
||||
use hyperfine::types::{BenchmarkResult, CmdFailureAction, Command, HyperfineOptions,
|
||||
OutputStyleOption};
|
||||
use hyperfine::app::get_arg_matches;
|
||||
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
|
||||
use hyperfine::export::{ExportManager, ExportType};
|
||||
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
|
||||
pub fn error(message: &str) -> ! {
|
||||
@@ -53,7 +57,7 @@ pub fn error(message: &str) -> ! {
|
||||
|
||||
/// Runs the benchmark for the given commands
|
||||
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![];
|
||||
|
||||
@@ -86,121 +90,31 @@ fn parse_parameter_scan_args<'a>(
|
||||
}
|
||||
|
||||
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 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();
|
||||
|
||||
options.warmup_count = matches
|
||||
@@ -215,28 +129,19 @@ fn main() {
|
||||
|
||||
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") {
|
||||
Some("full") => OutputStyleOption::Full,
|
||||
Some("basic") => OutputStyleOption::Basic,
|
||||
Some("nocolor") => OutputStyleOption::NoColor,
|
||||
_ => if atty::is(Stream::Stdout) {
|
||||
_ => if !options.show_output && atty::is(Stream::Stdout) {
|
||||
OutputStyleOption::Full
|
||||
} else {
|
||||
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.
|
||||
if cfg!(windows) && options.output_style == OutputStyleOption::Full {
|
||||
options.output_style = OutputStyleOption::NoColor;
|
||||
@@ -250,6 +155,27 @@ fn main() {
|
||||
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 commands = if let Some(args) = matches.values_of("parameter-scan") {
|
||||
@@ -269,20 +195,5 @@ fn main() {
|
||||
} else {
|
||||
command_strings.map(|c| Command::new(c)).collect()
|
||||
};
|
||||
|
||||
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()),
|
||||
}
|
||||
commands
|
||||
}
|
||||
Reference in new issue
Block a user