From a4cf1eaa295fcf4f7a14f1f51291b7c444988682 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 21 Nov 2018 19:55:58 +0100 Subject: [PATCH] Format source code --- src/hyperfine/benchmark.rs | 23 +++- src/hyperfine/export/markdown.rs | 200 +++++++++++++++++-------------- src/hyperfine/shell.rs | 14 ++- src/main.rs | 8 +- 4 files changed, 146 insertions(+), 99 deletions(-) diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 3be7bb6..af0cd21 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -11,7 +11,9 @@ use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD}; use hyperfine::shell::execute_and_time; use hyperfine::timer::wallclocktimer::WallClockTimer; use hyperfine::timer::{TimerStart, TimerStop}; -use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption}; +use hyperfine::types::{ + BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, +}; use hyperfine::units::Second; use hyperfine::warnings::Warnings; @@ -117,10 +119,13 @@ pub fn mean_shell_spawning_time( }; return Err(io::Error::new( - io::ErrorKind::Other, - format!("Could not measure shell execution time. \ - Make sure you can run '{}'.", shell_cmd), - )) + io::ErrorKind::Other, + format!( + "Could not measure shell execution time. \ + Make sure you can run '{}'.", + shell_cmd + ), + )); } Ok((r, _)) => { times_real.push(r.time_real); @@ -189,7 +194,13 @@ pub fn run_benchmark( ); for _ in 0..options.warmup_count { - let _ = time_shell_command(&options.shell, cmd, options.show_output, options.failure_action, None)?; + let _ = time_shell_command( + &options.shell, + cmd, + options.show_output, + options.failure_action, + None, + )?; progress_bar.inc(1); } progress_bar.finish_and_clear(); diff --git a/src/hyperfine/export/markdown.rs b/src/hyperfine/export/markdown.rs index d008c5a..25b12a6 100644 --- a/src/hyperfine/export/markdown.rs +++ b/src/hyperfine/export/markdown.rs @@ -33,7 +33,10 @@ impl Exporter for MarkdownExporter { } fn table_header(unit_short_name: String) -> String { - format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=unit_short_name) + format!( + "| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", + unit = unit_short_name + ) } fn start_table(unit: Unit) -> Vec { @@ -49,12 +52,13 @@ fn add_table_row(dest: &mut Vec, entry: &BenchmarkResult, unit: Unit) { dest.extend( format!( "| `{command}` | {mean} ± {stddev} | {min}…{max} |\n", - command=entry.command.replace("|", "\\|"), - mean=mean_str, - stddev=stddev_str, - min=min_str, - max=max_str, - ).as_bytes(), + command = entry.command.replace("|", "\\|"), + mean = mean_str, + stddev = stddev_str, + min = min_str, + max = max_str, + ) + .as_bytes(), ); } @@ -71,34 +75,36 @@ fn test_markdown_format_ms() { let mut timing_results = vec![]; timing_results.push(BenchmarkResult::new( - String::from("sleep 0.1"), - 0.1057, // mean - 0.0016, // std dev - 0.0009, // user_mean - 0.0011, // system_mean - 0.1023, // min - 0.1080, // max - vec![0.1, 0.1, 0.1], // times - )); + String::from("sleep 0.1"), + 0.1057, // mean + 0.0016, // std dev + 0.0009, // user_mean + 0.0011, // system_mean + 0.1023, // min + 0.1080, // max + vec![0.1, 0.1, 0.1], // times + )); timing_results.push(BenchmarkResult::new( - String::from("sleep 2"), - 2.0050, // mean - 0.0020, // std dev - 0.0009, // user_mean - 0.0012, // system_mean - 2.0020, // min - 2.0080, // max - vec![2.0, 2.0, 2.0], // times - )); + String::from("sleep 2"), + 2.0050, // mean + 0.0020, // std dev + 0.0009, // user_mean + 0.0012, // system_mean + 2.0020, // min + 2.0080, // max + vec![2.0, 2.0, 2.0], // times + )); let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap(); let formatted_expected = format!( -"{}\ + "{}\ | `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 | | `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 | -", table_header("ms".to_string())); +", + table_header("ms".to_string()) + ); assert_eq!(formatted_expected, formatted); } @@ -112,34 +118,36 @@ fn test_markdown_format_s() { let mut timing_results = vec![]; timing_results.push(BenchmarkResult::new( - String::from("sleep 2"), - 2.0050, // mean - 0.0020, // std dev - 0.0009, // user_mean - 0.0012, // system_mean - 2.0020, // min - 2.0080, // max - vec![2.0, 2.0, 2.0], // times - )); + String::from("sleep 2"), + 2.0050, // mean + 0.0020, // std dev + 0.0009, // user_mean + 0.0012, // system_mean + 2.0020, // min + 2.0080, // max + vec![2.0, 2.0, 2.0], // times + )); timing_results.push(BenchmarkResult::new( - String::from("sleep 0.1"), - 0.1057, // mean - 0.0016, // std dev - 0.0009, // user_mean - 0.0011, // system_mean - 0.1023, // min - 0.1080, // max - vec![0.1, 0.1, 0.1], // times - )); + String::from("sleep 0.1"), + 0.1057, // mean + 0.0016, // std dev + 0.0009, // user_mean + 0.0011, // system_mean + 0.1023, // min + 0.1080, // max + vec![0.1, 0.1, 0.1], // times + )); let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap(); let formatted_expected = format!( -"{}\ + "{}\ | `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 | | `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 | -", table_header("s".to_string())); +", + table_header("s".to_string()) + ); assert_eq!(formatted_expected, formatted); } @@ -152,34 +160,41 @@ fn test_markdown_format_time_unit_s() { let mut timing_results = vec![]; timing_results.push(BenchmarkResult::new( - String::from("sleep 0.1"), - 0.1057, // mean - 0.0016, // std dev - 0.0009, // user_mean - 0.0011, // system_mean - 0.1023, // min - 0.1080, // max - vec![0.1, 0.1, 0.1], // times - )); + String::from("sleep 0.1"), + 0.1057, // mean + 0.0016, // std dev + 0.0009, // user_mean + 0.0011, // system_mean + 0.1023, // min + 0.1080, // max + vec![0.1, 0.1, 0.1], // times + )); timing_results.push(BenchmarkResult::new( - String::from("sleep 2"), - 2.0050, // mean - 0.0020, // std dev - 0.0009, // user_mean - 0.0012, // system_mean - 2.0020, // min - 2.0080, // max - vec![2.0, 2.0, 2.0], // times - )); + String::from("sleep 2"), + 2.0050, // mean + 0.0020, // std dev + 0.0009, // user_mean + 0.0012, // system_mean + 2.0020, // min + 2.0080, // max + vec![2.0, 2.0, 2.0], // times + )); - let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::Second)).unwrap()).unwrap(); + let formatted = String::from_utf8( + exporter + .serialize(&timing_results, Some(Unit::Second)) + .unwrap(), + ) + .unwrap(); let formatted_expected = format!( -"{}\ + "{}\ | `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 | | `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 | -", table_header("s".to_string())); +", + table_header("s".to_string()) + ); assert_eq!(formatted_expected, formatted); } @@ -193,34 +208,41 @@ fn test_markdown_format_time_unit_ms() { let mut timing_results = vec![]; timing_results.push(BenchmarkResult::new( - String::from("sleep 2"), - 2.0050, // mean - 0.0020, // std dev - 0.0009, // user_mean - 0.0012, // system_mean - 2.0020, // min - 2.0080, // max - vec![2.0, 2.0, 2.0], // times - )); + String::from("sleep 2"), + 2.0050, // mean + 0.0020, // std dev + 0.0009, // user_mean + 0.0012, // system_mean + 2.0020, // min + 2.0080, // max + vec![2.0, 2.0, 2.0], // times + )); timing_results.push(BenchmarkResult::new( - String::from("sleep 0.1"), - 0.1057, // mean - 0.0016, // std dev - 0.0009, // user_mean - 0.0011, // system_mean - 0.1023, // min - 0.1080, // max - vec![0.1, 0.1, 0.1], // times - )); + String::from("sleep 0.1"), + 0.1057, // mean + 0.0016, // std dev + 0.0009, // user_mean + 0.0011, // system_mean + 0.1023, // min + 0.1080, // max + vec![0.1, 0.1, 0.1], // times + )); - let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::MilliSecond)).unwrap()).unwrap(); + let formatted = String::from_utf8( + exporter + .serialize(&timing_results, Some(Unit::MilliSecond)) + .unwrap(), + ) + .unwrap(); let formatted_expected = format!( -"{}\ + "{}\ | `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 | | `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 | -", table_header("ms".to_string())); +", + table_header("ms".to_string()) + ); assert_eq!(formatted_expected, formatted); } diff --git a/src/hyperfine/shell.rs b/src/hyperfine/shell.rs index d069f17..543bc21 100644 --- a/src/hyperfine/shell.rs +++ b/src/hyperfine/shell.rs @@ -19,7 +19,12 @@ pub struct ExecuteResult { /// Execute the given command and return a timing summary #[cfg(windows)] -pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result { +pub fn execute_and_time( + stdout: Stdio, + stderr: Stdio, + command: &str, + shell: &str, +) -> io::Result { let mut child = run_shell_command(stdout, stderr, command, shell)?; let cpu_timer = get_cpu_timer(&child); let status = child.wait()?; @@ -34,7 +39,12 @@ pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str /// Execute the given command and return a timing summary #[cfg(not(windows))] -pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result { +pub fn execute_and_time( + stdout: Stdio, + stderr: Stdio, + command: &str, + shell: &str, +) -> io::Result { let cpu_timer = get_cpu_timer(); let status = run_shell_command(stdout, stderr, command, shell)?; diff --git a/src/main.rs b/src/main.rs index 9a0c49c..0d13f64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,8 @@ 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.shell, &options.output_style, options.show_output)?; + let shell_spawning_time = + mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?; let mut timing_results = vec![]; @@ -191,7 +192,10 @@ fn build_hyperfine_options(matches: &ArgMatches) -> Result {} }; - options.shell = matches.value_of("shell").unwrap_or(&options.shell).to_string(); + options.shell = matches + .value_of("shell") + .unwrap_or(&options.shell) + .to_string(); if matches.is_present("ignore-failure") { options.failure_action = CmdFailureAction::Ignore;