Skip to content
Myr

Turn show_output into enum

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent f9334e1 · Browse files · 3 files+81 added−49 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+81 added−49 removed

  • other:+81 added−49 removed

3 files+81 added−49 removed

modified:src/benchmark/mod.rs+38 added-28 removed
8 unmodified lines
9use statistical::{mean, median, standard_deviation};1011use crate::benchmark::result::BenchmarkResult;12use crate::command::Command;13use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell};13use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};14use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};15use crate::output::format::{format_duration, format_duration_unit};16use crate::output::progress_bar::get_progress_bar;17use crate::output::warnings::Warnings;
33 unmodified lines
51/// Run the given shell command and measure the execution time52pub fn time_shell_command(53 shell: &Shell,54 command: &Command<'_>,55 show_output: bool,55 command_output_policy: CommandOutputPolicy,56 failure_action: CmdFailureAction,57 shell_spawning_time: Option<TimingResult>,58) -> Result<(TimingResult, ExitStatus)> {59 let (stdout, stderr) = if show_output {60 (Stdio::inherit(), Stdio::inherit())61 } else {62 (Stdio::null(), Stdio::null())59 let (stdout, stderr) = match command_output_policy {60 CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),61 CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),62 };6364 let wallclock_timer = WallClockTimer::start();65 let result = execute_and_time(stdout, stderr, &command.get_shell_command(), shell)?;
33 unmodified lines
99/// Measure the average shell spawning time100pub fn mean_shell_spawning_time(101 shell: &Shell,102 style: OutputStyleOption,104 show_output: bool,103 command_output_policy: CommandOutputPolicy,104) -> Result<TimingResult> {105 const COUNT: u64 = 50;106 let progress_bar = if style != OutputStyleOption::Disabled {107 Some(get_progress_bar(
13 unmodified lines
121 // Just run the shell without any command122 let res = time_shell_command(123 shell,124 &Command::new(None, ""),126 show_output,125 command_output_policy,126 CmdFailureAction::RaiseError,127 None,128 );129
35 unmodified lines
165166fn run_intermediate_command(167 shell: &Shell,168 command: &Option<Command<'_>>,170 show_output: bool,169 command_output_policy: CommandOutputPolicy,170 error_output: &'static str,171) -> Result<TimingResult> {172 if let Some(ref cmd) = command {174 let res = time_shell_command(shell, cmd, show_output, CmdFailureAction::RaiseError, None);173 let res = time_shell_command(174 shell,175 cmd,176 command_output_policy,177 CmdFailureAction::RaiseError,178 None,179 );180 if res.is_err() {181 bail!(error_output);182 }183 return res.map(|r| r.0);
6 unmodified lines
190/// Run the command specified by `--setup`.191fn run_setup_command(192 shell: &Shell,193 command: &Option<Command<'_>>,189 show_output: bool,194 output_policy: CommandOutputPolicy,195) -> Result<TimingResult> {196 let error_output = "The setup command terminated with a non-zero exit code. \197 Append ' || true' to the command if you are sure that this can be ignored.";198194 run_intermediate_command(shell, command, show_output, error_output)199 run_intermediate_command(shell, command, output_policy, error_output)200}201202/// Run the command specified by `--prepare`.203fn run_preparation_command(204 shell: &Shell,205 command: &Option<Command<'_>>,201 show_output: bool,206 output_policy: CommandOutputPolicy,207) -> Result<TimingResult> {208 let error_output = "The preparation command terminated with a non-zero exit code. \209 Append ' || true' to the command if you are sure that this can be ignored.";210206 run_intermediate_command(shell, command, show_output, error_output)211 run_intermediate_command(shell, command, output_policy, error_output)212}213214/// Run the command specified by `--cleanup`.215fn run_cleanup_command(216 shell: &Shell,217 command: &Option<Command<'_>>,213 show_output: bool,218 output_policy: CommandOutputPolicy,219) -> Result<TimingResult> {220 let error_output = "The cleanup command terminated with a non-zero exit code. \221 Append ' || true' to the command if you are sure that this can be ignored.";222218 run_intermediate_command(shell, command, show_output, error_output)223 run_intermediate_command(shell, command, output_policy, error_output)224}225226#[cfg(unix)]227fn extract_exit_code(status: ExitStatus) -> Option<i32> {
52 unmodified lines
280 // Run setup command281 let setup_cmd = options.setup_command.as_ref().map(|setup_command| {282 Command::new_parametrized(None, setup_command, cmd.get_parameters().clone())283 });279 run_setup_command(&options.shell, &setup_cmd, options.show_output)?;284 run_setup_command(&options.shell, &setup_cmd, options.command_output_policy)?;285286 // Warmup phase287 if options.warmup_count > 0 {288 let progress_bar = if options.output_style != OutputStyleOption::Disabled {
6 unmodified lines
295 None296 };297298 for _ in 0..options.warmup_count {294 let _ = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;299 let _ = run_preparation_command(300 &options.shell,301 &prepare_cmd,302 options.command_output_policy,303 )?;304 let _ = time_shell_command(305 &options.shell,306 cmd,298 options.show_output,299 options.failure_action,307 options.command_output_policy,308 options.command_failure_action,309 None,310 )?;311 if let Some(bar) = progress_bar.as_ref() {312 bar.inc(1)
14 unmodified lines
327 } else {328 None329 };330322 let prepare_result = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;331 let prepare_result =332 run_preparation_command(&options.shell, &prepare_cmd, options.command_output_policy)?;333334 // Initial timing run335 let (res, status) = time_shell_command(336 &options.shell,337 cmd,328 options.show_output,329 options.failure_action,338 options.command_output_policy,339 options.command_failure_action,340 Some(shell_spawning_time),341 )?;342 let success = status.success();343344 // Determine number of benchmark runs335 let runs_in_min_time = (options.min_time_sec345 let runs_in_min_time = (options.min_benchmarking_time346 / (res.time_real + prepare_result.time_real + shell_spawning_time.time_real))347 as u64;348349 let count = {
26 unmodified lines
376 }377378 // Gather statistics379 for _ in 0..count_remaining {370 run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;380 run_preparation_command(&options.shell, &prepare_cmd, options.command_output_policy)?;381382 let msg = {383 let mean = format_duration(mean(&times_real), options.time_unit);384 format!("Current estimate: {}", mean.to_string().green())
5 unmodified lines
390391 let (res, status) = time_shell_command(392 &options.shell,393 cmd,384 options.show_output,385 options.failure_action,394 options.command_output_policy,395 options.command_failure_action,396 Some(shell_spawning_time),397 )?;398 let success = status.success();399
107 unmodified lines
507 // Run cleanup command508 let cleanup_cmd = options.cleanup_command.as_ref().map(|cleanup_command| {509 Command::new_parametrized(None, cleanup_command, cmd.get_parameters().clone())510 });501 run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;511 run_cleanup_command(&options.shell, &cleanup_cmd, options.command_output_policy)?;512513 Ok(BenchmarkResult {514 command: command_name,515 mean: t_mean,
15 unmodified lines
modified:src/benchmark/scheduler.rs+1 added-1 removed
30 unmodified lines
31 pub fn run_benchmarks(&mut self) -> Result<()> {32 let shell_spawning_time = mean_shell_spawning_time(33 &self.options.shell,34 self.options.output_style,35 self.options.show_output,35 self.options.command_output_policy,36 )?;3738 for (num, cmd) in self.commands.iter().enumerate() {39 self.results
53 unmodified lines
modified:src/options.rs+42 added-20 removed
101 unmodified lines
102 pub max: Option<u64>,103}104105impl Default for RunBounds {106 fn default() -> RunBounds {106 fn default() -> Self {107 RunBounds { min: 10, max: None }108 }109}110111/// A set of options for hyperfine112pub struct Options {113 /// Number of warmup runs114 pub warmup_count: u64,111/// How to handle the output of benchmarked commands112#[derive(Debug, Clone, Copy, PartialEq)]113pub enum CommandOutputPolicy {114 /// Discard all output115 Discard,116117 /// Show command output on the terminal118 Forward,119}120121impl Default for CommandOutputPolicy {122 fn default() -> Self {123 CommandOutputPolicy::Discard124 }125}126127/// The main settings for a hyperfine benchmark session128pub struct Options {129 /// Upper and lower bound for the number of benchmark runs130 pub run_bounds: RunBounds,131132 /// Number of warmup runs133 pub warmup_count: u64,134135 /// Minimum benchmarking time120 pub min_time_sec: Second,136 pub min_benchmarking_time: Second,137138 /// Whether or not to ignore non-zero exit codes123 pub failure_action: CmdFailureAction,139 pub command_failure_action: CmdFailureAction,140125 /// Command to run before each batch of timing runs126 pub setup_command: Option<String>,127141 /// Command to run before each timing run142 pub preparation_command: Option<Vec<String>>,143131 /// Command to run after each benchmark144 /// Command to run before each *batch* of timing runs, i.e. before each individual benchmark145 pub setup_command: Option<String>,146147 /// Command to run after each *batch* of timing runs, i.e. after each individual benchmark148 pub cleanup_command: Option<String>,149134 /// What color mode to use for output150 /// What color mode to use for the terminal output151 pub output_style: OutputStyleOption,152153 /// The shell to use for executing commands.154 pub shell: Shell,155140 /// Forward benchmark's stdout to hyperfine's stdout141 pub show_output: bool,156 /// What to do with the output of the benchmarked command157 pub command_output_policy: CommandOutputPolicy,158159 /// Which time unit to use for CLI & Markdown output160 pub time_unit: Option<Unit>,161
8 unmodified lines
170 Options {171 names: None,172 warmup_count: 0,173 run_bounds: RunBounds::default(),158 min_time_sec: 3.0,159 failure_action: CmdFailureAction::RaiseError,174 min_benchmarking_time: 3.0,175 command_failure_action: CmdFailureAction::RaiseError,176 setup_command: None,177 preparation_command: None,178 cleanup_command: None,179 output_style: OutputStyleOption::Full,180 shell: Shell::default(),165 show_output: false,181 command_output_policy: CommandOutputPolicy::Discard,182 time_unit: None,183 }184 }185}
47 unmodified lines
233 .map(|values| values.map(String::from).collect::<Vec<String>>());234235 options.cleanup_command = matches.value_of("cleanup").map(String::from);236221 options.show_output = matches.is_present("show-output");237 options.command_output_policy = if matches.is_present("show-output") {238 CommandOutputPolicy::Forward239 } else {240 CommandOutputPolicy::Discard241 };242243 options.output_style = match matches.value_of("style") {244 Some("full") => OutputStyleOption::Full,245 Some("basic") => OutputStyleOption::Basic,246 Some("nocolor") => OutputStyleOption::NoColor,247 Some("color") => OutputStyleOption::Color,248 Some("none") => OutputStyleOption::Disabled,249 _ => {230 if !options.show_output && atty::is(Stream::Stdout) {250 if options.command_output_policy == CommandOutputPolicy::Discard251 && atty::is(Stream::Stdout)252 {253 OutputStyleOption::Full254 } else {255 OutputStyleOption::Basic256 }
14 unmodified lines
271 options.shell = Shell::parse(shell)?;272 }273274 if matches.is_present("ignore-failure") {253 options.failure_action = CmdFailureAction::Ignore;275 options.command_failure_action = CmdFailureAction::Ignore;276 }277278 options.time_unit = match matches.value_of("time-unit") {279 Some("millisecond") => Some(Unit::MilliSecond),
67 unmodified lines