From 556788c160f8ae9faf0b73d75a3773b1afbdbd24 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Mon, 21 May 2018 21:04:30 +0200 Subject: [PATCH] Print stderr with `--show-output` flag The `--print-stdout` flag has been changed to `--show-output` and now both stderr and stdout are forwarded when this option is specified. --- src/hyperfine/app.rs | 9 +++++---- src/hyperfine/benchmark.rs | 28 ++++++++++++++-------------- src/hyperfine/shell.rs | 16 ++++++++-------- src/hyperfine/types.rs | 4 ++-- src/main.rs | 6 +++--- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/hyperfine/app.rs b/src/hyperfine/app.rs index b399d12..fb7d4a1 100644 --- a/src/hyperfine/app.rs +++ b/src/hyperfine/app.rs @@ -119,12 +119,13 @@ fn build_app() -> App<'static, 'static> { .help("Export the timing results as a Markdown table to the given FILE."), ) .arg( - Arg::with_name("print-stdout") - .long("print-stdout") + Arg::with_name("show-output") + .long("show-output") .conflicts_with("style") - .help("Print the stdout of the benchmark instead of suppressing it. \ + .help("Print the stdout and stderr of the benchmark instead of suppressing it. \ This will increase the time it takes for benchmarks to run, \ - so it should only be used when trying to benchmark output speed."), + so it should only be used for debugging purposes or \ + when trying to benchmark output speed."), ) .help_message("Print this help message.") .version_message("Show version information.") diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index c4b89dd..1444a2b 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -39,18 +39,18 @@ fn subtract_shell_spawning_time(time: Second, shell_spawning_time: Second) -> Se /// Run the given shell command and measure the execution time pub fn time_shell_command( command: &Command, - print_stdout: bool, + show_output: bool, failure_action: CmdFailureAction, shell_spawning_time: Option, ) -> io::Result<(TimingResult, bool)> { let wallclock_timer = WallClockTimer::start(); - let stdout = if print_stdout { - Stdio::inherit() + let (stdout, stderr) = if show_output { + (Stdio::inherit(), Stdio::inherit()) } else { - Stdio::null() + (Stdio::null(), Stdio::null()) }; - let result = execute_and_time(stdout, &command.get_shell_command())?; + let result = execute_and_time(stdout, stderr, &command.get_shell_command())?; let mut time_user = result.user_time; let mut time_system = result.system_time; @@ -85,7 +85,7 @@ pub fn time_shell_command( /// Measure the average shell spawning time pub fn mean_shell_spawning_time( style: &OutputStyleOption, - print_stdout: bool, + show_output: bool, ) -> io::Result { const COUNT: u64 = 200; let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style); @@ -98,7 +98,7 @@ pub fn mean_shell_spawning_time( // Just run the shell without any command let res = time_shell_command( &Command::new(""), - print_stdout, + show_output, CmdFailureAction::RaiseError, None, ); @@ -131,12 +131,12 @@ pub fn mean_shell_spawning_time( /// Run the command specified by `--prepare`. fn run_preparation_command( command: &Option, - print_stdout: bool, + show_output: bool, ) -> io::Result { if let &Some(ref preparation_command) = command { let res = time_shell_command( &Command::new(preparation_command), - print_stdout, + show_output, CmdFailureAction::RaiseError, None, ); @@ -183,7 +183,7 @@ pub fn run_benchmark( ); for _ in 0..options.warmup_count { - let _ = time_shell_command(cmd, options.print_stdout, options.failure_action, None)?; + let _ = time_shell_command(cmd, options.show_output, options.failure_action, None)?; progress_bar.inc(1); } progress_bar.finish_and_clear(); @@ -197,12 +197,12 @@ pub fn run_benchmark( ); // Run init / cleanup command - let prepare_res = run_preparation_command(&options.preparation_command, options.print_stdout)?; + let prepare_res = run_preparation_command(&options.preparation_command, options.show_output)?; // Initial timing run let (res, success) = time_shell_command( cmd, - options.print_stdout, + options.show_output, options.failure_action, Some(shell_spawning_time), )?; @@ -232,7 +232,7 @@ pub fn run_benchmark( // Gather statistics for _ in 0..count_remaining { - run_preparation_command(&options.preparation_command, options.print_stdout)?; + run_preparation_command(&options.preparation_command, options.show_output)?; let msg = { let mean = format_duration(mean(×_real), None); @@ -242,7 +242,7 @@ pub fn run_benchmark( let (res, success) = time_shell_command( cmd, - options.print_stdout, + options.show_output, options.failure_action, Some(shell_spawning_time), )?; diff --git a/src/hyperfine/shell.rs b/src/hyperfine/shell.rs index 1ef1917..9032e3e 100644 --- a/src/hyperfine/shell.rs +++ b/src/hyperfine/shell.rs @@ -19,8 +19,8 @@ pub struct ExecuteResult { /// Execute the given command and return a timing summary #[cfg(windows)] -pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result { - let mut child = run_shell_command(stdout, command)?; +pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result { + let mut child = run_shell_command(stdout, stderr, command)?; let cpu_timer = get_cpu_timer(&child); let status = child.wait()?; @@ -34,10 +34,10 @@ pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result io::Result { +pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result { let cpu_timer = get_cpu_timer(); - let status = run_shell_command(stdout, command)?; + let status = run_shell_command(stdout, stderr, command)?; let (user_time, system_time) = cpu_timer.stop(); @@ -50,24 +50,24 @@ pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result io::Result { +fn run_shell_command(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result { Command::new("sh") .arg("-c") .arg(command) .stdin(Stdio::null()) .stdout(stdout) - .stderr(Stdio::null()) + .stderr(stderr) .status() } /// Run a Windows shell command using cmd.exe #[cfg(windows)] -fn run_shell_command(stdout: Stdio, command: &str) -> io::Result { +fn run_shell_command(stdout: Stdio, stderr: Stdio, command: &str) -> io::Result { Command::new("cmd") .arg("/C") .arg(command) .stdin(Stdio::null()) .stdout(stdout) - .stderr(Stdio::null()) + .stderr(stderr) .spawn() } diff --git a/src/hyperfine/types.rs b/src/hyperfine/types.rs index 3032c51..66c86a9 100644 --- a/src/hyperfine/types.rs +++ b/src/hyperfine/types.rs @@ -91,7 +91,7 @@ pub struct HyperfineOptions { pub output_style: OutputStyleOption, /// Forward benchmark's stdout to hyperfine's stdout - pub print_stdout: bool, + pub show_output: bool, } impl Default for HyperfineOptions { @@ -103,7 +103,7 @@ impl Default for HyperfineOptions { failure_action: CmdFailureAction::RaiseError, preparation_command: None, output_style: OutputStyleOption::Full, - print_stdout: false, + show_output: false, } } } diff --git a/src/main.rs b/src/main.rs index b109fa0..bdd908f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ pub fn error(message: &str) -> ! { /// Runs the benchmark for the given commands fn run(commands: &Vec, options: &HyperfineOptions) -> io::Result> { let shell_spawning_time = - mean_shell_spawning_time(&options.output_style, options.print_stdout)?; + mean_shell_spawning_time(&options.output_style, options.show_output)?; let mut timing_results = vec![]; @@ -129,13 +129,13 @@ fn build_hyperfine_options(matches: &ArgMatches) -> HyperfineOptions { options.preparation_command = matches.value_of("prepare").map(String::from); - options.print_stdout = matches.is_present("print-stdout"); + options.show_output = matches.is_present("show-output"); options.output_style = match matches.value_of("style") { Some("full") => OutputStyleOption::Full, Some("basic") => OutputStyleOption::Basic, Some("nocolor") => OutputStyleOption::NoColor, - _ => if !options.print_stdout && atty::is(Stream::Stdout) { + _ => if !options.show_output && atty::is(Stream::Stdout) { OutputStyleOption::Full } else { OutputStyleOption::Basic