From 5bdfdbf1cb797df2052f4b09cb61cb2c8b590d99 Mon Sep 17 00:00:00 2001 From: knidarkness Date: Sat, 12 Oct 2019 11:17:14 +0300 Subject: [PATCH] Added 'none' as a possible value for "style" option This commit allows to remove all non-errornous output of the tool. --- src/hyperfine/app.rs | 5 +- src/hyperfine/benchmark.rs | 107 +++++++++++++++++++++---------------- src/hyperfine/types.rs | 3 ++ src/main.rs | 6 ++- 4 files changed, 72 insertions(+), 49 deletions(-) diff --git a/src/hyperfine/app.rs b/src/hyperfine/app.rs index eefe9eb..471f7e2 100644 --- a/src/hyperfine/app.rs +++ b/src/hyperfine/app.rs @@ -135,13 +135,14 @@ fn build_app() -> App<'static, 'static> { .short("s") .takes_value(true) .value_name("TYPE") - .possible_values(&["auto", "basic", "full", "nocolor", "color"]) + .possible_values(&["auto", "basic", "full", "nocolor", "color", "none"]) .help( "Set output style type (default: auto). Set this to 'basic' to disable output \ coloring and interactive elements. Set it to 'full' to enable all effects \ even if no interactive terminal was detected. Set this to 'nocolor' to \ keep the interactive output without any colors. Set this to 'color' to keep \ - the colors without any interactive output.", + the colors without any interactive output. Set this to 'none' to disable all \ + the output of the tool.", ), ) .arg( diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 13b533b..f58d4c9 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -5,6 +5,8 @@ use std::process::Stdio; use colored::*; use statistical::{mean, median, standard_deviation}; +use indicatif::{ProgressBar}; + use crate::hyperfine::format::{format_duration, format_duration_unit}; use crate::hyperfine::internal::{get_progress_bar, max, min, MIN_EXECUTION_TIME}; use crate::hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD}; @@ -94,7 +96,10 @@ pub fn mean_shell_spawning_time( show_output: bool, ) -> io::Result { const COUNT: u64 = 200; - let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style); + let mut progress_bar: Option = None; + if style != OutputStyleOption::None { + progress_bar = Some(get_progress_bar(COUNT, "Measuring shell spawning time", style)); + } let mut times_real: Vec = vec![]; let mut times_user: Vec = vec![]; @@ -132,10 +137,10 @@ pub fn mean_shell_spawning_time( times_user.push(r.time_user); times_system.push(r.time_system); } - } - progress_bar.inc(1); + } + // if let Some(bar) = progress_bar { bar.inc(1); } } - progress_bar.finish_and_clear(); + if let Some(bar) = progress_bar { bar.finish_and_clear(); } Ok(TimingResult { time_real: mean(×_real), @@ -193,12 +198,14 @@ pub fn run_benchmark( shell_spawning_time: TimingResult, options: &HyperfineOptions, ) -> io::Result { - println!( - "{}{}: {}", - "Benchmark #".bold(), - (num + 1).to_string().bold(), - cmd - ); + if options.output_style != OutputStyleOption::None { + println!( + "{}{}: {}", + "Benchmark #".bold(), + (num + 1).to_string().bold(), + cmd + ); + } let mut times_real: Vec = vec![]; let mut times_user: Vec = vec![]; @@ -220,11 +227,14 @@ pub fn run_benchmark( // Warmup phase if options.warmup_count > 0 { - let progress_bar = get_progress_bar( - options.warmup_count, - "Performing warmup runs", - options.output_style, - ); + let mut progress_bar: Option = None; + if options.output_style != OutputStyleOption::None { + progress_bar = Some(get_progress_bar( + options.warmup_count, + "Performing warmup runs", + options.output_style, + )); + } for _ in 0..options.warmup_count { let _ = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; @@ -235,17 +245,21 @@ pub fn run_benchmark( options.failure_action, None, )?; - progress_bar.inc(1); + if let Some(pbar) = progress_bar.clone() { pbar.inc(1); } } - progress_bar.finish_and_clear(); + if let Some(bar) = progress_bar { bar.finish_and_clear(); } } // Set up progress bar (and spinner for initial measurement) - let progress_bar = get_progress_bar( - options.runs.min, - "Initial time measurement", - options.output_style, - ); + let mut progress_bar:Option = None; + + if options.output_style != OutputStyleOption::None { + progress_bar = Some(get_progress_bar( + options.runs.min, + "Initial time measurement", + options.output_style, + )) + } let prepare_res = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; @@ -284,8 +298,8 @@ pub fn run_benchmark( all_succeeded = all_succeeded && success; // Re-configure the progress bar - progress_bar.set_length(count); - progress_bar.inc(1); + if let Some(bar) = progress_bar.clone() { bar.set_length(count); } + if let Some(bar) = progress_bar.clone() { bar.inc(1); } // Gather statistics for _ in 0..count_remaining { @@ -295,7 +309,8 @@ pub fn run_benchmark( let mean = format_duration(mean(×_real), options.time_unit); format!("Current estimate: {}", mean.to_string().green()) }; - progress_bar.set_message(&msg); + + if let Some(bar) = progress_bar.clone() { bar.set_message(&msg); } let (res, success) = time_shell_command( &options.shell, @@ -311,9 +326,9 @@ pub fn run_benchmark( all_succeeded = all_succeeded && success; - progress_bar.inc(1); + if let Some(bar) = progress_bar.clone() { bar.inc(1); } } - progress_bar.finish_and_clear(); + if let Some(bar) = progress_bar { bar.finish_and_clear(); } // Compute statistical quantities let t_num = times_real.len(); @@ -336,24 +351,26 @@ pub fn run_benchmark( let (user_str, user_unit) = format_duration_unit(user_mean, None); let system_str = format_duration(system_mean, Some(user_unit)); - println!( - " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]", - "mean".green().bold(), - "σ".green(), - mean_str.green().bold(), - stddev_str.green(), - user_str.blue(), - system_str.blue() - ); + if options.output_style != OutputStyleOption::None { + println!( + " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]", + "mean".green().bold(), + "σ".green(), + mean_str.green().bold(), + stddev_str.green(), + user_str.blue(), + system_str.blue() + ); - println!( - " Range ({} … {}): {:>8} … {:>8} {}", - "min".cyan(), - "max".purple(), - min_str.cyan(), - max_str.purple(), - num_str.dimmed() - ); + println!( + " Range ({} … {}): {:>8} … {:>8} {}", + "min".cyan(), + "max".purple(), + min_str.cyan(), + max_str.purple(), + num_str.dimmed() + ); + } // Warnings let mut warnings = vec![]; @@ -384,7 +401,7 @@ pub fn run_benchmark( } } - println!(" "); + if options.output_style != OutputStyleOption::None { println!(" "); } // Run cleanup command let cleanup_cmd = diff --git a/src/hyperfine/types.rs b/src/hyperfine/types.rs index 584dd36..fd2738f 100644 --- a/src/hyperfine/types.rs +++ b/src/hyperfine/types.rs @@ -113,6 +113,9 @@ pub enum OutputStyleOption { /// Keep coloring, but use no progress bar Color, + + /// Disable all the output + None, } /// Number of runs for a benchmark diff --git a/src/main.rs b/src/main.rs index bd3c77a..e042338 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,7 @@ fn main() { let options = build_hyperfine_options(&matches); let export_manager = build_export_manager(&matches); let commands = build_commands(&matches); + let unwrapped_opts = options.unwrap(); let res = match options { Ok(ref opts) => run(&commands, &opts), @@ -63,8 +64,8 @@ fn main() { match res { Ok(timing_results) => { - write_benchmark_comparison(&timing_results); - let ans = export_manager.write_results(timing_results, options.unwrap().time_unit); + if unwrapped_opts.output_style != OutputStyleOption::None { write_benchmark_comparison(&timing_results); } + let ans = export_manager.write_results(timing_results, unwrapped_opts.time_unit); if let Err(e) = ans { error(&format!( "The following error occurred while exporting: {}", @@ -135,6 +136,7 @@ fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result OutputStyleOption::Basic, Some("nocolor") => OutputStyleOption::NoColor, Some("color") => OutputStyleOption::Color, + Some("none") => OutputStyleOption::None, _ => { if !options.show_output && atty::is(Stream::Stdout) { OutputStyleOption::Full