Add argument to take parameter range step size
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Piyush Rungta · committed by David Peter · parent 3f68aef · Browse files · 3 files·+28 added−5 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+28 added−5 removed
- other:+28 added−5 removed
Provenance
- Commit
3e3f4d9- Tree
484a3ec- Author
Piyush Rungta <piyushrungta25@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
3f68aef
3 files changed+28 added−5 removed
modified:src/hyperfine/app.rs+11 added
106 unmodified lines107 string '{VAR}' in each command by the current parameter value.",108 ),109 )110 .arg(111+ Arg::with_name("parameter-step-size")112+ .long("parameter-step-size")113+ .takes_value(true)114+ .value_names(&["STEP_SIZE"])115+ .requires("parameter-scan")116+ .help(117+ "This argument requires --parameter-scan to be sepcified as well.\118+ Traverse the range MIN..MAX in steps of STEP_SIZE.",119+ ),120+ )121+ .arg(122 Arg::with_name("style")123 .long("style")124 .short("s")125 .takes_value(true)76 unmodified linesmodified:src/hyperfine/error.rs+2 added
5 unmodified lines6 pub enum ParameterScanError {7 ParseIntError(num::ParseIntError),8 EmptyRange,9 TooLarge,10+ ZeroStep,11 }12 13 impl From<num::ParseIntError> for ParameterScanError {14 fn from(e: num::ParseIntError) -> ParameterScanError {12 unmodified lines27 match *self {28 ParameterScanError::ParseIntError(ref e) => e.description(),29 ParameterScanError::EmptyRange => "Empty parameter range",30 ParameterScanError::TooLarge => "Parameter range is too large",31+ ParameterScanError::ZeroStep => "Zero is not a valid parameter step",32 }33 }34 }35 20 unmodified linesmodified:src/main.rs+15 added-5 removed
1 use std::cmp;2 use std::env;3 use std::error::Error;4 use std::io;5+use std::iter::StepBy;6 use std::ops::Range;7 8 use atty::Stream;9 use clap::ArgMatches;36 unmodified lines46 }47 48 /// A function to read the `--parameter-scan` arguments49 fn parse_parameter_scan_args<'a>(49- mut vals: clap::Values<'a>,50-) -> Result<(&'a str, Range<i32>), ParameterScanError> {50+ mut vals: clap::Values<'a>, step_size: &str51+) -> Result<(&'a str, StepBy<Range<i32>>), ParameterScanError> {52 let param_name = vals.next().unwrap();53 let param_min: i32 = vals.next().unwrap().parse()?;54 let param_max: i32 = vals.next().unwrap().parse()?;55+ let step_size = step_size.parse()?;56 57 const MAX_PARAMETERS: i32 = 100_000;58 if param_max - param_min > MAX_PARAMETERS {59 return Err(ParameterScanError::TooLarge);2 unmodified lines62 if param_max < param_min {63 return Err(ParameterScanError::EmptyRange);64 }65 66+ if step_size == 0 {67+ // Iterator::step_by panics for zero step68+ return Err(ParameterScanError::ZeroStep);69+ }70+71+ let param_range = (param_min..(param_max + 1)).step_by(step_size);72+64- Ok((param_name, param_min..(param_max + 1)))73+ Ok((param_name, param_range))74 }75 76 fn main() {77 let matches = get_arg_matches(env::args_os());136 unmodified lines214 fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {215 let command_strings = matches.values_of("command").unwrap();216 217 if let Some(args) = matches.values_of("parameter-scan") {218+ let step_size = matches.value_of("parameter-step-size").unwrap_or("1");209- match parse_parameter_scan_args(args) {219+ match parse_parameter_scan_args(args, step_size) {220 Ok((param_name, param_range)) => {221 let mut commands = vec![];222 let command_strings = command_strings.collect::<Vec<&str>>();213- for value in param_range.start..param_range.end {223+ for value in param_range {224 for cmd in &command_strings {225 commands.push(Command::new_parametrized(cmd, param_name, value));226 }227 }8 unmodified lines