Added options to specify the max/exact numbers of runs.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #77.
authored by Diogo Sousa · committed by David Peter · parent fe041df · Browse files · 5 files·+108 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+108 added−26 removed
- other:+108 added−26 removed
Provenance
- Commit
d78c33b- Tree
0acd621- Author
Diogo Sousa <diogogsousa@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
fe041df
5 files changed+108 added−26 removed
modified:src/hyperfine/app.rs+16 added
53 unmodified lines54 .value_name("NUM")55 .help("Perform at least NUM runs for each command (default: 10)."),56 )57 .arg(58+ Arg::with_name("max-runs")59+ .long("max-runs")60+ .short("M")61+ .takes_value(true)62+ .value_name("NUM")63+ .help("Perform at most NUM runs for each command."),64+ )65+ .arg(66+ Arg::with_name("runs")67+ .long("runs")68+ .short("r")69+ .takes_value(true)70+ .value_name("NUM")71+ .help("Perform exactly NUM runs for each command."),72+ )73+ .arg(74 Arg::with_name("prepare")75 .long("prepare")76 .short("p")77 .takes_value(true)73 unmodified linesmodified:src/hyperfine/benchmark.rs+6 added-6 removed
1+use std::cmp;2 use std::io;3 use std::process::Stdio;4 5 use colored::*;181 unmodified lines187 }188 189 // Set up progress bar (and spinner for initial measurement)190 let progress_bar = get_progress_bar(190- options.min_runs,191+ options.runs.min,192 "Initial time measurement",193 &options.output_style,194 );195 19 unmodified lines215 let runs_in_min_time = (options.min_time_sec216 / (res.time_real + prepare_res.time_real + shell_spawning_time.time_real))217 as u64;218 218- let count = if runs_in_min_time >= options.min_runs {219- runs_in_min_time220- } else {221- options.min_runs222- };219+ let count = cmp::min(220+ cmp::max(runs_in_min_time, options.runs.min),221+ options.runs.max222+ );223 224 let count_remaining = count - 1;225 226 // Save the first result115 unmodified linesmodified:src/hyperfine/error.rs+25 added-10 removed
7 unmodified lines8 EmptyRange,9 TooLarge,10 }11 12+impl From<num::ParseIntError> for ParameterScanError {13+ fn from(e: num::ParseIntError) -> ParameterScanError {14+ ParameterScanError::ParseIntError(e)15+ }16+}17+18+impl fmt::Display for ParameterScanError {19+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {20+ write!(f, "{}", self.description())21+ }22+}23+12-impl ParameterScanError {13- fn __description(&self) -> &str {24+impl Error for ParameterScanError {25+ fn description(&self) -> &str {26 match *self {27 ParameterScanError::ParseIntError(ref e) => e.description(),28 ParameterScanError::EmptyRange => "Empty parameter range",29 ParameterScanError::TooLarge => "Parameter range is too large",30 }31 }32 }33 22-impl From<num::ParseIntError> for ParameterScanError {23- fn from(e: num::ParseIntError) -> ParameterScanError {24- ParameterScanError::ParseIntError(e)25- }34+#[derive(Debug)]35+pub enum OptionsError {36+ RunsBelowTwo,37+ EmptyRunsRange,38 }39 28-impl fmt::Display for ParameterScanError {40+impl fmt::Display for OptionsError {41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {30- write!(f, "{}", self.__description())42+ write!(f, "{}", self.description())43 }44 }45 34-impl Error for ParameterScanError {46+impl Error for OptionsError {47 fn description(&self) -> &str {36- self.__description()48+ match *self {49+ OptionsError::EmptyRunsRange => "Empty runs range",50+ OptionsError::RunsBelowTwo => "Number of runs below two",51+ }52 }53 }modified:src/hyperfine/types.rs+21 added-3 removed
71 unmodified lines72 /// Keep elements such as progress bar, but use no coloring73 NoColor,74 }75 76+/// Number of runs for a benchmark77+pub struct Runs {78+ /// Minimum number of benchmark runs79+ pub min: u64,80+81+ /// Maximum number of benchmark runs82+ pub max: u64,83+}84+85+impl Default for Runs {86+ fn default() -> Runs {87+ Runs {88+ min: 10,89+ max: ::std::u64::MAX,90+ }91+ }92+}93+94 /// A set of options for hyperfine95 pub struct HyperfineOptions {96 /// Number of warmup runs97 pub warmup_count: u64,98 81- /// Minimum number of benchmark runs82- pub min_runs: u64,99+ /// Number of benchmark runs100+ pub runs: Runs,101 102 /// Minimum benchmarking time103 pub min_time_sec: Second,104 13 unmodified lines118 impl Default for HyperfineOptions {119 fn default() -> HyperfineOptions {120 HyperfineOptions {121 warmup_count: 0,104- min_runs: 10,122+ runs: Runs::default(),123 min_time_sec: 3.0,124 failure_action: CmdFailureAction::RaiseError,125 preparation_command: None,126 output_style: OutputStyleOption::Full,58 unmodified linesmodified:src/main.rs+40 added-7 removed
40 unmodified lines41 mod hyperfine;42 43 use hyperfine::app::get_arg_matches;44 use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};45-use hyperfine::error::ParameterScanError;45+use hyperfine::error::{ParameterScanError, OptionsError};46 use hyperfine::export::{ExportManager, ExportType};47 use hyperfine::internal::write_benchmark_comparison;48 use hyperfine::types::{49 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,44 unmodified lines94 let options = build_hyperfine_options(&matches);95 let export_manager = build_export_manager(&matches);96 let commands = build_commands(&matches);97 98- let res = run(&commands, &options);98+ let res = match options {99+ Ok(opts) => run(&commands, &opts),100+ Err(e) => error(e.description()),101+ };102 103 match res {104 Ok(timing_results) => {105 write_benchmark_comparison(&timing_results);9 unmodified lines115 }116 }117 118 /// Build the HyperfineOptions that correspond to the given ArgMatches116-fn build_hyperfine_options(matches: &ArgMatches) -> HyperfineOptions {119+fn build_hyperfine_options(matches: &ArgMatches) -> Result<HyperfineOptions, OptionsError> {120 let mut options = HyperfineOptions::default();121 let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();122 123 options.warmup_count = matches124 .value_of("warmup")125 .and_then(&str_to_u64)126 .unwrap_or(0);127 125- if let Some(min_runs) = matches.value_of("min-runs").and_then(&str_to_u64) {126- // we need at least two runs to compute a variance127- options.min_runs = cmp::max(2, min_runs);128+ let mut min_runs = matches.value_of("min-runs").and_then(&str_to_u64);129+ let mut max_runs = matches.value_of("max-runs").and_then(&str_to_u64);130+131+ if let Some(runs) = matches.value_of("runs").and_then(&str_to_u64) {132+ min_runs = Some(runs);133+ max_runs = Some(runs);134 }135 136+ match (min_runs, max_runs) {137+ (Some(min), _) if min < 2 => {138+ // We need at least two runs to compute a variance.139+ return Err(OptionsError::RunsBelowTwo);140+ }141+ (Some(min), None) => {142+ options.runs.min = min;143+ }144+ (_, Some(max)) if max < 2 => {145+ // We need at least two runs to compute a variance.146+ return Err(OptionsError::RunsBelowTwo);147+ }148+ (None, Some(max)) => {149+ // Since the minimum was not explicit we lower it if max is below the default min.150+ options.runs.min = cmp::min(options.runs.min, max);151+ options.runs.max = max;152+ }153+ (Some(min), Some(max)) if min > max => {154+ return Err(OptionsError::EmptyRunsRange);155+ }156+ (Some(min), Some(max)) => {157+ options.runs.min = min;158+ options.runs.max = max;159+ }160+ (None, None) => {}161+ };162+163 options.preparation_command = matches.value_of("prepare").map(String::from);164 165 options.show_output = matches.is_present("show-output");166 20 unmodified lines187 if matches.is_present("ignore-failure") {188 options.failure_action = CmdFailureAction::Ignore;189 }190 158- options191+ Ok(options)192 }193 194 /// Build the ExportManager that will export the results specified195 /// in the given ArgMatches37 unmodified lines