Use 'enum' to represent numeric/textual parameter values

This commit is contained in:
sharkdp authored and David Peter committed 2019-10-20 13:45:41 +02:00
1 parent 4a5d08ddc2
commit d7dc232e2f
4 files changed
+35 -15

No files matched your search

+5 -8
View File
@@ -146,7 +146,7 @@ pub fn mean_shell_spawning_time(
}
progress_bar.as_ref().map(|bar| bar.finish_and_clear());
Ok(TimingResult {
time_real: mean(&times_real),
time_user: mean(&times_user),
@@ -225,7 +225,7 @@ pub fn run_benchmark(
&values[num]
};
match cmd.get_parameter() {
Some((param, value)) => Command::new_parametrized(preparation_command, param, value.clone()),
Some((param, value)) => Command::new_parametrized(preparation_command, param, *value),
None => Command::new(preparation_command),
}
});
@@ -252,10 +252,8 @@ pub fn run_benchmark(
None,
)?;
progress_bar.as_ref().map(|bar| bar.inc(1));
}
progress_bar.as_ref().map(|bar| bar.finish_and_clear());
}
// Set up progress bar (and spinner for initial measurement)
@@ -308,7 +306,7 @@ pub fn run_benchmark(
// Re-configure the progress bar
progress_bar.as_ref().map(|bar| bar.set_length(count));
progress_bar.as_ref().map(|bar| bar.inc(1));
// Gather statistics
for _ in 0..count_remaining {
run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;
@@ -339,7 +337,6 @@ pub fn run_benchmark(
progress_bar.as_ref().map(|bar| bar.finish_and_clear());
// Compute statistical quantities
let t_num = times_real.len();
let t_mean = mean(&times_real);
@@ -421,7 +418,7 @@ pub fn run_benchmark(
.cleanup_command
.as_ref()
.map(|cleanup_command| match cmd.get_parameter() {
Some((param, value)) => Command::new_parametrized(cleanup_command, param, value.clone()),
Some((param, value)) => Command::new_parametrized(cleanup_command, param, *value),
None => Command::new(cleanup_command),
});
run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;
@@ -436,6 +433,6 @@ pub fn run_benchmark(
t_min,
t_max,
times_real,
cmd.get_parameter().as_ref().map(|p| p.1.clone()),
cmd.get_parameter().map(|p| p.1.to_string()),
))
}
+6 -2
View File
@@ -1,5 +1,5 @@
use crate::hyperfine::error::ParameterScanError;
use crate::hyperfine::types::{Command, NumericType};
use crate::hyperfine::types::{Command, NumericType, ParameterValue};
use clap::Values;
use rust_decimal::Decimal;
use std::ops::{Add, AddAssign, Div, Sub};
@@ -92,7 +92,11 @@ fn build_parameterized_commands<'a, T: Numeric>(
for value in param_range {
for cmd in &command_strings {
commands.push(Command::new_parametrized(cmd, param_name, value.into().to_string()));
commands.push(Command::new_parametrized(
cmd,
param_name,
ParameterValue::Numeric(value.into()),
));
}
}
Ok(commands)
+18 -3
View File
@@ -39,6 +39,21 @@ impl Into<NumericType> for Decimal {
}
}
#[derive(Debug, Clone, Copy)]
pub enum ParameterValue<'a> {
Text(&'a str),
Numeric(NumericType),
}
impl<'a> ToString for ParameterValue<'a> {
fn to_string(&self) -> String {
match self {
ParameterValue::Text(value) => value.to_string(),
ParameterValue::Numeric(value) => value.to_string(),
}
}
}
/// A command that should be benchmarked.
#[derive(Debug, Clone)]
pub struct Command<'a> {
@@ -46,7 +61,7 @@ pub struct Command<'a> {
expression: &'a str,
/// A possible parameter value.
parameter: Option<(&'a str, String)>,
parameter: Option<(&'a str, ParameterValue<'a>)>,
}
impl<'a> Command<'a> {
@@ -60,7 +75,7 @@ impl<'a> Command<'a> {
pub fn new_parametrized(
expression: &'a str,
parameter: &'a str,
value: String,
value: ParameterValue<'a>,
) -> Command<'a> {
Command {
expression,
@@ -78,7 +93,7 @@ impl<'a> Command<'a> {
}
}
pub fn get_parameter(&self) -> &Option<(&'a str, String)> {
pub fn get_parameter(&self) -> &Option<(&'a str, ParameterValue<'a>)> {
&self.parameter
}
}
+6 -2
View File
@@ -16,7 +16,7 @@ use crate::hyperfine::export::{ExportManager, ExportType};
use crate::hyperfine::internal::write_benchmark_comparison;
use crate::hyperfine::parameter_range::get_parameterized_commands;
use crate::hyperfine::types::{
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,
};
use crate::hyperfine::units::Unit;
@@ -219,7 +219,11 @@ fn build_commands<'a>(matches: &'a ArgMatches<'_>) -> Vec<Command<'a>> {
for value in param_list {
for cmd in &command_list {
commands.push(Command::new_parametrized(cmd, param_name, value.to_string()));
commands.push(Command::new_parametrized(
cmd,
param_name,
ParameterValue::Text(value),
));
}
}