Add --warmup option, closes #1
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 197f9fb · Browse files · 1 file·+44 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+44 added−10 removed
- other:+44 added−10 removed
Provenance
- Commit
68fdc2c- Tree
c0e4e1b- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
197f9fb
1 file changed+44 added−10 removed
modified:src/main.rs+44 added-10 removed
43 unmodified lines44 45 CmdResult::new(execution_time_sec, status.success())46 }47 48+/// Return a pre-configured progress bar49+fn get_progress_bar(length: u64, msg: &str) -> ProgressBar {50+ let progressbar_style = ProgressStyle::default_spinner()51+ .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")52+ .template(" {spinner} {msg:<28} {wide_bar} ETA {eta_precise}");53+54+ let bar = ProgressBar::new(length);55+ bar.set_style(progressbar_style.clone());56+ bar.enable_steady_tick(80);57+ bar.set_message(msg);58+59+ bar60+}61+62 fn main() {63 let matches = App::new("hyperfine")64 .global_settings(&[AppSettings::ColoredHelp])65 .version("0.1")4 unmodified lines70 .help("Command to benchmark")71 .required(true)72 .multiple(true),73 )74+ .arg(75+ Arg::with_name("warmup")76+ .long("warmup")77+ .short("w")78+ .takes_value(true)79+ .value_name("NUM")80+ .help("Perform NUM warmup runs before the actual benchmark"),81+ )82 .get_matches();83 84 let min_time_sec = 5.0;85 let min_runs = 10;86 65- let progressbar_style = ProgressStyle::default_spinner()66- .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")67- .template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}");68-87 let commands = matches.values_of("command").unwrap();88 89 for cmd in commands {90 println!("Command: {}", Cyan.paint(cmd));91 println!();92 93 let mut results = vec![];94 77- // Set up progress bar78- let bar = ProgressBar::new(min_runs);79- bar.set_style(progressbar_style.clone());80- bar.enable_steady_tick(80);81- bar.set_message("Initial time measurement");95+ // Warmup phase96+ if let Some(warmup_count) = matches97+ .value_of("warmup")98+ .and_then(|n| u64::from_str_radix(n, 10).ok())99+ {100+ let bar = get_progress_bar(warmup_count, "Performing warmup runs");101 102+ for _ in 1..warmup_count {103+ bar.inc(1);104+ let _ = time_shell_command(cmd);105+ }106+ bar.finish_and_clear();107+ }108+109+ // Set up progress bar (and spinner for initial measurement)110+ let bar = get_progress_bar(min_runs, "Initial time measurement");111+83- // Initial run112+ // Initial timing run113 let res = time_shell_command(cmd);114 115 let runs_in_min_time = (min_time_sec / res.execution_time_sec) as u64;116 2 unmodified lines119 } else {120 min_runs121 };122 123+ // Save the first result124 results.push(res);125 126+ // Re-configure the progress bar127 bar.set_length(count);128 bar.set_message("Collecting statistics");129 130+ // Gather statistics131 for _ in 1..count {132 bar.inc(1);133 let res = time_shell_command(cmd);134 results.push(res);135 }136 bar.finish_and_clear();137 138+ // Compute statistical quantities139 let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();140 let t_mean = t_sum / (results.len() as f64);141 142 let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();143 let t2_mean = t2_sum / (results.len() as f64);144 145 let stddev = (t2_mean - t_mean.powi(2)).sqrt();146 147+ // Formatting and console output148 let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);149 150 println!(" Time: {}", Green.paint(time_fmt));151 10 unmodified lines