mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Use enum instead of bool
This commit is contained in:
1 parent
f44f24c8d9
commit
d42994bf64
3 files changed
+24
-12
No files matched your search
@@ -6,7 +6,7 @@ use ansi_term::Colour::{Cyan, Green, Purple, White, Yellow};
|
|||||||
use statistical::{mean, standard_deviation};
|
use statistical::{mean, standard_deviation};
|
||||||
|
|
||||||
use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,
|
use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,
|
||||||
MIN_EXECUTION_TIME};
|
MIN_EXECUTION_TIME, CmdFailureAction};
|
||||||
use hyperfine::format::{format_duration, format_duration_unit, Unit};
|
use hyperfine::format::{format_duration, format_duration_unit, Unit};
|
||||||
|
|
||||||
/// Results from timing a single shell command
|
/// Results from timing a single shell command
|
||||||
@@ -30,7 +30,7 @@ impl TimingResult {
|
|||||||
/// 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(
|
||||||
shell_cmd: &str,
|
shell_cmd: &str,
|
||||||
ignore_failure: bool,
|
failure_action: CmdFailureAction,
|
||||||
shell_spawning_time: Option<Second>,
|
shell_spawning_time: Option<Second>,
|
||||||
) -> io::Result<TimingResult> {
|
) -> io::Result<TimingResult> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
@@ -43,7 +43,7 @@ pub fn time_shell_command(
|
|||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.status()?;
|
.status()?;
|
||||||
|
|
||||||
if !ignore_failure && !status.success() {
|
if failure_action == CmdFailureAction::RaiseError && !status.success() {
|
||||||
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. \
|
||||||
@@ -80,7 +80,7 @@ pub fn mean_shell_spawning_time() -> io::Result<Second> {
|
|||||||
let mut times: Vec<Second> = vec![];
|
let mut times: Vec<Second> = vec![];
|
||||||
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("", false, None);
|
let res = time_shell_command("", CmdFailureAction::RaiseError, None);
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@@ -124,7 +124,7 @@ pub fn run_benchmark(
|
|||||||
let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");
|
let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");
|
||||||
|
|
||||||
for _ in 0..options.warmup_count {
|
for _ in 0..options.warmup_count {
|
||||||
let _ = time_shell_command(cmd, options.ignore_failure, None)?;
|
let _ = time_shell_command(cmd, options.failure_action, None)?;
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
}
|
}
|
||||||
bar.finish_and_clear();
|
bar.finish_and_clear();
|
||||||
@@ -136,13 +136,13 @@ pub fn run_benchmark(
|
|||||||
// Run init / cleanup command
|
// Run init / cleanup command
|
||||||
let run_preparation_command = || {
|
let run_preparation_command = || {
|
||||||
if let Some(ref preparation_command) = options.preparation_command {
|
if let Some(ref preparation_command) = options.preparation_command {
|
||||||
let _ = time_shell_command(preparation_command, options.ignore_failure, None);
|
let _ = time_shell_command(preparation_command, options.failure_action, None);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
run_preparation_command();
|
run_preparation_command();
|
||||||
|
|
||||||
// Initial timing run
|
// Initial timing run
|
||||||
let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;
|
let res = time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||||
|
|
||||||
// Determine number of benchmark runs
|
// Determine number of benchmark runs
|
||||||
let runs_in_min_time =
|
let runs_in_min_time =
|
||||||
@@ -173,7 +173,7 @@ pub fn run_benchmark(
|
|||||||
};
|
};
|
||||||
bar.set_message(&msg);
|
bar.set_message(&msg);
|
||||||
|
|
||||||
let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;
|
let res = time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||||
|
|
||||||
execution_times.push(res.execution_time);
|
execution_times.push(res.execution_time);
|
||||||
all_succeeded = all_succeeded && res.success;
|
all_succeeded = all_succeeded && res.success;
|
||||||
|
|||||||
@@ -8,6 +8,16 @@ pub type Second = f64;
|
|||||||
/// Threshold for warning about fast execution time
|
/// Threshold for warning about fast execution time
|
||||||
pub const MIN_EXECUTION_TIME: Second = 5e-3;
|
pub const MIN_EXECUTION_TIME: Second = 5e-3;
|
||||||
|
|
||||||
|
/// Action to take when an executed command fails.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum CmdFailureAction {
|
||||||
|
/// Exit with an error message
|
||||||
|
RaiseError,
|
||||||
|
|
||||||
|
/// Simply ignore the non-zero exit code
|
||||||
|
Ignore,
|
||||||
|
}
|
||||||
|
|
||||||
/// A set of options for hyperfine
|
/// A set of options for hyperfine
|
||||||
pub struct HyperfineOptions {
|
pub struct HyperfineOptions {
|
||||||
/// Number of warmup runs
|
/// Number of warmup runs
|
||||||
@@ -20,7 +30,7 @@ pub struct HyperfineOptions {
|
|||||||
pub min_time_sec: Second,
|
pub min_time_sec: Second,
|
||||||
|
|
||||||
/// Whether or not to ignore non-zero exit codes
|
/// Whether or not to ignore non-zero exit codes
|
||||||
pub ignore_failure: bool,
|
pub failure_action: CmdFailureAction,
|
||||||
|
|
||||||
/// Command to run before each timing run
|
/// Command to run before each timing run
|
||||||
pub preparation_command: Option<String>,
|
pub preparation_command: Option<String>,
|
||||||
@@ -32,7 +42,7 @@ impl Default for HyperfineOptions {
|
|||||||
warmup_count: 0,
|
warmup_count: 0,
|
||||||
min_runs: 10,
|
min_runs: 10,
|
||||||
min_time_sec: 3.0,
|
min_time_sec: 3.0,
|
||||||
ignore_failure: false,
|
failure_action: CmdFailureAction::RaiseError,
|
||||||
preparation_command: None,
|
preparation_command: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-2
@@ -13,7 +13,7 @@ use clap::{App, AppSettings, Arg};
|
|||||||
|
|
||||||
mod hyperfine;
|
mod hyperfine;
|
||||||
|
|
||||||
use hyperfine::internal::HyperfineOptions;
|
use hyperfine::internal::{HyperfineOptions, CmdFailureAction};
|
||||||
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
|
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
|
||||||
|
|
||||||
/// Print error message to stderr and terminate
|
/// Print error message to stderr and terminate
|
||||||
@@ -109,7 +109,9 @@ fn main() {
|
|||||||
|
|
||||||
options.preparation_command = matches.value_of("prepare").map(String::from);
|
options.preparation_command = matches.value_of("prepare").map(String::from);
|
||||||
|
|
||||||
options.ignore_failure = matches.is_present("ignore-failure");
|
if matches.is_present("ignore-failure") {
|
||||||
|
options.failure_action = CmdFailureAction::Ignore;
|
||||||
|
}
|
||||||
|
|
||||||
let commands = matches.values_of("command").unwrap().collect();
|
let commands = matches.values_of("command").unwrap().collect();
|
||||||
let res = run(&commands, &options);
|
let res = run(&commands, &options);
|
||||||
|
|||||||
Reference in new issue
Block a user