Skip to content
Myr

main.rs

This file

Path

src/main.rs

167 lines · 4.9 KiB · Rust

Last changed by

Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed

History of this file

Take it

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

line 1why, one step back
1extern crate atty;

Modify clap settingssharkdpbefore adoption5cdf013

line 2why, one step back
2#[macro_use]

Initial commitsharkdpbefore adoptiona658ab8

line 3
3extern crate clap;

Update warnings, switch to colored cratesharkdpbefore adoptiond5c21ec

line 4why, one step back
4extern crate colored;

Add a progress barsharkdpbefore adoptiond4ebdd7

line 5why, one step back
5extern crate indicatif;

Use statistical cratesharkdpbefore adoption05e5899

line 6why, one step back
6extern crate statistical;

Initial changes to skip libc for cpu time on windowsSteve Pentlandbefore adoption1ddf729

line 7why, one step back
7

Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed

line 8why, one step back
8#[cfg(not(windows))]

Initial changes to skip libc for cpu time on windowsSteve Pentlandbefore adoption1ddf729

line 9why, one step back
9extern crate libc;

Initial commitsharkdpbefore adoptiona658ab8

line 10
10

Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed

line 11why, one step back
11#[cfg(windows)]

Initial windows timing supportSteve Pentlandbefore adoption42635fb

lines 12–13why, one step back
12extern crate winapi;
13

Add unit testssharkdpbefore adoption241b89f

lines 14–17why, one step back
14#[cfg(test)]
15#[macro_use]16extern crate approx;17

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

line 18why, one step back
18use std::cmp;

ErrorsHenri Menkebefore adoption219bb1e

lines 19–20why, one step back
19use std::error::Error;
20use std::io;

Initial commitsharkdpbefore adoptiona658ab8

line 21
21

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

line 22why, one step back
22use atty::Stream;

Update warnings, switch to colored cratesharkdpbefore adoptiond5c21ec

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

Initial commitsharkdpbefore adoptiona658ab8

lines 24–25
24use clap::{App, AppSettings, Arg};
25

Refactor code into separate modulessharkdpbefore adoption2ba3160

line 26why, one step back
26mod hyperfine;

Handling of errors and warningssharkdpbefore adoption7e41c2a

line 27why, one step back
27

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

line 28why, one step back
28use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};

Refactor code into separate modulessharkdpbefore adoption2ba3160

line 29why, one step back
29use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};

Handling of errors and warningssharkdpbefore adoption7e41c2a

line 30why, one step back
30

ErrorsHenri Menkebefore adoption219bb1e

lines 31–32why, one step back
31/// Print error message to stderr and terminate
32pub fn error(message: &str) -> ! {

Update warnings, switch to colored cratesharkdpbefore adoptiond5c21ec

line 33why, one step back
33 eprintln!("{} {}", "Error:".red(), message);

ErrorsHenri Menkebefore adoption219bb1e

lines 34–36why, one step back
34 std::process::exit(1);
35}36

Correct for shell spawning time, closes #15sharkdpbefore adoption984cec7

lines 37–38why, one step back
37/// Runs the benchmark for the given commands
38fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

line 39why, one step back
39 let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;

Correct for shell spawning time, closes #15sharkdpbefore adoption984cec7

lines 40–41why, one step back
40
41 // Run the benchmarks

Output stylingsharkdpbefore adoption407c738

line 42why, one step back
42 for (num, cmd) in commands.iter().enumerate() {

Fix a few clippy warningssharkdpbefore adoption35a5139

line 43why, one step back
43 run_benchmark(num, cmd, shell_spawning_time, options)?;

Correct for shell spawning time, closes #15sharkdpbefore adoption984cec7

lines 44–46why, one step back
44 }
4546 Ok(())

Refactoringsharkdpbefore adoption9ba7ada

lines 47–48why, one step back
47}
48

Initial commitsharkdpbefore adoptiona658ab8

line 49
49fn main() {

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

lines 50–52why, one step back
50 // Process command line options
51 let mut options = HyperfineOptions::default();52

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

lines 53–58why, one step back
53 let clap_color_setting = if atty::is(Stream::Stdout) {
54 AppSettings::ColoredHelp55 } else {56 AppSettings::ColorNever57 };58

Initial commitsharkdpbefore adoptiona658ab8

line 59
59 let matches = App::new("hyperfine")

Modify clap settingssharkdpbefore adoption5cdf013

line 60why, one step back
60 .version(crate_version!())

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

line 61why, one step back
61 .setting(clap_color_setting)

Modify clap settingssharkdpbefore adoption5cdf013

line 62why, one step back
62 .setting(AppSettings::DeriveDisplayOrder)

Clean up help textsharkdpbefore adoptionb6ca2d6

lines 63–64why, one step back
63 .setting(AppSettings::UnifiedHelpMessage)
64 .max_term_width(90)

Update --help textsharkdpbefore adoptionf44f24c

line 65why, one step back
65 .about("A command-line benchmarking tool.")

Initial commitsharkdpbefore adoptiona658ab8

lines 66–69
66 .arg(
67 Arg::with_name("command")68 .help("Command to benchmark")69 .required(true)

ErrorsHenri Menkebefore adoption219bb1e

lines 70–71why, one step back
70 .multiple(true)
71 .empty_values(false),

Initial commitsharkdpbefore adoptiona658ab8

line 72
72 )

Add --warmup option, closes #1sharkdpbefore adoption68fdc2c

lines 73–78why, one step back
73 .arg(
74 Arg::with_name("warmup")75 .long("warmup")76 .short("w")77 .takes_value(true)78 .value_name("NUM")

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

lines 79–80why, one step back
79 .help(
80 "Perform NUM warmup runs before the actual benchmark. This can be used \

Update --help textsharkdpbefore adoptionf44f24c

line 81why, one step back
81 to fill (disk) caches for I/O-heavy programs.",

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

line 82why, one step back
82 ),

Add --warmup option, closes #1sharkdpbefore adoption68fdc2c

line 83why, one step back
83 )

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

lines 84–89why, one step back
84 .arg(
85 Arg::with_name("min-runs")86 .long("min-runs")87 .short("m")88 .takes_value(true)89 .value_name("NUM")

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

line 90why, one step back
90 .help(&format!(

Update --help textsharkdpbefore adoptionf44f24c

line 91why, one step back
91 "Perform at least NUM runs for each command (default: {}).",

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

lines 92–93why, one step back
92 options.min_runs
93 )),

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

line 94why, one step back
94 )

Clean up help textsharkdpbefore adoptionb6ca2d6

line 95why, one step back
95 .arg(

Rename --setup to --preparesharkdpbefore adoptiona656fbe

lines 96–98why, one step back
96 Arg::with_name("prepare")
97 .long("prepare")98 .short("p")

Clean up help textsharkdpbefore adoptionb6ca2d6

lines 99–101why, one step back
99 .takes_value(true)
100 .value_name("CMD")101 .help(

Rename --setup to --preparesharkdpbefore adoptiona656fbe

line 102why, one step back
102 "Execute CMD before each timing run. This is useful for \

Update --help textsharkdpbefore adoptionf44f24c

line 103why, one step back
103 clearing disk caches, for example.",

Clean up help textsharkdpbefore adoptionb6ca2d6

lines 104–105why, one step back
104 ),
105 )

Rename --setup to --preparesharkdpbefore adoptiona656fbe

line 106why, one step back
106 .arg(

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

lines 107–113why, one step back
107 Arg::with_name("style")
108 .long("style")109 .short("s")110 .takes_value(true)111 .value_name("TYPE")112 .possible_values(&["auto", "basic", "full"])113 .help(

Updates for 0.4.0sharkdpbefore adoption89e6d37

lines 114–116why, one step back
114 "Set output style type (default: auto). Set this to 'basic' to disable output \
115 coloring and interactive elements. Set it to 'full' to enable all effects \116 even if no interactive terminal was detected.",

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

lines 117–119why, one step back
117 ),
118 )119 .arg(

Rename --setup to --preparesharkdpbefore adoptiona656fbe

lines 120–122why, one step back
120 Arg::with_name("ignore-failure")
121 .long("ignore-failure")122 .short("i")

Update --help textsharkdpbefore adoptionf44f24c

line 123why, one step back
123 .help("Ignore non-zero exit codes."),

Rename --setup to --preparesharkdpbefore adoptiona656fbe

line 124why, one step back
124 )

Update --help textsharkdpbefore adoptionf44f24c

lines 125–126why, one step back
125 .help_message("Print this help message.")
126 .version_message("Show version information.")

Initial commitsharkdpbefore adoptiona658ab8

lines 127–128
127 .get_matches();
128

Add --setup option, closes #8sharkdpbefore adoptiona4df90e

lines 129–130why, one step back
129 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();
130

Warmup count as u64 onlysharkdpbefore adoption50b88b5

lines 131–134why, one step back
131 options.warmup_count = matches
132 .value_of("warmup")133 .and_then(&str_to_u64)134 .unwrap_or(0);

Code style updatesharkdpbefore adoption197f9fb

line 135why, one step back
135

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

line 136why, one step back
136 if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {

Use statistical cratesharkdpbefore adoption05e5899

lines 137–138why, one step back
137 // we need at least two runs to compute a variance
138 options.min_runs = cmp::max(2, min_runs);

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

line 139why, one step back
139 }

Clean up help textsharkdpbefore adoptionb6ca2d6

line 140why, one step back
140

Rename --setup to --preparesharkdpbefore adoptiona656fbe

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

Add new style option to disable coloring and progress bars (#24)Steve Pentlandbefore adoption2e24ff0

lines 143–151why, one step back
143 options.output_style = match matches.value_of("style") {
144 Some("full") => OutputStyleOption::Full,145 Some("basic") => OutputStyleOption::Basic,146 _ => if atty::is(Stream::Stdout) {147 OutputStyleOption::Full148 } else {149 OutputStyleOption::Basic150 },151 };

Initial working version with child proc HANDLE.Steve Pentlandbefore adoption9753eed

line 152why, one step back
152

No hybrid between basic and fullsharkdpbefore adoptioneb92bd4

lines 153–154why, one step back
153 if options.output_style == OutputStyleOption::Basic {
154 colored::control::set_override(false);

Exclude entire cputime file, force basic outputSteve Pentlandbefore adoptionff798ad

lines 155–156why, one step back
155 }
156

Use enum instead of boolsharkdpbefore adoptiond42994b

lines 157–159why, one step back
157 if matches.is_present("ignore-failure") {
158 options.failure_action = CmdFailureAction::Ignore;159 }

Add --min-runs, closes #2sharkdpbefore adoptiondab3f94

line 160why, one step back
160

Correct for shell spawning time, closes #15sharkdpbefore adoption984cec7

lines 161–162why, one step back
161 let commands = matches.values_of("command").unwrap().collect();
162 let res = run(&commands, &options);

Handling of errors and warningssharkdpbefore adoption7e41c2a

line 163why, one step back
163

Fix a few clippy warningssharkdpbefore adoption35a5139

lines 164–165why, one step back
164 if let Err(e) = res {
165 error(e.description());

Initial commitsharkdpbefore adoptiona658ab8

lines 166–167
166 }
167}