main.rs
This file
Path
src/main.rs
225 lines · 7.2 KiB · Rust
Last changed by
Pass `Copy`able enums by valuephilippbefore adoption16210a1
History of this fileTake it
The walk stopped after 200 commits: lines older than 501d7b7 point at it.
Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 1why, one step back1use std::cmp;All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
line 2why, one step back2use std::env;Run 'cargo fmt'sharkdpbefore adoption22491e2
line 3why, one step back3use std::error::Error;Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 4why, one step back4use std::io;Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
line 5why, one step back5use std::ops::Range;Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 6–7why, one step back67use atty::Stream;All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
line 8why, one step back8use clap::ArgMatches;Run 'cargo fmt'sharkdpbefore adoption22491e2
line 9why, one step back9use colored::*;Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 10–12why, one step back1011mod hyperfine;12convert to rust edition 2018Andrii Radykbefore adoption2d0b048
lines 13–18why, one step back13use crate::hyperfine::app::get_arg_matches;14use crate::hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};15use crate::hyperfine::error::{OptionsError, ParameterScanError};16use crate::hyperfine::export::{ExportManager, ExportType};17use crate::hyperfine::internal::write_benchmark_comparison;18use crate::hyperfine::types::{Moved `types::Second` and `types::Unit` to new `unit` moduleJason Peacockbefore adoption3aa26a3
line 19why, one step back19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,Run 'cargo fmt'sharkdpbefore adoption22491e2
line 20why, one step back20};convert to rust edition 2018Andrii Radykbefore adoption2d0b048
line 21why, one step back21use crate::hyperfine::units::Unit;Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 22–29why, one step back2223/// Print error message to stderr and terminate24pub fn error(message: &str) -> ! {25 eprintln!("{} {}", "Error:".red(), message);26 std::process::exit(1);27}2829/// Runs the benchmark for the given commandsExtract computation of relative speedsharkdpbefore adoptione09707c
line 30why, one step back30fn run(Use slices instead of borrowed vectorsphilippbefore adoptionc0ada7d
line 31why, one step back31 commands: &[Command<'_>],Extract computation of relative speedsharkdpbefore adoptione09707c
lines 32–33why, one step back32 options: &HyperfineOptions,33) -> io::Result<Vec<BenchmarkResult>> {Format source codesharkdpbefore adoptiona4cf1ea
line 34why, one step back34 let shell_spawning_time =Pass `Copy`able enums by valuephilippbefore adoption16210a1
line 35why, one step back35 mean_shell_spawning_time(&options.shell, options.output_style, options.show_output)?;Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 36why, one step back36Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777
lines 37–38why, one step back37 let mut timing_results = vec![];38Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 39–40why, one step back39 // Run the benchmarks40 for (num, cmd) in commands.iter().enumerate() {Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777
line 41why, one step back41 timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 42–43why, one step back42 }43Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777
line 44why, one step back44 Ok(timing_results)Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 45–46why, one step back45}46Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425
lines 47–48why, one step back47/// A function to read the `--parameter-scan` arguments48fn parse_parameter_scan_args<'a>(Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
line 49why, one step back49 mut vals: clap::Values<'a>,Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425
line 50why, one step back50) -> Result<(&'a str, Range<i32>), ParameterScanError> {Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 51–54why, one step back51 let param_name = vals.next().unwrap();52 let param_min: i32 = vals.next().unwrap().parse()?;53 let param_max: i32 = vals.next().unwrap().parse()?;54Avoid long literal without separatorphilippbefore adoption5849446
line 55why, one step back55 const MAX_PARAMETERS: i32 = 100_000;Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
line 56why, one step back56 if param_max - param_min > MAX_PARAMETERS {Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425
line 57why, one step back57 return Err(ParameterScanError::TooLarge);Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 58–60why, one step back58 }5960 if param_max < param_min {Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425
line 61why, one step back61 return Err(ParameterScanError::EmptyRange);Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 62–63why, one step back62 }63Avoid redundant returnphilippbefore adoptioncfd298e
line 64why, one step back64 Ok((param_name, param_min..(param_max + 1)))Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 65–66why, one step back65}66Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 67why, one step back67fn main() {All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 68–71why, one step back68 let matches = get_arg_matches(env::args_os());69 let options = build_hyperfine_options(&matches);70 let export_manager = build_export_manager(&matches);71 let commands = build_commands(&matches);Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 72why, one step back72Moved `types::Second` and `types::Unit` to new `unit` moduleJason Peacockbefore adoption3aa26a3
lines 73–75why, one step back73 let res = match options {74 Ok(ref opts) => run(&commands, &opts),75 Err(ref e) => error(e.description()),Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
line 76why, one step back76 };Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 77why, one step back77All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 78–80why, one step back78 match res {79 Ok(timing_results) => {80 write_benchmark_comparison(&timing_results);Add option to set time units for CLI & Markdown, closes #80Jason Peacockbefore adoptionf0b6f13
line 81why, one step back81 let ans = export_manager.write_results(timing_results, options.unwrap().time_unit);All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 82–83why, one step back82 if let Err(e) = ans {83 error(&format!(Fix a typo in main.rsFelix Yanbefore adoption4d68c5f
line 84why, one step back84 "The following error occurred while exporting: {}",All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 85–91why, one step back85 e.description()86 ));87 }88 }89 Err(e) => error(e.description()),90 }91}Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 92why, one step back92All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
line 93why, one step back93/// Build the HyperfineOptions that correspond to the given ArgMatchesconvert to rust edition 2018Andrii Radykbefore adoption2d0b048
line 94why, one step back94fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions, OptionsError> {All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
line 95why, one step back95 let mut options = HyperfineOptions::default();Factor common operations into closurephilippbefore adoption3f79985
line 96why, one step back96 let param_to_u64 = |param| matches.value_of(param).and_then(|n| u64::from_str_radix(n, 10).ok());Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 97why, one step back97Factor common operations into closurephilippbefore adoption3f79985
line 98why, one step back98 options.warmup_count = param_to_u64("warmup")Add option to set the shell, closes #61Jason Peacockbefore adoption01581f6
line 99why, one step back99 .unwrap_or(options.warmup_count);Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 100why, one step back100Factor common operations into closurephilippbefore adoption3f79985
lines 101–102why, one step back101 let mut min_runs = param_to_u64("min-runs");102 let mut max_runs = param_to_u64("max-runs");Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
line 103why, one step back103Factor common operations into closurephilippbefore adoption3f79985
line 104why, one step back104 if let Some(runs) = param_to_u64("runs") {Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
lines 105–106why, one step back105 min_runs = Some(runs);106 max_runs = Some(runs);Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 107–108why, one step back107 }108Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
lines 109–123why, one step back109 match (min_runs, max_runs) {110 (Some(min), _) if min < 2 => {111 // We need at least two runs to compute a variance.112 return Err(OptionsError::RunsBelowTwo);113 }114 (Some(min), None) => {115 options.runs.min = min;116 }117 (_, Some(max)) if max < 2 => {118 // We need at least two runs to compute a variance.119 return Err(OptionsError::RunsBelowTwo);120 }121 (None, Some(max)) => {122 // Since the minimum was not explicit we lower it if max is below the default min.123 options.runs.min = cmp::min(options.runs.min, max);Addressed review comments.Diogo Sousabefore adoption0466a1d
line 124why, one step back124 options.runs.max = Some(max);Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
lines 125–130why, one step back125 }126 (Some(min), Some(max)) if min > max => {127 return Err(OptionsError::EmptyRunsRange);128 }129 (Some(min), Some(max)) => {130 options.runs.min = min;Addressed review comments.Diogo Sousabefore adoption0466a1d
line 131why, one step back131 options.runs.max = Some(max);Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
lines 132–135why, one step back132 }133 (None, None) => {}134 };135Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 136–137why, one step back136 options.preparation_command = matches.value_of("prepare").map(String::from);137add cleanup command which is executed at the end of each benchmarkColin Wahlbefore adoption5af43cf
lines 138–139why, one step back138 options.cleanup_command = matches.value_of("cleanup").map(String::from);139Print stderr with `--show-output` flagChristian Duerrbefore adoption556788c
line 140why, one step back140 options.show_output = matches.is_present("show-output");Allow forwarding benchmark's stdout to hyperfineChristian Duerrbefore adoption4be4354
line 141why, one step back141Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 142–144why, one step back142 options.output_style = match matches.value_of("style") {143 Some("full") => OutputStyleOption::Full,144 Some("basic") => OutputStyleOption::Basic,Add 'nocolor' option for output.Steve Pentlandbefore adoptionfaf00b6
line 145why, one step back145 Some("nocolor") => OutputStyleOption::NoColor,Compact output, add `-s color` option, closes #70Jason Peacockbefore adoption3246ec2
line 146why, one step back146 Some("color") => OutputStyleOption::Color,cargo fmtsharkdpbefore adoptionf85e61d
lines 147–153why, one step back147 _ => {148 if !options.show_output && atty::is(Stream::Stdout) {149 OutputStyleOption::Full150 } else {151 OutputStyleOption::Basic152 }153 }Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 154why, one step back154 };Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed
line 155why, one step back155Make checks more conciseSteve Pentlandbefore adoption4829dfb
line 156why, one step back156 // We default Windows to NoColor if full had been specified.Add 'nocolor' option for output.Steve Pentlandbefore adoptionfaf00b6
lines 157–160why, one step back157 if cfg!(windows) && options.output_style == OutputStyleOption::Full {158 options.output_style = OutputStyleOption::NoColor;159 }160Compact output, add `-s color` option, closes #70Jason Peacockbefore adoption3246ec2
line 161why, one step back161 match options.output_style {Change color override partsharkdpbefore adoptiondf17139
lines 162–165why, one step back162 OutputStyleOption::Basic | OutputStyleOption::NoColor => {163 colored::control::set_override(false)164 }165 _ => {}Add option to set the shell, closes #61Jason Peacockbefore adoption01581f6
lines 166–167why, one step back166 };167Format source codesharkdpbefore adoptiona4cf1ea
lines 168–171why, one step back168 options.shell = matches169 .value_of("shell")170 .unwrap_or(&options.shell)171 .to_string();Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
lines 172–175why, one step back172173 if matches.is_present("ignore-failure") {174 options.failure_action = CmdFailureAction::Ignore;175 }Add option to set time units for CLI & Markdown, closes #80Jason Peacockbefore adoptionf0b6f13
lines 176–181why, one step back176177 options.time_unit = match matches.value_of("time-unit") {178 Some("millisecond") => Some(Unit::MilliSecond),179 Some("second") => Some(Unit::Second),180 _ => None,181 };Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 182why, one step back182Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b
line 183why, one step back183 Ok(options)All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 184–187why, one step back184}185186/// Build the ExportManager that will export the results specified187/// in the given ArgMatchesconvert to rust edition 2018Andrii Radykbefore adoption2d0b048
line 188why, one step back188fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
line 189why, one step back189 let mut export_manager = ExportManager::new();Reduce repetitionphilippbefore adoptionfdc92cb
lines 190–199why, one step back190 {191 let mut add_exporter = |flag, exporttype| {192 if let Some(filename) = matches.value_of(flag) {193 export_manager.add_exporter(exporttype, filename);194 }195 };196 add_exporter("export-asciidoc", ExportType::Asciidoc);197 add_exporter("export-json", ExportType::Json);198 add_exporter("export-csv", ExportType::Csv);199 add_exporter("export-markdown", ExportType::Markdown);All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9
lines 200–204why, one step back200 }201 export_manager202}203204/// Build the commands to benchmarkconvert to rust edition 2018Andrii Radykbefore adoption2d0b048
line 205why, one step back205fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 206–207why, one step back206 let command_strings = matches.values_of("command").unwrap();207Return value directlyphilippbefore adoptionb573617
line 208why, one step back208 if let Some(args) = matches.values_of("parameter-scan") {Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425
line 209why, one step back209 match parse_parameter_scan_args(args) {Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 210–213why, one step back210 Ok((param_name, param_range)) => {211 let mut commands = vec![];212 let command_strings = command_strings.collect::<Vec<&str>>();213 for value in param_range.start..param_range.end {Avoid redundant refphilippbefore adoptionf3f8c9c
line 214why, one step back214 for cmd in &command_strings {Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8
lines 215–222why, one step back215 commands.push(Command::new_parametrized(cmd, param_name, value));216 }217 }218 commands219 }220 Err(e) => error(e.description()),221 }222 } else {Avoid closurephilippbefore adoptiona5ce659
line 223why, one step back223 command_strings.map(Command::new).collect()Return value directlyphilippbefore adoptionb573617
line 224why, one step back224 }Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7
line 225why, one step back225}