Show unused parameters in command names
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #600
authored by David Peter · committed by David Peter · parent 4b9cd06 · Browse files · 11 files·+85 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+85 added−10 removed
- tests:+16 added−0 removed
- other:+69 added−10 removed
Provenance
- Commit
a875276- Tree
9483fe4- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
4b9cd06
11 files changed+85 added−10 removed
modified:src/benchmark/benchmark_result.rs+5 added
10 unmodified lines11 pub struct BenchmarkResult {12 /// The full command line of the program that is being benchmarked13 pub command: String,14 15+ /// The full command line of the program that is being benchmarked, possibly including a list of16+ /// parameters that were not used in the command line template.17+ #[serde(skip_serializing)]18+ pub command_with_unused_parameters: String,19+20 /// The average run time21 pub mean: Second,22 23 /// The standard deviation of all run times. Not available if only one run has been performed28 unmodified linesmodified:src/benchmark/mod.rs+3 added-3 removed
110 unmodified lines111 }112 113 /// Run the benchmark for a single command114 pub fn run(&self) -> Result<BenchmarkResult> {115- let command_name = self.command.get_name();115 if self.options.output_style != OutputStyleOption::Disabled {116 println!(117 "{}{}: {}",118 "Benchmark ".bold(),119 (self.number + 1).to_string().bold(),121- command_name,120+ self.command.get_name_with_unused_parameters(),121 );122 }123 124 let mut times_real: Vec<Second> = vec![];242 unmodified lines367 368 self.run_cleanup_command(self.command.get_parameters().iter().cloned())?;369 370 Ok(BenchmarkResult {372- command: command_name,371+ command: self.command.get_name(),372+ command_with_unused_parameters: self.command.get_name_with_unused_parameters(),373 mean: t_mean,374 stddev: t_stddev,375 median: t_median,376 user: user_mean,14 unmodified linesmodified:src/benchmark/relative_speed.rs+1 added
87 unmodified lines88 use std::collections::BTreeMap;89 90 BenchmarkResult {91 command: name.into(),92+ command_with_unused_parameters: name.into(),93 mean,94 stddev: Some(1.0),95 median: mean,96 user: mean,34 unmodified linesmodified:src/benchmark/scheduler.rs+6 added-3 removed
66 unmodified lines67 let fastest = &annotated_results[0];68 let others = &annotated_results[1..];69 70 println!("{}", "Summary".bold());71- println!(" '{}' ran", fastest.result.command.cyan());71+ println!(72+ " {} ran",73+ fastest.result.command_with_unused_parameters.cyan()74+ );75 76 for item in others {77 println!(75- "{}{} times faster than '{}'",78+ "{}{} times faster than {}",79 format!("{:8.2}", item.relative_speed).bold().green(),80 if let Some(stddev) = item.relative_speed_stddev {81 format!(" ± {}", format!("{:.2}", stddev).green())82 } else {83 "".into()84 },82- &item.result.command.magenta()85+ &item.result.command_with_unused_parameters.magenta()86 );87 }88 } else {89 eprintln!(15 unmodified linesmodified:src/export/asciidoc.rs+4 added
92 unmodified lines93 94 let results = vec![95 BenchmarkResult {96 command: String::from("FOO=1 BAR=2 command | 1"),97+ command_with_unused_parameters: String::from("FOO=1 BAR=2 command | 1"),98 mean: 1.0,99 stddev: Some(2.0),100 median: 1.0,101 user: 3.0,10 unmodified lines112 },113 },114 BenchmarkResult {115 command: String::from("FOO=1 BAR=7 command | 2"),116+ command_with_unused_parameters: String::from("FOO=1 BAR=7 command | 2"),117 mean: 11.0,118 stddev: Some(12.0),119 median: 11.0,120 user: 13.0,43 unmodified lines164 165 let results = vec![166 BenchmarkResult {167 command: String::from("FOO=1 BAR=7 command | 2"),168+ command_with_unused_parameters: String::from("FOO=1 BAR=7 command | 2"),169 mean: 0.011,170 stddev: Some(0.012),171 median: 0.011,172 user: 0.013,10 unmodified lines183 },184 },185 BenchmarkResult {186 command: String::from("FOO=1 BAR=2 command | 1"),187+ command_with_unused_parameters: String::from("FOO=1 BAR=2 command | 1"),188 mean: 1.0,189 stddev: Some(2.0),190 median: 1.0,191 user: 3.0,40 unmodified linesmodified:src/export/csv.rs+2 added
60 unmodified lines61 62 let results = vec![63 BenchmarkResult {64 command: String::from("FOO=one BAR=two command | 1"),65+ command_with_unused_parameters: String::from("FOO=one BAR=two command | 1"),66 mean: 1.0,67 stddev: Some(2.0),68 median: 1.0,69 user: 3.0,10 unmodified lines80 },81 },82 BenchmarkResult {83 command: String::from("FOO=one BAR=seven command | 2"),84+ command_with_unused_parameters: String::from("FOO=one BAR=seven command | 2"),85 mean: 11.0,86 stddev: Some(12.0),87 median: 11.0,88 user: 13.0,24 unmodified linesmodified:src/export/markdown.rs+8 added
69 unmodified lines70 71 let timing_results = vec![72 BenchmarkResult {73 command: String::from("sleep 0.1"),74+ command_with_unused_parameters: String::from("sleep 0.1"),75 mean: 0.1057,76 stddev: Some(0.0016),77 median: 0.1057,78 user: 0.0009,5 unmodified lines84 parameters: BTreeMap::new(),85 },86 BenchmarkResult {87 command: String::from("sleep 2"),88+ command_with_unused_parameters: String::from("sleep 2"),89 mean: 2.0050,90 stddev: Some(0.0020),91 median: 2.0050,92 user: 0.0009,29 unmodified lines122 123 let timing_results = vec![124 BenchmarkResult {125 command: String::from("sleep 2"),126+ command_with_unused_parameters: String::from("sleep 2"),127 mean: 2.0050,128 stddev: Some(0.0020),129 median: 2.0050,130 user: 0.0009,5 unmodified lines136 parameters: BTreeMap::new(),137 },138 BenchmarkResult {139 command: String::from("sleep 0.1"),140+ command_with_unused_parameters: String::from("sleep 0.1"),141 mean: 0.1057,142 stddev: Some(0.0016),143 median: 0.1057,144 user: 0.0009,29 unmodified lines174 175 let timing_results = vec![176 BenchmarkResult {177 command: String::from("sleep 0.1"),178+ command_with_unused_parameters: String::from("sleep 0.1"),179 mean: 0.1057,180 stddev: Some(0.0016),181 median: 0.1057,182 user: 0.0009,5 unmodified lines188 parameters: BTreeMap::new(),189 },190 BenchmarkResult {191 command: String::from("sleep 2"),192+ command_with_unused_parameters: String::from("sleep 2"),193 mean: 2.0050,194 stddev: Some(0.0020),195 median: 2.0050,196 user: 0.0009,35 unmodified lines232 233 let timing_results = vec![234 BenchmarkResult {235 command: String::from("sleep 2"),236+ command_with_unused_parameters: String::from("sleep 2"),237 mean: 2.0050,238 stddev: Some(0.0020),239 median: 2.0050,240 user: 0.0009,5 unmodified lines246 parameters: BTreeMap::new(),247 },248 BenchmarkResult {249 command: String::from("sleep 0.1"),250+ command_with_unused_parameters: String::from("sleep 0.1"),251 mean: 0.1057,252 stddev: Some(0.0016),253 median: 0.1057,254 user: 0.0009,25 unmodified linesmodified:src/export/markup.rs+11 added-1 removed
41 unmodified lines42 43 for entry in entries {44 let measurement = &entry.result;45 // prepare data row strings46- let cmd_str = measurement.command.replace('|', "\\|");46+ let cmd_str = measurement47+ .command_with_unused_parameters48+ .replace('|', "\\|");49 let mean_str = format_duration_value(measurement.mean, Some(unit)).0;50 let stddev_str = if let Some(stddev) = measurement.stddev {51 format!(" ± {}", format_duration_value(stddev, Some(unit)).0)52 } else {67 unmodified lines120 use std::collections::BTreeMap;121 let results = vec![122 BenchmarkResult {123 command: String::from("sleep 2"),124+ command_with_unused_parameters: String::from("sleep 2"),125 mean: 2.0050,126 stddev: Some(0.0020),127 median: 2.0050,128 user: 0.0009,5 unmodified lines134 parameters: BTreeMap::new(),135 },136 BenchmarkResult {137 command: String::from("sleep 0.1"),138+ command_with_unused_parameters: String::from("sleep 0.1"),139 mean: 0.1057,140 stddev: Some(0.0016),141 median: 0.1057,142 user: 0.0009,19 unmodified lines162 use std::collections::BTreeMap;163 let results = vec![164 BenchmarkResult {165 command: String::from("sleep 2"),166+ command_with_unused_parameters: String::from("sleep 2"),167 mean: 2.0050,168 stddev: Some(0.0020),169 median: 2.0050,170 user: 0.0009,5 unmodified lines176 parameters: BTreeMap::new(),177 },178 BenchmarkResult {179 command: String::from("sleep 0.1"),180+ command_with_unused_parameters: String::from("sleep 0.1"),181 mean: 0.1057,182 stddev: Some(0.0016),183 median: 0.1057,184 user: 0.0009,19 unmodified lines204 use std::collections::BTreeMap;205 let results = vec![206 BenchmarkResult {207 command: String::from("sleep 2"),208+ command_with_unused_parameters: String::from("sleep 2"),209 mean: 2.0050,210 stddev: Some(0.0020),211 median: 2.0050,212 user: 0.0009,5 unmodified lines218 parameters: BTreeMap::new(),219 },220 BenchmarkResult {221 command: String::from("sleep 0.1"),222+ command_with_unused_parameters: String::from("sleep 0.1"),223 mean: 0.1057,224 stddev: Some(0.0016),225 median: 0.1057,226 user: 0.0009,19 unmodified lines246 use std::collections::BTreeMap;247 let results = vec![248 BenchmarkResult {249 command: String::from("sleep 0.1"),250+ command_with_unused_parameters: String::from("sleep 0.1"),251 mean: 0.1057,252 stddev: Some(0.0016),253 median: 0.1057,254 user: 0.0009,5 unmodified lines260 parameters: BTreeMap::new(),261 },262 BenchmarkResult {263 command: String::from("sleep 2"),264+ command_with_unused_parameters: String::from("sleep 2"),265 mean: 2.0050,266 stddev: Some(0.0020),267 median: 2.0050,268 user: 0.0009,27 unmodified linesmodified:src/export/orgmode.rs+4 added
75 unmodified lines76 77 let results = vec![78 BenchmarkResult {79 command: String::from("sleep 0.1"),80+ command_with_unused_parameters: String::from("sleep 0.1"),81 mean: 0.1057,82 stddev: Some(0.0016),83 median: 0.1057,84 user: 0.0009,5 unmodified lines90 parameters: BTreeMap::new(),91 },92 BenchmarkResult {93 command: String::from("sleep 2"),94+ command_with_unused_parameters: String::from("sleep 2"),95 mean: 2.0050,96 stddev: Some(0.0020),97 median: 2.0050,98 user: 0.0009,31 unmodified lines130 131 let results = vec![132 BenchmarkResult {133 command: String::from("sleep 2"),134+ command_with_unused_parameters: String::from("sleep 2"),135 mean: 2.0050,136 stddev: Some(0.0020),137 median: 2.0050,138 user: 0.0009,5 unmodified lines144 parameters: BTreeMap::new(),145 },146 BenchmarkResult {147 command: String::from("sleep 0.1"),148+ command_with_unused_parameters: String::from("sleep 0.1"),149 mean: 0.1057,150 stddev: Some(0.0016),151 median: 0.1057,152 user: 0.0009,21 unmodified linesmodified:src/command.rs+25 added-3 removed
1 use std::collections::BTreeMap;2 use std::fmt;3 use std::str::FromStr;4 5+use crate::parameter::tokenize::tokenize;6+use crate::parameter::ParameterValue;7 use crate::{8 error::{OptionsError, ParameterScanError},9 parameter::{10 range_step::{Numeric, RangeStep},2 unmodified lines13 };14 15 use clap::{parser::ValuesRef, ArgMatches};16 15-use crate::parameter::tokenize::tokenize;16-use crate::parameter::ParameterValue;17-17 use anyhow::{bail, Context, Result};18 use rust_decimal::Decimal;19 20 /// A command that should be benchmarked.36 unmodified lines57 |name| self.replace_parameters_in(name),58 )59 }60 61+ pub fn get_name_with_unused_parameters(&self) -> String {62+ let parameters = self63+ .get_unused_parameters()64+ .map(|(parameter, value)| {65+ format!("{} = {}, ", parameter.to_string(), value.to_string())66+ })67+ .collect::<String>();68+ let parameters = parameters.trim_end_matches(", ");69+ let parameters = if parameters.is_empty() {70+ "".into()71+ } else {72+ format!(" ({})", parameters)73+ };74+75+ format!("{}{}", self.get_name(), parameters)76+ }77+78 pub fn get_command_line(&self) -> String {79 self.replace_parameters_in(self.expression)80 }81 15 unmodified lines97 pub fn get_parameters(&self) -> &[(&'a str, ParameterValue)] {98 &self.parameters99 }100 101+ pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {102+ self.parameters103+ .iter()104+ .filter(move |(parameter, _)| !self.expression.contains(&format!("{{{}}}", parameter)))105+ }106+107 fn replace_parameters_in(&self, original: &str) -> String {108 let mut result = String::new();109 let mut replacements = BTreeMap::<String, String>::new();110 for (param_name, param_value) in &self.parameters {457 unmodified linesmodified:tests/integration_tests.rs+16 added
380 unmodified lines381 382 let contents = std::fs::read_to_string(export_path).unwrap();383 assert!(contents.contains("true"));384 }385+386+#[test]387+fn unused_parameters_are_shown_in_benchmark_name() {388+ hyperfine()389+ .arg("--runs=2")390+ .arg("--parameter-list")391+ .arg("branch")392+ .arg("master,feature")393+ .arg("echo test")394+ .assert()395+ .success()396+ .stdout(397+ predicate::str::contains("echo test (branch = master)")398+ .and(predicate::str::contains("echo test (branch = feature)")),399+ );400+}