Use enum instead of bool
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent f44f24c · Browse files · 3 files·+24 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−12 removed
- other:+24 added−12 removed
Provenance
- Commit
d42994b- Tree
7ef67f2- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
f44f24c
3 files changed+24 added−12 removed
modified:src/hyperfine/benchmark.rs+8 added-8 removed
4 unmodified lines5 use ansi_term::Colour::{Cyan, Green, Purple, White, Yellow};6 use statistical::{mean, standard_deviation};7 8 use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9- MIN_EXECUTION_TIME};9+ MIN_EXECUTION_TIME, CmdFailureAction};10 use hyperfine::format::{format_duration, format_duration_unit, Unit};11 12 /// Results from timing a single shell command13 pub struct TimingResult {15 unmodified lines29 30 /// Run the given shell command and measure the execution time31 pub fn time_shell_command(32 shell_cmd: &str,33- ignore_failure: bool,33+ failure_action: CmdFailureAction,34 shell_spawning_time: Option<Second>,35 ) -> io::Result<TimingResult> {36 let start = Instant::now();37 4 unmodified lines42 .stdout(Stdio::null())43 .stderr(Stdio::null())44 .status()?;45 46- if !ignore_failure && !status.success() {46+ if failure_action == CmdFailureAction::RaiseError && !status.success() {47 return Err(io::Error::new(48 io::ErrorKind::Other,49 "Command terminated with non-zero exit code. \50 Use the '-i'/'--ignore-failure' option if you want to ignore this.",28 unmodified lines79 80 let mut times: Vec<Second> = vec![];81 for _ in 0..COUNT {82 // Just run the shell without any command83- let res = time_shell_command("", false, None);83+ let res = time_shell_command("", CmdFailureAction::RaiseError, None);84 85 match res {86 Err(_) => {87 return Err(io::Error::new(35 unmodified lines123 if options.warmup_count > 0 {124 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");125 126 for _ in 0..options.warmup_count {127- let _ = time_shell_command(cmd, options.ignore_failure, None)?;127+ let _ = time_shell_command(cmd, options.failure_action, None)?;128 bar.inc(1);129 }130 bar.finish_and_clear();131 }3 unmodified lines135 136 // Run init / cleanup command137 let run_preparation_command = || {138 if let Some(ref preparation_command) = options.preparation_command {139- let _ = time_shell_command(preparation_command, options.ignore_failure, None);139+ let _ = time_shell_command(preparation_command, options.failure_action, None);140 }141 };142 run_preparation_command();143 144 // Initial timing run145- let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;145+ let res = time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;146 147 // Determine number of benchmark runs148 let runs_in_min_time =149 (options.min_time_sec / (res.execution_time + shell_spawning_time)) as u64;22 unmodified lines172 format!("Current estimate: {}", Green.paint(mean))173 };174 bar.set_message(&msg);175 176- let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;176+ let res = time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;177 178 execution_times.push(res.execution_time);179 all_succeeded = all_succeeded && res.success;180 57 unmodified linesmodified:src/hyperfine/internal.rs+12 added-2 removed
6 unmodified lines7 8 /// Threshold for warning about fast execution time9 pub const MIN_EXECUTION_TIME: Second = 5e-3;10 11+/// Action to take when an executed command fails.12+#[derive(Debug, Clone, Copy, PartialEq)]13+pub enum CmdFailureAction {14+ /// Exit with an error message15+ RaiseError,16+17+ /// Simply ignore the non-zero exit code18+ Ignore,19+}20+21 /// A set of options for hyperfine22 pub struct HyperfineOptions {23 /// Number of warmup runs24 pub warmup_count: u64,4 unmodified lines29 /// Minimum benchmarking time30 pub min_time_sec: Second,31 32 /// Whether or not to ignore non-zero exit codes23- pub ignore_failure: bool,33+ pub failure_action: CmdFailureAction,34 35 /// Command to run before each timing run36 pub preparation_command: Option<String>,37 }3 unmodified lines41 HyperfineOptions {42 warmup_count: 0,43 min_runs: 10,44 min_time_sec: 3.0,35- ignore_failure: false,45+ failure_action: CmdFailureAction::RaiseError,46 preparation_command: None,47 }48 }49 }49 unmodified linesmodified:src/main.rs+4 added-2 removed
11 unmodified lines12 use clap::{App, AppSettings, Arg};13 14 mod hyperfine;15 16-use hyperfine::internal::HyperfineOptions;16+use hyperfine::internal::{HyperfineOptions, CmdFailureAction};17 use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};18 19 /// Print error message to stderr and terminate20 pub fn error(message: &str) -> ! {87 unmodified lines108 }109 110 options.preparation_command = matches.value_of("prepare").map(String::from);111 112- options.ignore_failure = matches.is_present("ignore-failure");112+ if matches.is_present("ignore-failure") {113+ options.failure_action = CmdFailureAction::Ignore;114+ }115 116 let commands = matches.values_of("command").unwrap().collect();117 let res = run(&commands, &options);118 5 unmodified lines