Turn show_output into enum

This commit is contained in:
David Peter 2022-02-20 13:15:54 +01:00 committed by David Peter
parent f9334e15fc
commit 0ebd76dfc2
3 changed files with 81 additions and 49 deletions

View File

@ -10,7 +10,7 @@ use statistical::{mean, median, standard_deviation};
use crate::benchmark::result::BenchmarkResult; use crate::benchmark::result::BenchmarkResult;
use crate::command::Command; use crate::command::Command;
use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell}; use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};
use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD}; use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
use crate::output::format::{format_duration, format_duration_unit}; use crate::output::format::{format_duration, format_duration_unit};
use crate::output::progress_bar::get_progress_bar; use crate::output::progress_bar::get_progress_bar;
@ -52,14 +52,13 @@ fn subtract_shell_spawning_time(time: Second, shell_spawning_time: Second) -> Se
pub fn time_shell_command( pub fn time_shell_command(
shell: &Shell, shell: &Shell,
command: &Command<'_>, command: &Command<'_>,
show_output: bool, command_output_policy: CommandOutputPolicy,
failure_action: CmdFailureAction, failure_action: CmdFailureAction,
shell_spawning_time: Option<TimingResult>, shell_spawning_time: Option<TimingResult>,
) -> Result<(TimingResult, ExitStatus)> { ) -> Result<(TimingResult, ExitStatus)> {
let (stdout, stderr) = if show_output { let (stdout, stderr) = match command_output_policy {
(Stdio::inherit(), Stdio::inherit()) CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),
} else { CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
(Stdio::null(), Stdio::null())
}; };
let wallclock_timer = WallClockTimer::start(); let wallclock_timer = WallClockTimer::start();
@ -101,7 +100,7 @@ pub fn time_shell_command(
pub fn mean_shell_spawning_time( pub fn mean_shell_spawning_time(
shell: &Shell, shell: &Shell,
style: OutputStyleOption, style: OutputStyleOption,
show_output: bool, command_output_policy: CommandOutputPolicy,
) -> Result<TimingResult> { ) -> Result<TimingResult> {
const COUNT: u64 = 50; const COUNT: u64 = 50;
let progress_bar = if style != OutputStyleOption::Disabled { let progress_bar = if style != OutputStyleOption::Disabled {
@ -123,7 +122,7 @@ pub fn mean_shell_spawning_time(
let res = time_shell_command( let res = time_shell_command(
shell, shell,
&Command::new(None, ""), &Command::new(None, ""),
show_output, command_output_policy,
CmdFailureAction::RaiseError, CmdFailureAction::RaiseError,
None, None,
); );
@ -167,11 +166,17 @@ pub fn mean_shell_spawning_time(
fn run_intermediate_command( fn run_intermediate_command(
shell: &Shell, shell: &Shell,
command: &Option<Command<'_>>, command: &Option<Command<'_>>,
show_output: bool, command_output_policy: CommandOutputPolicy,
error_output: &'static str, error_output: &'static str,
) -> Result<TimingResult> { ) -> Result<TimingResult> {
if let Some(ref cmd) = command { if let Some(ref cmd) = command {
let res = time_shell_command(shell, cmd, show_output, CmdFailureAction::RaiseError, None); let res = time_shell_command(
shell,
cmd,
command_output_policy,
CmdFailureAction::RaiseError,
None,
);
if res.is_err() { if res.is_err() {
bail!(error_output); bail!(error_output);
} }
@ -186,36 +191,36 @@ fn run_intermediate_command(
fn run_setup_command( fn run_setup_command(
shell: &Shell, shell: &Shell,
command: &Option<Command<'_>>, command: &Option<Command<'_>>,
show_output: bool, output_policy: CommandOutputPolicy,
) -> Result<TimingResult> { ) -> Result<TimingResult> {
let error_output = "The setup command terminated with a non-zero exit code. \ let error_output = "The setup command terminated with a non-zero exit code. \
Append ' || true' to the command if you are sure that this can be ignored."; Append ' || true' to the command if you are sure that this can be ignored.";
run_intermediate_command(shell, command, show_output, error_output) run_intermediate_command(shell, command, output_policy, error_output)
} }
/// Run the command specified by `--prepare`. /// Run the command specified by `--prepare`.
fn run_preparation_command( fn run_preparation_command(
shell: &Shell, shell: &Shell,
command: &Option<Command<'_>>, command: &Option<Command<'_>>,
show_output: bool, output_policy: CommandOutputPolicy,
) -> Result<TimingResult> { ) -> Result<TimingResult> {
let error_output = "The preparation command terminated with a non-zero exit code. \ let error_output = "The preparation command terminated with a non-zero exit code. \
Append ' || true' to the command if you are sure that this can be ignored."; Append ' || true' to the command if you are sure that this can be ignored.";
run_intermediate_command(shell, command, show_output, error_output) run_intermediate_command(shell, command, output_policy, error_output)
} }
/// Run the command specified by `--cleanup`. /// Run the command specified by `--cleanup`.
fn run_cleanup_command( fn run_cleanup_command(
shell: &Shell, shell: &Shell,
command: &Option<Command<'_>>, command: &Option<Command<'_>>,
show_output: bool, output_policy: CommandOutputPolicy,
) -> Result<TimingResult> { ) -> Result<TimingResult> {
let error_output = "The cleanup command terminated with a non-zero exit code. \ let error_output = "The cleanup command terminated with a non-zero exit code. \
Append ' || true' to the command if you are sure that this can be ignored."; Append ' || true' to the command if you are sure that this can be ignored.";
run_intermediate_command(shell, command, show_output, error_output) run_intermediate_command(shell, command, output_policy, error_output)
} }
#[cfg(unix)] #[cfg(unix)]
@ -276,7 +281,7 @@ pub fn run_benchmark(
let setup_cmd = options.setup_command.as_ref().map(|setup_command| { let setup_cmd = options.setup_command.as_ref().map(|setup_command| {
Command::new_parametrized(None, setup_command, cmd.get_parameters().clone()) Command::new_parametrized(None, setup_command, cmd.get_parameters().clone())
}); });
run_setup_command(&options.shell, &setup_cmd, options.show_output)?; run_setup_command(&options.shell, &setup_cmd, options.command_output_policy)?;
// Warmup phase // Warmup phase
if options.warmup_count > 0 { if options.warmup_count > 0 {
@ -291,12 +296,16 @@ pub fn run_benchmark(
}; };
for _ in 0..options.warmup_count { for _ in 0..options.warmup_count {
let _ = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; let _ = run_preparation_command(
&options.shell,
&prepare_cmd,
options.command_output_policy,
)?;
let _ = time_shell_command( let _ = time_shell_command(
&options.shell, &options.shell,
cmd, cmd,
options.show_output, options.command_output_policy,
options.failure_action, options.command_failure_action,
None, None,
)?; )?;
if let Some(bar) = progress_bar.as_ref() { if let Some(bar) = progress_bar.as_ref() {
@ -319,20 +328,21 @@ pub fn run_benchmark(
None None
}; };
let prepare_result = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; let prepare_result =
run_preparation_command(&options.shell, &prepare_cmd, options.command_output_policy)?;
// Initial timing run // Initial timing run
let (res, status) = time_shell_command( let (res, status) = time_shell_command(
&options.shell, &options.shell,
cmd, cmd,
options.show_output, options.command_output_policy,
options.failure_action, options.command_failure_action,
Some(shell_spawning_time), Some(shell_spawning_time),
)?; )?;
let success = status.success(); let success = status.success();
// Determine number of benchmark runs // Determine number of benchmark runs
let runs_in_min_time = (options.min_time_sec let runs_in_min_time = (options.min_benchmarking_time
/ (res.time_real + prepare_result.time_real + shell_spawning_time.time_real)) / (res.time_real + prepare_result.time_real + shell_spawning_time.time_real))
as u64; as u64;
@ -367,7 +377,7 @@ pub fn run_benchmark(
// Gather statistics // Gather statistics
for _ in 0..count_remaining { for _ in 0..count_remaining {
run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; run_preparation_command(&options.shell, &prepare_cmd, options.command_output_policy)?;
let msg = { let msg = {
let mean = format_duration(mean(&times_real), options.time_unit); let mean = format_duration(mean(&times_real), options.time_unit);
@ -381,8 +391,8 @@ pub fn run_benchmark(
let (res, status) = time_shell_command( let (res, status) = time_shell_command(
&options.shell, &options.shell,
cmd, cmd,
options.show_output, options.command_output_policy,
options.failure_action, options.command_failure_action,
Some(shell_spawning_time), Some(shell_spawning_time),
)?; )?;
let success = status.success(); let success = status.success();
@ -498,7 +508,7 @@ pub fn run_benchmark(
let cleanup_cmd = options.cleanup_command.as_ref().map(|cleanup_command| { let cleanup_cmd = options.cleanup_command.as_ref().map(|cleanup_command| {
Command::new_parametrized(None, cleanup_command, cmd.get_parameters().clone()) Command::new_parametrized(None, cleanup_command, cmd.get_parameters().clone())
}); });
run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?; run_cleanup_command(&options.shell, &cleanup_cmd, options.command_output_policy)?;
Ok(BenchmarkResult { Ok(BenchmarkResult {
command: command_name, command: command_name,

View File

@ -32,7 +32,7 @@ impl<'a> Scheduler<'a> {
let shell_spawning_time = mean_shell_spawning_time( let shell_spawning_time = mean_shell_spawning_time(
&self.options.shell, &self.options.shell,
self.options.output_style, self.options.output_style,
self.options.show_output, self.options.command_output_policy,
)?; )?;
for (num, cmd) in self.commands.iter().enumerate() { for (num, cmd) in self.commands.iter().enumerate() {

View File

@ -103,42 +103,58 @@ pub struct RunBounds {
} }
impl Default for RunBounds { impl Default for RunBounds {
fn default() -> RunBounds { fn default() -> Self {
RunBounds { min: 10, max: None } RunBounds { min: 10, max: None }
} }
} }
/// A set of options for hyperfine /// How to handle the output of benchmarked commands
pub struct Options { #[derive(Debug, Clone, Copy, PartialEq)]
/// Number of warmup runs pub enum CommandOutputPolicy {
pub warmup_count: u64, /// Discard all output
Discard,
/// Show command output on the terminal
Forward,
}
impl Default for CommandOutputPolicy {
fn default() -> Self {
CommandOutputPolicy::Discard
}
}
/// The main settings for a hyperfine benchmark session
pub struct Options {
/// Upper and lower bound for the number of benchmark runs /// Upper and lower bound for the number of benchmark runs
pub run_bounds: RunBounds, pub run_bounds: RunBounds,
/// Number of warmup runs
pub warmup_count: u64,
/// Minimum benchmarking time /// Minimum benchmarking time
pub min_time_sec: Second, pub min_benchmarking_time: Second,
/// Whether or not to ignore non-zero exit codes /// Whether or not to ignore non-zero exit codes
pub failure_action: CmdFailureAction, pub command_failure_action: CmdFailureAction,
/// Command to run before each batch of timing runs
pub setup_command: Option<String>,
/// Command to run before each timing run /// Command to run before each timing run
pub preparation_command: Option<Vec<String>>, pub preparation_command: Option<Vec<String>>,
/// Command to run after each benchmark /// Command to run before each *batch* of timing runs, i.e. before each individual benchmark
pub setup_command: Option<String>,
/// Command to run after each *batch* of timing runs, i.e. after each individual benchmark
pub cleanup_command: Option<String>, pub cleanup_command: Option<String>,
/// What color mode to use for output /// What color mode to use for the terminal output
pub output_style: OutputStyleOption, pub output_style: OutputStyleOption,
/// The shell to use for executing commands. /// The shell to use for executing commands.
pub shell: Shell, pub shell: Shell,
/// Forward benchmark's stdout to hyperfine's stdout /// What to do with the output of the benchmarked command
pub show_output: bool, pub command_output_policy: CommandOutputPolicy,
/// Which time unit to use for CLI & Markdown output /// Which time unit to use for CLI & Markdown output
pub time_unit: Option<Unit>, pub time_unit: Option<Unit>,
@ -155,14 +171,14 @@ impl Default for Options {
names: None, names: None,
warmup_count: 0, warmup_count: 0,
run_bounds: RunBounds::default(), run_bounds: RunBounds::default(),
min_time_sec: 3.0, min_benchmarking_time: 3.0,
failure_action: CmdFailureAction::RaiseError, command_failure_action: CmdFailureAction::RaiseError,
setup_command: None, setup_command: None,
preparation_command: None, preparation_command: None,
cleanup_command: None, cleanup_command: None,
output_style: OutputStyleOption::Full, output_style: OutputStyleOption::Full,
shell: Shell::default(), shell: Shell::default(),
show_output: false, command_output_policy: CommandOutputPolicy::Discard,
time_unit: None, time_unit: None,
} }
} }
@ -218,7 +234,11 @@ impl Options {
options.cleanup_command = matches.value_of("cleanup").map(String::from); options.cleanup_command = matches.value_of("cleanup").map(String::from);
options.show_output = matches.is_present("show-output"); options.command_output_policy = if matches.is_present("show-output") {
CommandOutputPolicy::Forward
} else {
CommandOutputPolicy::Discard
};
options.output_style = match matches.value_of("style") { options.output_style = match matches.value_of("style") {
Some("full") => OutputStyleOption::Full, Some("full") => OutputStyleOption::Full,
@ -227,7 +247,9 @@ impl Options {
Some("color") => OutputStyleOption::Color, Some("color") => OutputStyleOption::Color,
Some("none") => OutputStyleOption::Disabled, Some("none") => OutputStyleOption::Disabled,
_ => { _ => {
if !options.show_output && atty::is(Stream::Stdout) { if options.command_output_policy == CommandOutputPolicy::Discard
&& atty::is(Stream::Stdout)
{
OutputStyleOption::Full OutputStyleOption::Full
} else { } else {
OutputStyleOption::Basic OutputStyleOption::Basic
@ -250,7 +272,7 @@ impl Options {
} }
if matches.is_present("ignore-failure") { if matches.is_present("ignore-failure") {
options.failure_action = CmdFailureAction::Ignore; options.command_failure_action = CmdFailureAction::Ignore;
} }
options.time_unit = match matches.value_of("time-unit") { options.time_unit = match matches.value_of("time-unit") {