Refactoring
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent f8cbb33 · Browse files · 1 file·+94 added−73 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+94 added−73 removed
- other:+94 added−73 removed
Provenance
- Commit
9ba7ada- Tree
fca2cd2- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
f8cbb33
1 file changed+94 added−73 removed
modified:src/main.rs+94 added-73 removed
57 unmodified lines58 59 bar60 }61 62+/// Run the benchmark for a single shell command63+fn run_benchmark(cmd: &str, options: &HyperfineOptions) {64+ println!("Command: {}", Cyan.paint(cmd));65+ println!();66+67+ let mut results = vec![];68+69+ // Warmup phase70+ if let Some(warmup_count) = options.warmup_count71+ {72+ let bar = get_progress_bar(warmup_count, "Performing warmup runs");73+74+ for _ in 1..warmup_count {75+ bar.inc(1);76+ let _ = time_shell_command(cmd);77+ }78+ bar.finish_and_clear();79+ }80+81+ // Set up progress bar (and spinner for initial measurement)82+ let bar = get_progress_bar(options.min_runs, "Initial time measurement");83+84+ // Initial timing run85+ let res = time_shell_command(cmd);86+87+ let runs_in_min_time = (options.min_time_sec / res.execution_time_sec) as u64;88+89+ let count = if runs_in_min_time >= options.min_runs {90+ runs_in_min_time91+ } else {92+ options.min_runs93+ };94+95+ // Save the first result96+ results.push(res);97+98+ // Re-configure the progress bar99+ bar.set_length(count);100+ bar.set_message("Collecting statistics");101+102+ // Gather statistics103+ for _ in 1..count {104+ bar.inc(1);105+ let res = time_shell_command(cmd);106+ results.push(res);107+ }108+ bar.finish_and_clear();109+110+ // Compute statistical quantities111+ let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();112+ let t_mean = t_sum / (results.len() as f64);113+114+ let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();115+ let t2_mean = t2_sum / (results.len() as f64);116+117+ let stddev = (t2_mean - t_mean.powi(2)).sqrt();118+119+ // Formatting and console output120+ let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);121+122+ println!(" Time: {}", Green.paint(time_fmt));123+124+ if !results.iter().all(|r| r.success) {125+ println!(126+ " {}: Program returned non-zero exit status",127+ Red.paint("Warning")128+ );129+ };130+131+ println!();132+}133+134+pub struct HyperfineOptions {135+ pub warmup_count: Option<u64>,136+ pub min_runs: u64,137+ pub min_time_sec: f64,138+}139+140+impl Default for HyperfineOptions {141+ fn default() -> HyperfineOptions {142+ HyperfineOptions {143+ warmup_count: None,144+ min_runs: 10,145+ min_time_sec: 5.0146+ }147+ }148+}149+150 fn main() {151 let matches = App::new("hyperfine")152 .global_settings(&[AppSettings::ColoredHelp])153 .version("0.1")14 unmodified lines168 .help("Perform NUM warmup runs before the actual benchmark"),169 )170 .get_matches();171 84- let min_time_sec = 5.0;85- let min_runs = 10;172 87- let commands = matches.values_of("command").unwrap();173+ let mut options = HyperfineOptions::default();174+ options.warmup_count =175+ matches.value_of("warmup")176+ .and_then(|n| u64::from_str_radix(n, 10).ok());177 178+ let commands = matches.values_of("command").unwrap();179 for cmd in commands {90- println!("Command: {}", Cyan.paint(cmd));91- println!();92-93- let mut results = vec![];94-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-112- // 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-117- let count = if runs_in_min_time >= min_runs {118- runs_in_min_time119- } 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-152- if !results.iter().all(|r| r.success) {153- println!(154- " {}: Program returned non-zero exit status",155- Red.paint("Warning")156- );157- };158-159- println!();180+ run_benchmark(&cmd, &options);181 }182 }