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
Provenance
- Commit
0ebd76d- Tree
7ddab31- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
f9334e1
3 files changed+81 added−49 removed
modified:src/benchmark/mod.rs+38 added-28 removed
8 unmodified lines9 use statistical::{mean, median, standard_deviation};10 11 use crate::benchmark::result::BenchmarkResult;12 use crate::command::Command;13-use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell};13+use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};14 use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};15 use crate::output::format::{format_duration, format_duration_unit};16 use crate::output::progress_bar::get_progress_bar;17 use crate::output::warnings::Warnings;33 unmodified lines51 /// Run the given shell command and measure the execution time52 pub 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 };63 64 let wallclock_timer = WallClockTimer::start();65 let result = execute_and_time(stdout, stderr, &command.get_shell_command(), shell)?;33 unmodified lines99 /// Measure the average shell spawning time100 pub 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 lines121 // 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 lines165 166 fn 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 lines190 /// Run the command specified by `--setup`.191 fn 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.";198 194- run_intermediate_command(shell, command, show_output, error_output)199+ run_intermediate_command(shell, command, output_policy, error_output)200 }201 202 /// Run the command specified by `--prepare`.203 fn 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.";210 206- run_intermediate_command(shell, command, show_output, error_output)211+ run_intermediate_command(shell, command, output_policy, error_output)212 }213 214 /// Run the command specified by `--cleanup`.215 fn 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.";222 218- run_intermediate_command(shell, command, show_output, error_output)223+ run_intermediate_command(shell, command, output_policy, error_output)224 }225 226 #[cfg(unix)]227 fn extract_exit_code(status: ExitStatus) -> Option<i32> {52 unmodified lines280 // 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)?;285 286 // Warmup phase287 if options.warmup_count > 0 {288 let progress_bar = if options.output_style != OutputStyleOption::Disabled {6 unmodified lines295 None296 };297 298 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 lines327 } else {328 None329 };330 322- 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)?;333 334 // 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();343 344 // 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;348 349 let count = {26 unmodified lines376 }377 378 // 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)?;381 382 let msg = {383 let mean = format_duration(mean(×_real), options.time_unit);384 format!("Current estimate: {}", mean.to_string().green())5 unmodified lines390 391 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 lines507 // 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)?;512 513 Ok(BenchmarkResult {514 command: command_name,515 mean: t_mean,15 unmodified linesmodified:src/benchmark/scheduler.rs+1 added-1 removed
30 unmodified lines31 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 )?;37 38 for (num, cmd) in self.commands.iter().enumerate() {39 self.results53 unmodified linesmodified:src/options.rs+42 added-20 removed
101 unmodified lines102 pub max: Option<u64>,103 }104 105 impl Default for RunBounds {106- fn default() -> RunBounds {106+ fn default() -> Self {107 RunBounds { min: 10, max: None }108 }109 }110 111-/// A set of options for hyperfine112-pub 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)]113+pub enum CommandOutputPolicy {114+ /// Discard all output115+ Discard,116 117+ /// Show command output on the terminal118+ Forward,119+}120+121+impl Default for CommandOutputPolicy {122+ fn default() -> Self {123+ CommandOutputPolicy::Discard124+ }125+}126+127+/// The main settings for a hyperfine benchmark session128+pub struct Options {129 /// Upper and lower bound for the number of benchmark runs130 pub run_bounds: RunBounds,131 132+ /// Number of warmup runs133+ pub warmup_count: u64,134+135 /// Minimum benchmarking time120- pub min_time_sec: Second,136+ pub min_benchmarking_time: Second,137 138 /// Whether or not to ignore non-zero exit codes123- pub failure_action: CmdFailureAction,139+ pub command_failure_action: CmdFailureAction,140 125- /// Command to run before each batch of timing runs126- pub setup_command: Option<String>,127-141 /// Command to run before each timing run142 pub preparation_command: Option<Vec<String>>,143 131- /// 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>,146+147+ /// Command to run after each *batch* of timing runs, i.e. after each individual benchmark148 pub cleanup_command: Option<String>,149 134- /// What color mode to use for output150+ /// What color mode to use for the terminal output151 pub output_style: OutputStyleOption,152 153 /// The shell to use for executing commands.154 pub shell: Shell,155 140- /// 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,158 159 /// Which time unit to use for CLI & Markdown output160 pub time_unit: Option<Unit>,161 8 unmodified lines170 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 lines233 .map(|values| values.map(String::from).collect::<Vec<String>>());234 235 options.cleanup_command = matches.value_of("cleanup").map(String::from);236 221- 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+ };242 243 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 lines271 options.shell = Shell::parse(shell)?;272 }273 274 if matches.is_present("ignore-failure") {253- options.failure_action = CmdFailureAction::Ignore;275+ options.command_failure_action = CmdFailureAction::Ignore;276 }277 278 options.time_unit = match matches.value_of("time-unit") {279 Some("millisecond") => Some(Unit::MilliSecond),67 unmodified lines