Skip to content
Myr

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 file

Take 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 back
1use std::cmp;

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

line 2why, one step back
2use std::env;

Run 'cargo fmt'sharkdpbefore adoption22491e2

line 3why, one step back
3use std::error::Error;

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

line 4why, one step back
4use std::io;

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

line 5why, one step back
5use std::ops::Range;

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 6–7why, one step back
6
7use atty::Stream;

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

line 8why, one step back
8use clap::ArgMatches;

Run 'cargo fmt'sharkdpbefore adoption22491e2

line 9why, one step back
9use colored::*;

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 10–12why, one step back
10
11mod hyperfine;12

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

lines 13–18why, one step back
13use 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 back
19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,

Run 'cargo fmt'sharkdpbefore adoption22491e2

line 20why, one step back
20};

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

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

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 22–29why, one step back
22
23/// 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 commands

Extract computation of relative speedsharkdpbefore adoptione09707c

line 30why, one step back
30fn run(

Use slices instead of borrowed vectorsphilippbefore adoptionc0ada7d

line 31why, one step back
31 commands: &[Command<'_>],

Extract computation of relative speedsharkdpbefore adoptione09707c

lines 32–33why, one step back
32 options: &HyperfineOptions,
33) -> io::Result<Vec<BenchmarkResult>> {

Format source codesharkdpbefore adoptiona4cf1ea

line 34why, one step back
34 let shell_spawning_time =

Pass `Copy`able enums by valuephilippbefore adoption16210a1

line 35why, one step back
35 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 back
36

Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777

lines 37–38why, one step back
37 let mut timing_results = vec![];
38

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 39–40why, one step back
39 // Run the benchmarks
40 for (num, cmd) in commands.iter().enumerate() {

Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777

line 41why, one step back
41 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 back
42 }
43

Change the structure of everything to remove Manager trait.Steve Pentlandbefore adoption7c72777

line 44why, one step back
44 Ok(timing_results)

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 45–46why, one step back
45}
46

Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425

lines 47–48why, one step back
47/// A function to read the `--parameter-scan` arguments
48fn parse_parameter_scan_args<'a>(

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

line 49why, one step back
49 mut vals: clap::Values<'a>,

Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425

line 50why, one step back
50) -> Result<(&'a str, Range<i32>), ParameterScanError> {

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 51–54why, one step back
51 let param_name = vals.next().unwrap();
52 let param_min: i32 = vals.next().unwrap().parse()?;53 let param_max: i32 = vals.next().unwrap().parse()?;54

Avoid long literal without separatorphilippbefore adoption5849446

line 55why, one step back
55 const MAX_PARAMETERS: i32 = 100_000;

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

line 56why, one step back
56 if param_max - param_min > MAX_PARAMETERS {

Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425

line 57why, one step back
57 return Err(ParameterScanError::TooLarge);

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 58–60why, one step back
58 }
5960 if param_max < param_min {

Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425

line 61why, one step back
61 return Err(ParameterScanError::EmptyRange);

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 62–63why, one step back
62 }
63

Avoid redundant returnphilippbefore adoptioncfd298e

line 64why, one step back
64 Ok((param_name, param_min..(param_max + 1)))

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 65–66why, one step back
65}
66

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

line 67why, one step back
67fn main() {

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

lines 68–71why, one step back
68 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 back
72

Moved `types::Second` and `types::Unit` to new `unit` moduleJason Peacockbefore adoption3aa26a3

lines 73–75why, one step back
73 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 back
76 };

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

line 77why, one step back
77

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

lines 78–80why, one step back
78 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 back
81 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 back
82 if let Err(e) = ans {
83 error(&format!(

Fix a typo in main.rsFelix Yanbefore adoption4d68c5f

line 84why, one step back
84 "The following error occurred while exporting: {}",

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

lines 85–91why, one step back
85 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 back
92

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

line 93why, one step back
93/// Build the HyperfineOptions that correspond to the given ArgMatches

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

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

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

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

Factor common operations into closurephilippbefore adoption3f79985

line 96why, one step back
96 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 back
97

Factor common operations into closurephilippbefore adoption3f79985

line 98why, one step back
98 options.warmup_count = param_to_u64("warmup")

Add option to set the shell, closes #61Jason Peacockbefore adoption01581f6

line 99why, one step back
99 .unwrap_or(options.warmup_count);

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

line 100why, one step back
100

Factor common operations into closurephilippbefore adoption3f79985

lines 101–102why, one step back
101 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 back
103

Factor common operations into closurephilippbefore adoption3f79985

line 104why, one step back
104 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 back
105 min_runs = Some(runs);
106 max_runs = Some(runs);

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 107–108why, one step back
107 }
108

Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b

lines 109–123why, one step back
109 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 back
124 options.runs.max = Some(max);

Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b

lines 125–130why, one step back
125 }
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 back
131 options.runs.max = Some(max);

Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b

lines 132–135why, one step back
132 }
133 (None, None) => {}134 };135

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 136–137why, one step back
136 options.preparation_command = matches.value_of("prepare").map(String::from);
137

add cleanup command which is executed at the end of each benchmarkColin Wahlbefore adoption5af43cf

lines 138–139why, one step back
138 options.cleanup_command = matches.value_of("cleanup").map(String::from);
139

Print stderr with `--show-output` flagChristian Duerrbefore adoption556788c

line 140why, one step back
140 options.show_output = matches.is_present("show-output");

Allow forwarding benchmark's stdout to hyperfineChristian Duerrbefore adoption4be4354

line 141why, one step back
141

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

lines 142–144why, one step back
142 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 back
145 Some("nocolor") => OutputStyleOption::NoColor,

Compact output, add `-s color` option, closes #70Jason Peacockbefore adoption3246ec2

line 146why, one step back
146 Some("color") => OutputStyleOption::Color,

cargo fmtsharkdpbefore adoptionf85e61d

lines 147–153why, one step back
147 _ => {
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 back
154 };

Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed

line 155why, one step back
155

Make checks more conciseSteve Pentlandbefore adoption4829dfb

line 156why, one step back
156 // We default Windows to NoColor if full had been specified.

Add 'nocolor' option for output.Steve Pentlandbefore adoptionfaf00b6

lines 157–160why, one step back
157 if cfg!(windows) && options.output_style == OutputStyleOption::Full {
158 options.output_style = OutputStyleOption::NoColor;159 }160

Compact output, add `-s color` option, closes #70Jason Peacockbefore adoption3246ec2

line 161why, one step back
161 match options.output_style {

Change color override partsharkdpbefore adoptiondf17139

lines 162–165why, one step back
162 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 back
166 };
167

Format source codesharkdpbefore adoptiona4cf1ea

lines 168–171why, one step back
168 options.shell = matches
169 .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 back
172
173 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 back
176
177 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 back
182

Added options to specify the max/exact numbers of runs.Diogo Sousabefore adoptiond78c33b

line 183why, one step back
183 Ok(options)

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

lines 184–187why, one step back
184}
185186/// Build the ExportManager that will export the results specified187/// in the given ArgMatches

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

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

All parsing of arguments now split out.Steve Pentlandbefore adoption6b19af9

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

Reduce repetitionphilippbefore adoptionfdc92cb

lines 190–199why, one step back
190 {
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 back
200 }
201 export_manager202}203204/// Build the commands to benchmark

convert to rust edition 2018Andrii Radykbefore adoption2d0b048

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

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

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

Return value directlyphilippbefore adoptionb573617

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

Rename parameter to '--parameter-scan'sharkdpbefore adoption07f1425

line 209why, one step back
209 match parse_parameter_scan_args(args) {

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 210–213why, one step back
210 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 back
214 for cmd in &command_strings {

Implement '--parameter-range var min max', see #19sharkdpbefore adoptione862cf8

lines 215–222why, one step back
215 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 back
223 command_strings.map(Command::new).collect()

Return value directlyphilippbefore adoptionb573617

line 224why, one step back
224 }

Don't include mod decl when on WindowsSteve Pentlandbefore adoption501d7b7

line 225why, one step back
225}