Skip to content
Myr

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

3 files+24 added−12 removed

modified:src/hyperfine/benchmark.rs+8 added-8 removed
4 unmodified lines
5use ansi_term::Colour::{Cyan, Green, Purple, White, Yellow};6use statistical::{mean, standard_deviation};78use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9 MIN_EXECUTION_TIME};9 MIN_EXECUTION_TIME, CmdFailureAction};10use hyperfine::format::{format_duration, format_duration_unit, Unit};1112/// Results from timing a single shell command13pub struct TimingResult {
15 unmodified lines
2930/// Run the given shell command and measure the execution time31pub 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 lines
42 .stdout(Stdio::null())43 .stderr(Stdio::null())44 .status()?;4546 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 lines
7980 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);8485 match res {86 Err(_) => {87 return Err(io::Error::new(
35 unmodified lines
123 if options.warmup_count > 0 {124 let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");125126 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 lines
135136 // 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();143144 // 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))?;146147 // 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 lines
172 format!("Current estimate: {}", Green.paint(mean))173 };174 bar.set_message(&msg);175176 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))?;177178 execution_times.push(res.execution_time);179 all_succeeded = all_succeeded && res.success;180
57 unmodified lines
modified:src/hyperfine/internal.rs+12 added-2 removed
6 unmodified lines
78/// Threshold for warning about fast execution time9pub const MIN_EXECUTION_TIME: Second = 5e-3;1011/// Action to take when an executed command fails.12#[derive(Debug, Clone, Copy, PartialEq)]13pub enum CmdFailureAction {14 /// Exit with an error message15 RaiseError,1617 /// Simply ignore the non-zero exit code18 Ignore,19}2021/// A set of options for hyperfine22pub struct HyperfineOptions {23 /// Number of warmup runs24 pub warmup_count: u64,
4 unmodified lines
29 /// Minimum benchmarking time30 pub min_time_sec: Second,3132 /// Whether or not to ignore non-zero exit codes23 pub ignore_failure: bool,33 pub failure_action: CmdFailureAction,3435 /// Command to run before each timing run36 pub preparation_command: Option<String>,37}
3 unmodified lines
41 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 lines
modified:src/main.rs+4 added-2 removed
11 unmodified lines
12use clap::{App, AppSettings, Arg};1314mod hyperfine;1516use hyperfine::internal::HyperfineOptions;16use hyperfine::internal::{HyperfineOptions, CmdFailureAction};17use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};1819/// Print error message to stderr and terminate20pub fn error(message: &str) -> ! {
87 unmodified lines
108 }109110 options.preparation_command = matches.value_of("prepare").map(String::from);111112 options.ignore_failure = matches.is_present("ignore-failure");112 if matches.is_present("ignore-failure") {113 options.failure_action = CmdFailureAction::Ignore;114 }115116 let commands = matches.values_of("command").unwrap().collect();117 let res = run(&commands, &options);118
5 unmodified lines