Skip to content
Myr

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

5 files+108 added−26 removed

modified:src/hyperfine/app.rs+16 added
53 unmodified lines
54 .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 lines
modified:src/hyperfine/benchmark.rs+6 added-6 removed
1use std::cmp;2use std::io;3use std::process::Stdio;45use colored::*;
181 unmodified lines
187 }188189 // 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 lines
215 let runs_in_min_time = (options.min_time_sec216 / (res.time_real + prepare_res.time_real + shell_spawning_time.time_real))217 as u64;218218 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 );223224 let count_remaining = count - 1;225226 // Save the first result
115 unmodified lines
modified:src/hyperfine/error.rs+25 added-10 removed
7 unmodified lines
8 EmptyRange,9 TooLarge,10}1112impl From<num::ParseIntError> for ParameterScanError {13 fn from(e: num::ParseIntError) -> ParameterScanError {14 ParameterScanError::ParseIntError(e)15 }16}1718impl fmt::Display for ParameterScanError {19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {20 write!(f, "{}", self.description())21 }22}2312impl ParameterScanError {13 fn __description(&self) -> &str {24impl 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}3322impl From<num::ParseIntError> for ParameterScanError {23 fn from(e: num::ParseIntError) -> ParameterScanError {24 ParameterScanError::ParseIntError(e)25 }34#[derive(Debug)]35pub enum OptionsError {36 RunsBelowTwo,37 EmptyRunsRange,38}3928impl fmt::Display for ParameterScanError {40impl 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}4534impl Error for ParameterScanError {46impl 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 lines
72 /// Keep elements such as progress bar, but use no coloring73 NoColor,74}7576/// Number of runs for a benchmark77pub struct Runs {78 /// Minimum number of benchmark runs79 pub min: u64,8081 /// Maximum number of benchmark runs82 pub max: u64,83}8485impl Default for Runs {86 fn default() -> Runs {87 Runs {88 min: 10,89 max: ::std::u64::MAX,90 }91 }92}9394/// A set of options for hyperfine95pub struct HyperfineOptions {96 /// Number of warmup runs97 pub warmup_count: u64,9881 /// Minimum number of benchmark runs82 pub min_runs: u64,99 /// Number of benchmark runs100 pub runs: Runs,101102 /// Minimum benchmarking time103 pub min_time_sec: Second,104
13 unmodified lines
118impl 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 lines
modified:src/main.rs+40 added-7 removed
40 unmodified lines
41mod hyperfine;4243use hyperfine::app::get_arg_matches;44use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};45use hyperfine::error::ParameterScanError;45use hyperfine::error::{ParameterScanError, OptionsError};46use hyperfine::export::{ExportManager, ExportType};47use hyperfine::internal::write_benchmark_comparison;48use hyperfine::types::{49 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
44 unmodified lines
94 let options = build_hyperfine_options(&matches);95 let export_manager = build_export_manager(&matches);96 let commands = build_commands(&matches);9798 let res = run(&commands, &options);98 let res = match options {99 Ok(opts) => run(&commands, &opts),100 Err(e) => error(e.description()),101 };102103 match res {104 Ok(timing_results) => {105 write_benchmark_comparison(&timing_results);
9 unmodified lines
115 }116}117118/// Build the HyperfineOptions that correspond to the given ArgMatches116fn build_hyperfine_options(matches: &ArgMatches) -> HyperfineOptions {119fn 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();122123 options.warmup_count = matches124 .value_of("warmup")125 .and_then(&str_to_u64)126 .unwrap_or(0);127125 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);130131 if let Some(runs) = matches.value_of("runs").and_then(&str_to_u64) {132 min_runs = Some(runs);133 max_runs = Some(runs);134 }135136 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 };162163 options.preparation_command = matches.value_of("prepare").map(String::from);164165 options.show_output = matches.is_present("show-output");166
20 unmodified lines
187 if matches.is_present("ignore-failure") {188 options.failure_action = CmdFailureAction::Ignore;189 }190158 options191 Ok(options)192}193194/// Build the ExportManager that will export the results specified195/// in the given ArgMatches
37 unmodified lines