Skip to content

Commit

Permalink
Show unused parameters in command names
Browse files Browse the repository at this point in the history
closes #600
  • Loading branch information
sharkdp committed Apr 20, 2023
1 parent 4b9cd06 commit a875276
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/benchmark/benchmark_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub struct BenchmarkResult {
/// The full command line of the program that is being benchmarked
pub command: String,

/// The full command line of the program that is being benchmarked, possibly including a list of
/// parameters that were not used in the command line template.
#[serde(skip_serializing)]
pub command_with_unused_parameters: String,

/// The average run time
pub mean: Second,

Expand Down
6 changes: 3 additions & 3 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ impl<'a> Benchmark<'a> {

/// Run the benchmark for a single command
pub fn run(&self) -> Result<BenchmarkResult> {
let command_name = self.command.get_name();
if self.options.output_style != OutputStyleOption::Disabled {
println!(
"{}{}: {}",
"Benchmark ".bold(),
(self.number + 1).to_string().bold(),
command_name,
self.command.get_name_with_unused_parameters(),
);
}

Expand Down Expand Up @@ -369,7 +368,8 @@ impl<'a> Benchmark<'a> {
self.run_cleanup_command(self.command.get_parameters().iter().cloned())?;

Ok(BenchmarkResult {
command: command_name,
command: self.command.get_name(),
command_with_unused_parameters: self.command.get_name_with_unused_parameters(),
mean: t_mean,
stddev: t_stddev,
median: t_median,
Expand Down
1 change: 1 addition & 0 deletions src/benchmark/relative_speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {

BenchmarkResult {
command: name.into(),
command_with_unused_parameters: name.into(),
mean,
stddev: Some(1.0),
median: mean,
Expand Down
9 changes: 6 additions & 3 deletions src/benchmark/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ impl<'a> Scheduler<'a> {
let others = &annotated_results[1..];

println!("{}", "Summary".bold());
println!(" '{}' ran", fastest.result.command.cyan());
println!(
" {} ran",
fastest.result.command_with_unused_parameters.cyan()
);

for item in others {
println!(
"{}{} times faster than '{}'",
"{}{} times faster than {}",
format!("{:8.2}", item.relative_speed).bold().green(),
if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:.2}", stddev).green())
} else {
"".into()
},
&item.result.command.magenta()
&item.result.command_with_unused_parameters.magenta()
);
}
} else {
Expand Down
28 changes: 25 additions & 3 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;

use crate::parameter::tokenize::tokenize;
use crate::parameter::ParameterValue;
use crate::{
error::{OptionsError, ParameterScanError},
parameter::{
Expand All @@ -12,9 +14,6 @@ use crate::{

use clap::{parser::ValuesRef, ArgMatches};

use crate::parameter::tokenize::tokenize;
use crate::parameter::ParameterValue;

use anyhow::{bail, Context, Result};
use rust_decimal::Decimal;

Expand Down Expand Up @@ -59,6 +58,23 @@ impl<'a> Command<'a> {
)
}

pub fn get_name_with_unused_parameters(&self) -> String {
let parameters = self
.get_unused_parameters()
.map(|(parameter, value)| {
format!("{} = {}, ", parameter.to_string(), value.to_string())
})
.collect::<String>();
let parameters = parameters.trim_end_matches(", ");
let parameters = if parameters.is_empty() {
"".into()
} else {
format!(" ({})", parameters)
};

format!("{}{}", self.get_name(), parameters)
}

pub fn get_command_line(&self) -> String {
self.replace_parameters_in(self.expression)
}
Expand All @@ -82,6 +98,12 @@ impl<'a> Command<'a> {
&self.parameters
}

pub fn get_unused_parameters(&self) -> impl Iterator<Item = &(&'a str, ParameterValue)> {
self.parameters
.iter()
.filter(move |(parameter, _)| !self.expression.contains(&format!("{{{}}}", parameter)))
}

fn replace_parameters_in(&self, original: &str) -> String {
let mut result = String::new();
let mut replacements = BTreeMap::<String, String>::new();
Expand Down
4 changes: 4 additions & 0 deletions src/export/asciidoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn test_asciidoc_format_s() {
let results = vec![
BenchmarkResult {
command: String::from("FOO=1 BAR=2 command | 1"),
command_with_unused_parameters: String::from("FOO=1 BAR=2 command | 1"),
mean: 1.0,
stddev: Some(2.0),
median: 1.0,
Expand All @@ -112,6 +113,7 @@ fn test_asciidoc_format_s() {
},
BenchmarkResult {
command: String::from("FOO=1 BAR=7 command | 2"),
command_with_unused_parameters: String::from("FOO=1 BAR=7 command | 2"),
mean: 11.0,
stddev: Some(12.0),
median: 11.0,
Expand Down Expand Up @@ -163,6 +165,7 @@ fn test_asciidoc_format_ms() {
let results = vec![
BenchmarkResult {
command: String::from("FOO=1 BAR=7 command | 2"),
command_with_unused_parameters: String::from("FOO=1 BAR=7 command | 2"),
mean: 0.011,
stddev: Some(0.012),
median: 0.011,
Expand All @@ -181,6 +184,7 @@ fn test_asciidoc_format_ms() {
},
BenchmarkResult {
command: String::from("FOO=1 BAR=2 command | 1"),
command_with_unused_parameters: String::from("FOO=1 BAR=2 command | 1"),
mean: 1.0,
stddev: Some(2.0),
median: 1.0,
Expand Down
2 changes: 2 additions & 0 deletions src/export/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn test_csv() {
let results = vec![
BenchmarkResult {
command: String::from("FOO=one BAR=two command | 1"),
command_with_unused_parameters: String::from("FOO=one BAR=two command | 1"),
mean: 1.0,
stddev: Some(2.0),
median: 1.0,
Expand All @@ -80,6 +81,7 @@ fn test_csv() {
},
BenchmarkResult {
command: String::from("FOO=one BAR=seven command | 2"),
command_with_unused_parameters: String::from("FOO=one BAR=seven command | 2"),
mean: 11.0,
stddev: Some(12.0),
median: 11.0,
Expand Down
8 changes: 8 additions & 0 deletions src/export/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn test_markdown_format_ms() {
let timing_results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand All @@ -84,6 +85,7 @@ fn test_markdown_format_ms() {
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand Down Expand Up @@ -121,6 +123,7 @@ fn test_markdown_format_s() {
let timing_results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -134,6 +137,7 @@ fn test_markdown_format_s() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down Expand Up @@ -171,6 +175,7 @@ fn test_markdown_format_time_unit_s() {
let timing_results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand All @@ -184,6 +189,7 @@ fn test_markdown_format_time_unit_s() {
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand Down Expand Up @@ -227,6 +233,7 @@ fn test_markdown_format_time_unit_ms() {
let timing_results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -240,6 +247,7 @@ fn test_markdown_format_time_unit_ms() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down
12 changes: 11 additions & 1 deletion src/export/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub trait MarkupExporter {
for entry in entries {
let measurement = &entry.result;
// prepare data row strings
let cmd_str = measurement.command.replace('|', "\\|");
let cmd_str = measurement
.command_with_unused_parameters
.replace('|', "\\|");
let mean_str = format_duration_value(measurement.mean, Some(unit)).0;
let stddev_str = if let Some(stddev) = measurement.stddev {
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
Expand Down Expand Up @@ -119,6 +121,7 @@ fn test_determine_unit_from_results_unit_given_s() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -132,6 +135,7 @@ fn test_determine_unit_from_results_unit_given_s() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down Expand Up @@ -159,6 +163,7 @@ fn test_determine_unit_from_results_unit_given_ms() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -172,6 +177,7 @@ fn test_determine_unit_from_results_unit_given_ms() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down Expand Up @@ -199,6 +205,7 @@ fn test_determine_unit_from_results_unit_first_s() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -212,6 +219,7 @@ fn test_determine_unit_from_results_unit_first_s() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down Expand Up @@ -239,6 +247,7 @@ fn test_determine_unit_from_results_unit_first_ms() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand All @@ -252,6 +261,7 @@ fn test_determine_unit_from_results_unit_first_ms() {
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand Down
4 changes: 4 additions & 0 deletions src/export/orgmode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn test_orgmode_format_ms() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand All @@ -90,6 +91,7 @@ fn test_orgmode_format_ms() {
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand Down Expand Up @@ -129,6 +131,7 @@ fn test_orgmode_format_s() {
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
Expand All @@ -142,6 +145,7 @@ fn test_orgmode_format_s() {
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
Expand Down
16 changes: 16 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,19 @@ fn exports_intermediate_results_to_file() {
let contents = std::fs::read_to_string(export_path).unwrap();
assert!(contents.contains("true"));
}

#[test]
fn unused_parameters_are_shown_in_benchmark_name() {
hyperfine()
.arg("--runs=2")
.arg("--parameter-list")
.arg("branch")
.arg("master,feature")
.arg("echo test")
.assert()
.success()
.stdout(
predicate::str::contains("echo test (branch = master)")
.and(predicate::str::contains("echo test (branch = feature)")),
);
}

0 comments on commit a875276

Please sign in to comment.