Skip to content
Myr

main.rs

This file

Path

src/main.rs

233 lines · 7.6 KiB · Rust

Last changed by

remove deprecated Error::descriptionAndrii Radykbefore adoption9a064b7

History of this file

Take it

The walk stopped after 200 commits: lines older than 0878544 point at it.

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 1–10why, one step back
1use std::cmp;
2use std::env;3use std::io;45use atty::Stream;6use clap::ArgMatches;7use colored::*;89mod hyperfine;10

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

lines 11–12why, one step back
11use crate::hyperfine::app::get_arg_matches;
12use crate::hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};

Add support for floats in --parameter-scanPiyush Rungtabefore adoptionb10b93f

line 13why, one step back
13use crate::hyperfine::error::OptionsError;

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 14why, one step back
14use crate::hyperfine::export::{ExportManager, ExportType};

Add simple tokenizer for parameter listsharkdpbefore adoption3e217f4

line 15why, one step back
15use crate::hyperfine::internal::{tokenize, write_benchmark_comparison};

Add support for floats in --parameter-scanPiyush Rungtabefore adoptionb10b93f

line 16why, one step back
16use crate::hyperfine::parameter_range::get_parameterized_commands;

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 17why, one step back
17use crate::hyperfine::types::{

Use 'enum' to represent numeric/textual parameter valuessharkdpbefore adoptiond7dc232

line 18why, one step back
18 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,

update help text for cleanup commandColin Wahlbefore adoption0878544

line 19why, one step back
19};

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 20why, one step back
20use crate::hyperfine::units::Unit;

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 21–28why, one step back
21
22/// Print error message to stderr and terminate23pub fn error(message: &str) -> ! {24 eprintln!("{} {}", "Error:".red(), message);25 std::process::exit(1);26}2728/// Runs the benchmark for the given commands

Run cargo fmtPiyush Rungtabefore adoption737403d

line 29why, one step back
29fn run(commands: &[Command<'_>], options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {

update help text for cleanup commandColin Wahlbefore adoption0878544

line 30why, one step back
30 let shell_spawning_time =

Pass `Copy`able enums by valuephilippbefore adoption16210a1

line 31why, one step back
31 mean_shell_spawning_time(&options.shell, options.output_style, options.show_output)?;

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 32–34why, one step back
32
33 let mut timing_results = vec![];34

raise error if preparation_command length is not equal to command or 1Saurav Sharmabefore adoptionbc82bd5

line 35why, one step back
35 if let Some(preparation_command) = &options.preparation_command {

Update error messagesharkdpbefore adoption33591d1

line 36why, one step back
36 if preparation_command.len() > 1 && commands.len() != preparation_command.len() {

raise error if preparation_command length is not equal to command or 1Saurav Sharmabefore adoptionbc82bd5

line 37why, one step back
37 error(

Update error messagesharkdpbefore adoption33591d1

lines 38–39why, one step back
38 "The '--prepare' option has to be provided just once or N times, where N is the \
39 number of benchmark commands.",

raise error if preparation_command length is not equal to command or 1Saurav Sharmabefore adoptionbc82bd5

lines 40–43why, one step back
40 );
41 }42 }43

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 44–59why, one step back
44 // Run the benchmarks
45 for (num, cmd) in commands.iter().enumerate() {46 timing_results.push(run_benchmark(num, cmd, shell_spawning_time, options)?);47 }4849 Ok(timing_results)50}5152fn main() {53 let matches = get_arg_matches(env::args_os());54 let options = build_hyperfine_options(&matches);55 let export_manager = build_export_manager(&matches);56 let commands = build_commands(&matches);5758 let res = match options {59 Ok(ref opts) => run(&commands, &opts),

remove deprecated Error::descriptionAndrii Radykbefore adoption9a064b7

line 60why, one step back
60 Err(ref e) => error(&e.to_string()),

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 61–64why, one step back
61 };
6263 match res {64 Ok(timing_results) => {

updates as requested in the reviewknidarknessbefore adoption34eb150

lines 65–71why, one step back
65 let options = options.unwrap();
6667 if options.output_style != OutputStyleOption::Disabled {68 write_benchmark_comparison(&timing_results);69 }7071 let ans = export_manager.write_results(timing_results, options.time_unit);

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 72–74why, one step back
72 if let Err(e) = ans {
73 error(&format!(74 "The following error occurred while exporting: {}",

remove deprecated Error::descriptionAndrii Radykbefore adoption9a064b7

line 75why, one step back
75 e

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 76–78why, one step back
76 ));
77 }78 }

remove deprecated Error::descriptionAndrii Radykbefore adoption9a064b7

line 79why, one step back
79 Err(e) => error(&e.to_string()),

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 80–83why, one step back
80 }
81}8283/// Build the HyperfineOptions that correspond to the given ArgMatches

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 84why, one step back
84fn build_hyperfine_options(matches: &ArgMatches<'_>) -> Result<HyperfineOptions, OptionsError> {

update help text for cleanup commandColin Wahlbefore adoption0878544

line 85why, one step back
85 let mut options = HyperfineOptions::default();

Run cargo fmtPiyush Rungtabefore adoption737403d

lines 86–90why, one step back
86 let param_to_u64 = |param| {
87 matches88 .value_of(param)89 .and_then(|n| u64::from_str_radix(n, 10).ok())90 };

update help text for cleanup commandColin Wahlbefore adoption0878544

line 91why, one step back
91

Run cargo fmtPiyush Rungtabefore adoption737403d

line 92why, one step back
92 options.warmup_count = param_to_u64("warmup").unwrap_or(options.warmup_count);

update help text for cleanup commandColin Wahlbefore adoption0878544

line 93why, one step back
93

Factor common operations into closurephilippbefore adoption3f79985

lines 94–95why, one step back
94 let mut min_runs = param_to_u64("min-runs");
95 let mut max_runs = param_to_u64("max-runs");

update help text for cleanup commandColin Wahlbefore adoption0878544

line 96why, one step back
96

Factor common operations into closurephilippbefore adoption3f79985

line 97why, one step back
97 if let Some(runs) = param_to_u64("runs") {

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 98–128why, one step back
98 min_runs = Some(runs);
99 max_runs = Some(runs);100 }101102 match (min_runs, max_runs) {103 (Some(min), _) if min < 2 => {104 // We need at least two runs to compute a variance.105 return Err(OptionsError::RunsBelowTwo);106 }107 (Some(min), None) => {108 options.runs.min = min;109 }110 (_, Some(max)) if max < 2 => {111 // We need at least two runs to compute a variance.112 return Err(OptionsError::RunsBelowTwo);113 }114 (None, Some(max)) => {115 // Since the minimum was not explicit we lower it if max is below the default min.116 options.runs.min = cmp::min(options.runs.min, max);117 options.runs.max = Some(max);118 }119 (Some(min), Some(max)) if min > max => {120 return Err(OptionsError::EmptyRunsRange);121 }122 (Some(min), Some(max)) => {123 options.runs.min = min;124 options.runs.max = Some(max);125 }126 (None, None) => {}127 };128

refactor codeSaurav Sharmabefore adoptiond1cd30b

lines 129–131why, one step back
129 options.preparation_command = matches
130 .values_of("prepare")131 .map(|values| values.map(String::from).collect::<Vec<String>>());

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 132–141why, one step back
132
133 options.cleanup_command = matches.value_of("cleanup").map(String::from);134135 options.show_output = matches.is_present("show-output");136137 options.output_style = match matches.value_of("style") {138 Some("full") => OutputStyleOption::Full,139 Some("basic") => OutputStyleOption::Basic,140 Some("nocolor") => OutputStyleOption::NoColor,141 Some("color") => OutputStyleOption::Color,

updates as requested in the reviewknidarknessbefore adoption34eb150

line 142why, one step back
142 Some("none") => OutputStyleOption::Disabled,

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 143–183why, one step back
143 _ => {
144 if !options.show_output && atty::is(Stream::Stdout) {145 OutputStyleOption::Full146 } else {147 OutputStyleOption::Basic148 }149 }150 };151152 // We default Windows to NoColor if full had been specified.153 if cfg!(windows) && options.output_style == OutputStyleOption::Full {154 options.output_style = OutputStyleOption::NoColor;155 }156157 match options.output_style {158 OutputStyleOption::Basic | OutputStyleOption::NoColor => {159 colored::control::set_override(false)160 }161 _ => {}162 };163164 options.shell = matches165 .value_of("shell")166 .unwrap_or(&options.shell)167 .to_string();168169 if matches.is_present("ignore-failure") {170 options.failure_action = CmdFailureAction::Ignore;171 }172173 options.time_unit = match matches.value_of("time-unit") {174 Some("millisecond") => Some(Unit::MilliSecond),175 Some("second") => Some(Unit::Second),176 _ => None,177 };178179 Ok(options)180}181182/// Build the ExportManager that will export the results specified183/// in the given ArgMatches

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 184why, one step back
184fn build_export_manager(matches: &ArgMatches<'_>) -> ExportManager {

update help text for cleanup commandColin Wahlbefore adoption0878544

line 185why, one step back
185 let mut export_manager = ExportManager::new();

Reduce repetitionphilippbefore adoptionfdc92cb

lines 186–195why, one step back
186 {
187 let mut add_exporter = |flag, exporttype| {188 if let Some(filename) = matches.value_of(flag) {189 export_manager.add_exporter(exporttype, filename);190 }191 };192 add_exporter("export-asciidoc", ExportType::Asciidoc);193 add_exporter("export-json", ExportType::Json);194 add_exporter("export-csv", ExportType::Csv);195 add_exporter("export-markdown", ExportType::Markdown);

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 196–200why, one step back
196 }
197 export_manager198}199200/// Build the commands to benchmark

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

line 201why, one step back
201fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {

update help text for cleanup commandColin Wahlbefore adoption0878544

lines 202–203why, one step back
202 let command_strings = matches.values_of("command").unwrap();
203

Return value directlyphilippbefore adoptionb573617

line 204why, one step back
204 if let Some(args) = matches.values_of("parameter-scan") {

Add support for floats in --parameter-scanPiyush Rungtabefore adoptionb10b93f

lines 205–207why, one step back
205 let step_size = matches.value_of("parameter-step-size");
206 match get_parameterized_commands(command_strings, args, step_size) {207 Ok(commands) => commands,

remove deprecated Error::descriptionAndrii Radykbefore adoption9a064b7

line 208why, one step back
208 Err(e) => error(&e.to_string()),

update help text for cleanup commandColin Wahlbefore adoption0878544

line 209why, one step back
209 }

Implement basic --parameter-list with naive list parsingHasan Ibraheembefore adoption4a5d08d

lines 210–213why, one step back
210 } else if let Some(mut args) = matches.values_of("parameter-list") {
211 let param_name = args.next().unwrap();212 let param_list_str = args.next().unwrap();213

Add simple tokenizer for parameter listsharkdpbefore adoption3e217f4

line 214why, one step back
214 let param_list = tokenize(param_list_str);

Implement basic --parameter-list with naive list parsingHasan Ibraheembefore adoption4a5d08d

lines 215–220why, one step back
215 let command_list = command_strings.collect::<Vec<&str>>();
216217 let mut commands = Vec::with_capacity(param_list.len() * command_list.len());218219 for value in param_list {220 for cmd in &command_list {

Use 'enum' to represent numeric/textual parameter valuessharkdpbefore adoptiond7dc232

lines 221–223why, one step back
221 commands.push(Command::new_parametrized(
222 cmd,223 param_name,

Add simple tokenizer for parameter listsharkdpbefore adoption3e217f4

line 224why, one step back
224 ParameterValue::Text(value.clone()),

Use 'enum' to represent numeric/textual parameter valuessharkdpbefore adoptiond7dc232

line 225why, one step back
225 ));

Implement basic --parameter-list with naive list parsingHasan Ibraheembefore adoption4a5d08d

lines 226–229why, one step back
226 }
227 }228229 commands

update help text for cleanup commandColin Wahlbefore adoption0878544

line 230why, one step back
230 } else {

Avoid closurephilippbefore adoptiona5ce659

line 231why, one step back
231 command_strings.map(Command::new).collect()

Return value directlyphilippbefore adoptionb573617

line 232why, one step back
232 }

update help text for cleanup commandColin Wahlbefore adoption0878544

line 233why, one step back
233}