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()); progress_bar.as_ref().map(|bar| bar.finish_and_clear());
Ok(TimingResult { Ok(TimingResult {
time_real: mean(&times_real), time_real: mean(&times_real),
time_user: mean(&times_user), time_user: mean(&times_user),
@@ -225,7 +225,7 @@ pub fn run_benchmark(
&values[num] &values[num]
}; };
match cmd.get_parameter() { 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), None => Command::new(preparation_command),
} }
}); });
@@ -252,10 +252,8 @@ pub fn run_benchmark(
None, None,
)?; )?;
progress_bar.as_ref().map(|bar| bar.inc(1)); progress_bar.as_ref().map(|bar| bar.inc(1));
} }
progress_bar.as_ref().map(|bar| bar.finish_and_clear()); progress_bar.as_ref().map(|bar| bar.finish_and_clear());
} }
// Set up progress bar (and spinner for initial measurement) // Set up progress bar (and spinner for initial measurement)
@@ -308,7 +306,7 @@ pub fn run_benchmark(
// Re-configure the progress bar // Re-configure the progress bar
progress_bar.as_ref().map(|bar| bar.set_length(count)); progress_bar.as_ref().map(|bar| bar.set_length(count));
progress_bar.as_ref().map(|bar| bar.inc(1)); progress_bar.as_ref().map(|bar| bar.inc(1));
// Gather statistics // Gather statistics
for _ in 0..count_remaining { for _ in 0..count_remaining {
run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?; 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()); progress_bar.as_ref().map(|bar| bar.finish_and_clear());
// Compute statistical quantities // Compute statistical quantities
let t_num = times_real.len(); let t_num = times_real.len();
let t_mean = mean(&times_real); let t_mean = mean(&times_real);
@@ -421,7 +418,7 @@ pub fn run_benchmark(
.cleanup_command .cleanup_command
.as_ref() .as_ref()
.map(|cleanup_command| match cmd.get_parameter() { .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), None => Command::new(cleanup_command),
}); });
run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?; run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;
@@ -436,6 +433,6 @@ pub fn run_benchmark(
t_min, t_min,
t_max, t_max,
times_real, 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::error::ParameterScanError;
use crate::hyperfine::types::{Command, NumericType}; use crate::hyperfine::types::{Command, NumericType, ParameterValue};
use clap::Values; use clap::Values;
use rust_decimal::Decimal; use rust_decimal::Decimal;
use std::ops::{Add, AddAssign, Div, Sub}; use std::ops::{Add, AddAssign, Div, Sub};
@@ -92,7 +92,11 @@ fn build_parameterized_commands<'a, T: Numeric>(
for value in param_range { for value in param_range {
for cmd in &command_strings { 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) 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. /// A command that should be benchmarked.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Command<'a> { pub struct Command<'a> {
@@ -46,7 +61,7 @@ pub struct Command<'a> {
expression: &'a str, expression: &'a str,
/// A possible parameter value. /// A possible parameter value.
parameter: Option<(&'a str, String)>, parameter: Option<(&'a str, ParameterValue<'a>)>,
} }
impl<'a> Command<'a> { impl<'a> Command<'a> {
@@ -60,7 +75,7 @@ impl<'a> Command<'a> {
pub fn new_parametrized( pub fn new_parametrized(
expression: &'a str, expression: &'a str,
parameter: &'a str, parameter: &'a str,
value: String, value: ParameterValue<'a>,
) -> Command<'a> { ) -> Command<'a> {
Command { Command {
expression, 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 &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::internal::write_benchmark_comparison;
use crate::hyperfine::parameter_range::get_parameterized_commands; use crate::hyperfine::parameter_range::get_parameterized_commands;
use crate::hyperfine::types::{ use crate::hyperfine::types::{
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,
}; };
use crate::hyperfine::units::Unit; 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 value in param_list {
for cmd in &command_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),
));
} }
} }