Skip to content
Myr

Add --setup option, closes #8

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by sharkdp · parent b0bab45 · Browse files · 3 files+47 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+47 added−14 removed

  • other:+47 added−14 removed

3 files+47 added−14 removed

modified:src/hyperfine/benchmark.rs+11 added-1 removed
1use std::io;2use std::process::{Command, Stdio};3use std::time::Instant;45use ansi_term::Colour::{Cyan, Green, White, Yellow, Purple};5use ansi_term::Colour::{Cyan, Green, Purple, White, Yellow};6use statistical::{mean, standard_deviation};78use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,9 MIN_EXECUTION_TIME};
122 unmodified lines
132133 // Set up progress bar (and spinner for initial measurement)134 let bar = get_progress_bar(options.min_runs, "Initial time measurement");135136 // Run setup / cleanup command137 let run_setup_command = || {138 if let Some(ref setup_command) = options.setup_command {139 let _ = time_shell_command(setup_command, options.ignore_failure, None);140 }141 };142 run_setup_command();143144 // Initial timing run145 let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;146147 // Determine number of benchmark runs
16 unmodified lines
164 bar.set_length(count_remaining);165166 // Gather statistics167 for _ in 0..count_remaining {168 run_setup_command();169170 let msg = {171 let mean = format_duration(mean(&execution_times), Unit::Auto);172 format!("Current estimate: {}", Green.paint(mean))173 };
64 unmodified lines
modified:src/hyperfine/internal.rs+4 added
19 unmodified lines
20 pub min_time_sec: Second,2122 /// Whether or not to ignore non-zero exit codes23 pub ignore_failure: bool,2425 /// Command to run before each benchmark run26 pub setup_command: Option<String>,27}2829impl Default for HyperfineOptions {30 fn default() -> HyperfineOptions {
1 unmodified line
32 warmup_count: 0,33 min_runs: 10,34 min_time_sec: 3.0,35 ignore_failure: false,36 setup_command: None,37 }38 }39}40
48 unmodified lines
modified:src/main.rs+32 added-13 removed
33 unmodified lines
34 Ok(())35}3637fn main() {38 // Process command line options39 let mut options = HyperfineOptions::default();4041 let matches = App::new("hyperfine")42 .version(crate_version!())43 .setting(AppSettings::ColoredHelp)44 .setting(AppSettings::DeriveDisplayOrder)
5 unmodified lines
50 .multiple(true)51 .empty_values(false),52 )53 .arg(54 Arg::with_name("ignore-failure")55 .long("ignore-failure")56 .short("i")57 .help("Ignore non-zero exit codes"),58 )59 .arg(60 Arg::with_name("setup")61 .long("setup")62 .short("S")63 .takes_value(true)64 .value_name("CMD")65 .help(66 "Execute CMD before each benchmark run. This is useful for \67 clearing disk caches, for example.",68 ),69 )70 .arg(71 Arg::with_name("warmup")72 .long("warmup")73 .short("w")74 .takes_value(true)75 .value_name("NUM")56 .help("Perform NUM warmup runs before the actual benchmark"),76 .help(77 "Perform NUM warmup runs before the actual benchmark. This can be used \78 to fill (disk) caches for I/O-heavy programs.",79 ),80 )81 .arg(82 Arg::with_name("min-runs")83 .long("min-runs")84 .short("m")85 .takes_value(true)86 .value_name("NUM")64 .help("Perform at least NUM runs for each command"),87 .help(&format!(88 "Perform at least NUM runs for each command (default: {}).",89 options.min_runs90 )),91 )66 .arg(67 Arg::with_name("ignore-failure")68 .long("ignore-failure")69 .short("i")70 .help("Ignore non-zero exit codes"),71 )92 .get_matches();9374 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();94 options.ignore_failure = matches.is_present("ignore-failure");9576 // Process command line options77 let mut options = HyperfineOptions::default();96 options.setup_command = matches.value_of("setup").map(String::from);9798 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();99100 options.warmup_count = matches101 .value_of("warmup")102 .and_then(&str_to_u64)103 .unwrap_or(0);
2 unmodified lines
106 // we need at least two runs to compute a variance107 options.min_runs = cmp::max(2, min_runs);108 }10989 options.ignore_failure = matches.is_present("ignore-failure");90110 let commands = matches.values_of("command").unwrap().collect();111 let res = run(&commands, &options);112113 match res {
4 unmodified lines