Compare commits

...
Author SHA1 Message Date
David Peter 7d3c7bb81e Clippy 2024-12-29 13:57:53 +01:00
David Peter eb1a3212f7 SortOrder field 2024-12-29 13:57:18 +01:00
David Peter 72aa8b40fe Basic scheduler test 2024-12-29 13:40:05 +01:00
David Peter 74070e2357 Simplify CSV tests 2024-12-29 13:38:54 +01:00
David Peter 7004997397 Move more tests to insta 2024-12-29 13:10:50 +01:00
David Peter 7ea8e9f215 Use insta for exporter tests 2024-12-29 12:52:35 +01:00
12 changed files with 452 additions and 801 deletions

No files matched your search

Generated
+26
View File
@@ -422,6 +422,7 @@ dependencies = [
"colored",
"csv",
"indicatif",
"insta",
"libc",
"nix",
"once_cell",
@@ -460,6 +461,19 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "insta"
version = "1.41.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8"
dependencies = [
"console",
"lazy_static",
"linked-hash-map",
"serde",
"similar",
]
[[package]]
name = "instant"
version = "0.1.13"
@@ -493,6 +507,12 @@ version = "0.2.162"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@@ -1006,6 +1026,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
[[package]]
name = "similar"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"
[[package]]
name = "statistical"
version = "1.0.0"
+5
View File
@@ -63,9 +63,14 @@ features = [
[dev-dependencies]
approx = "0.5"
assert_cmd = "2.0"
insta = { version = "1.41.1", features = ["yaml"] }
predicates = "3.0"
tempfile = "3.14"
[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
[build-dependencies]
clap = "4.4.12"
clap_complete = "4.2.1"
+71 -7
View File
@@ -52,11 +52,7 @@ impl<'a> Scheduler<'a> {
// We export results after each individual benchmark, because
// we would risk losing them if a later benchmark fails.
self.export_manager.write_results(
&self.results,
self.options.sort_order_exports,
true,
)?;
self.export_manager.write_results(&self.results, true)?;
}
Ok(())
@@ -158,7 +154,75 @@ impl<'a> Scheduler<'a> {
}
pub fn final_export(&self) -> Result<()> {
self.export_manager
.write_results(&self.results, self.options.sort_order_exports, false)
self.export_manager.write_results(&self.results, false)
}
}
#[cfg(test)]
fn generate_results(args: &[&'static str]) -> Result<Vec<BenchmarkResult>> {
use crate::cli::get_cli_arguments;
let args = ["hyperfine", "--debug-mode", "--style=none"]
.iter()
.chain(args);
let cli_arguments = get_cli_arguments(args);
let mut options = Options::from_cli_arguments(&cli_arguments)?;
assert_eq!(options.executor_kind, ExecutorKind::Mock(None));
let commands = Commands::from_cli_arguments(&cli_arguments)?;
let export_manager = ExportManager::from_cli_arguments(
&cli_arguments,
options.time_unit,
options.sort_order_exports,
)?;
options.validate_against_command_list(&commands)?;
let mut scheduler = Scheduler::new(&commands, &options, &export_manager);
scheduler.run_benchmarks()?;
Ok(scheduler.results)
}
#[test]
fn scheduler_basic() -> Result<()> {
insta::assert_yaml_snapshot!(generate_results(&["--runs=2", "sleep 0.123", "sleep 0.456"])?, @r#"
- command: sleep 0.123
mean: 0.123
stddev: 0
median: 0.123
user: 0
system: 0
min: 0.123
max: 0.123
times:
- 0.123
- 0.123
memory_usage_byte:
- 0
- 0
exit_codes:
- 0
- 0
- command: sleep 0.456
mean: 0.456
stddev: 0
median: 0.456
user: 0
system: 0
min: 0.456
max: 0.456
times:
- 0.456
- 0.456
memory_usage_byte:
- 0
- 0
exit_codes:
- 0
- 0
"#);
Ok(())
}
-174
View File
@@ -65,177 +65,3 @@ fn test_asciidoc_exporter_table_header() {
assert_eq!(expect, actual);
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Asciidoc.
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: &str) -> String {
format!(
"[cols=\"<,>,>,>,>\"]\n|===\n| Command \n| Mean [{unit_short_name}] \n| Min [{unit_short_name}] \n| Max [{unit_short_name}] \n| Relative \n"
)
}
#[cfg(test)]
use crate::options::SortOrder;
#[cfg(test)]
use crate::util::units::Unit;
#[cfg(test)]
use crate::export::BenchmarkResult;
#[cfg(test)]
use crate::export::Exporter;
/// Integration test
#[test]
fn test_asciidoc_format_s() {
use std::collections::BTreeMap;
let exporter = AsciidocExporter::default();
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,
user: 3.0,
system: 4.0,
min: 5.0,
max: 6.0,
times: Some(vec![7.0, 8.0, 9.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: {
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
params.insert("bar".into(), "2".into());
params
},
},
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,
user: 13.0,
system: 14.0,
min: 15.0,
max: 16.0,
times: Some(vec![17.0, 18.0, 19.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: {
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
params.insert("bar".into(), "7".into());
params
},
},
];
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}
| `FOO=1 BAR=2 command \\| 1`
| 1.000 ± 2.000
| 5.000
| 6.000
| 1.00
| `FOO=1 BAR=7 command \\| 2`
| 11.000 ± 12.000
| 15.000
| 16.000
| 11.00 ± 25.06
|===
",
cfg_test_table_header("s")
);
assert_eq!(expect, actual);
}
/// This test demonstrates that the given unit (ms) is used to set
/// the units for all entries.
#[test]
fn test_asciidoc_format_ms() {
use std::collections::BTreeMap;
let exporter = AsciidocExporter::default();
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,
user: 0.013,
system: 0.014,
min: 0.015,
max: 0.016,
times: Some(vec![0.017, 0.018, 0.019]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: {
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
params.insert("bar".into(), "7".into());
params
},
},
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,
user: 3.0,
system: 4.0,
min: 5.0,
max: 6.0,
times: Some(vec![7.0, 8.0, 9.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: {
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
params.insert("bar".into(), "2".into());
params
},
},
];
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::MilliSecond), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}
| `FOO=1 BAR=7 command \\| 2`
| 11.0 ± 12.0
| 15.0
| 16.0
| 1.00
| `FOO=1 BAR=2 command \\| 1`
| 1000.0 ± 2000.0
| 5000.0
| 6000.0
| 90.91 ± 207.11
|===
",
cfg_test_table_header("ms")
);
assert_eq!(expect, actual);
}
+11 -12
View File
@@ -67,8 +67,8 @@ 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"),
command: String::from("command_a"),
command_with_unused_parameters: String::from("command_a"),
mean: 1.0,
stddev: Some(2.0),
median: 1.0,
@@ -87,8 +87,8 @@ 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"),
command: String::from("command_b"),
command_with_unused_parameters: String::from("command_b"),
mean: 11.0,
stddev: Some(12.0),
median: 11.0,
@@ -107,18 +107,17 @@ fn test_csv() {
},
},
];
let exps: String = String::from(
"command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo\n\
FOO=one BAR=two command | 1,1,2,1,3,4,5,6,two,one\n\
FOO=one BAR=seven command | 2,11,12,11,13,14,15,16.5,seven,one\n\
",
);
let gens = String::from_utf8(
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
assert_eq!(exps, gens);
insta::assert_snapshot!(actual, @r#"
command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo
command_a,1,2,1,3,4,5,6,two,one
command_b,11,12,11,13,14,15,16.5,seven,one
"#);
}
-273
View File
@@ -28,9 +28,6 @@ impl MarkupExporter for MarkdownExporter {
}
}
#[cfg(test)]
use crate::options::SortOrder;
/// Check Markdown-based data row formatting
#[test]
fn test_markdown_formatter_table_data() {
@@ -47,273 +44,3 @@ fn test_markdown_formatter_table_divider() {
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
assert_eq!(divider, "|:---|---:|:---|\n");
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Markdown.
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|:---|---:|---:|---:|---:|\n"
)
}
/// Ensure the markdown output includes the table header and the multiple
/// benchmark results as a table. The list of actual times is not included
/// in the output.
///
/// This also demonstrates that the first entry's units (ms) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_markdown_format_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&timing_results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
",
cfg_test_table_header("ms".to_string())
);
assert_eq!(expect, actual);
}
/// This (again) demonstrates that the first entry's units (s) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_markdown_format_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&timing_results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}
/// The given time unit (s) is used to set the units for all entries.
#[test]
fn test_markdown_format_time_unit_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
{
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}
{
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second), SortOrder::MeanTime)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}
}
/// This (again) demonstrates that the given time unit (ms) is used to set
/// the units for all entries.
#[test]
fn test_markdown_format_time_unit_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
",
cfg_test_table_header("ms".to_string())
);
assert_eq!(expect, actual);
}
-188
View File
@@ -119,191 +119,3 @@ impl<T: MarkupExporter> Exporter for T {
Ok(table.as_bytes().to_vec())
}
}
/// Check unit resolving for timing results and given unit 's'
#[test]
fn test_determine_unit_from_results_unit_given_s() {
use std::collections::BTreeMap;
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = Some(Unit::Second);
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results and given unit 'ms'
#[test]
fn test_determine_unit_from_results_unit_given_ms() {
use std::collections::BTreeMap;
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = Some(Unit::MilliSecond);
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results using the first result entry as 's'
#[test]
fn test_determine_unit_from_results_unit_first_s() {
use std::collections::BTreeMap;
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results using the first result entry as 'ms'
#[test]
fn test_determine_unit_from_results_unit_first_ms() {
use std::collections::BTreeMap;
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for not timing results and no given unit defaulting to 's'
#[test]
fn test_determine_unit_from_results_unit_default_s() {
let results: Vec<BenchmarkResult> = vec![];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
+14 -9
View File
@@ -7,6 +7,8 @@ mod json;
mod markdown;
mod markup;
mod orgmode;
#[cfg(test)]
mod tests;
use self::asciidoc::AsciidocExporter;
use self::csv::CsvExporter;
@@ -62,19 +64,24 @@ struct ExporterWithTarget {
}
/// Handles the management of multiple file exporters.
#[derive(Default)]
pub struct ExportManager {
exporters: Vec<ExporterWithTarget>,
time_unit: Option<Unit>,
sort_order: SortOrder,
}
impl ExportManager {
/// Build the ExportManager that will export the results specified
/// in the given ArgMatches
pub fn from_cli_arguments(matches: &ArgMatches, time_unit: Option<Unit>) -> Result<Self> {
pub fn from_cli_arguments(
matches: &ArgMatches,
time_unit: Option<Unit>,
sort_order: SortOrder,
) -> Result<Self> {
let mut export_manager = Self {
exporters: vec![],
time_unit,
sort_order,
};
{
let mut add_exporter = |flag, exporttype| -> Result<()> {
@@ -122,14 +129,12 @@ impl ExportManager {
/// results are written to all file targets (to always have them up to date, even
/// if a benchmark fails). In the latter case, we only print to stdout targets (in
/// order not to clutter the output of hyperfine with intermediate results).
pub fn write_results(
&self,
results: &[BenchmarkResult],
sort_order: SortOrder,
intermediate: bool,
) -> Result<()> {
pub fn write_results(&self, results: &[BenchmarkResult], intermediate: bool) -> Result<()> {
for e in &self.exporters {
let content = || e.exporter.serialize(results, self.time_unit, sort_order);
let content = || {
e.exporter
.serialize(results, self.time_unit, self.sort_order)
};
match e.target {
ExportTarget::File(ref filename) => {
-137
View File
@@ -22,9 +22,6 @@ impl MarkupExporter for OrgmodeExporter {
}
}
#[cfg(test)]
use crate::options::SortOrder;
/// Check Emacs org-mode data row formatting
#[test]
fn test_orgmode_formatter_table_data() {
@@ -52,137 +49,3 @@ fn test_orgmode_formatter_table_line() {
assert_eq!(expect, actual);
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Emacs org-mode.
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit_short_name}] | Min [{unit_short_name}] | Max [{unit_short_name}] | Relative |\n|--+--+--+--+--|\n"
)
}
/// Ensure the Emacs org-mode output includes the table header and the multiple
/// benchmark results as a table. The list of actual times is not included
/// in the output.
///
/// This also demonstrates that the first entry's units (ms) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_orgmode_format_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = OrgmodeExporter::default();
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
",
cfg_test_table_header("ms".to_string())
);
assert_eq!(expect, actual);
}
/// This test demonstrates that the given unit (s) is used to set
/// the units for all entries.
#[test]
fn test_orgmode_format_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = OrgmodeExporter::default();
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,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}
+319
View File
@@ -0,0 +1,319 @@
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::export::asciidoc::AsciidocExporter;
use crate::export::orgmode::OrgmodeExporter;
use crate::util::units::Unit;
use crate::{export::markdown::MarkdownExporter, options::SortOrder};
use std::collections::BTreeMap;
fn get_output<E: Exporter + Default>(
results: &[BenchmarkResult],
unit: Option<Unit>,
sort_order: SortOrder,
) -> String {
let exporter = E::default();
String::from_utf8(exporter.serialize(results, unit, sort_order).unwrap()).unwrap()
}
/// Ensure the makrup output includes the table header and the multiple
/// benchmark results as a table. The list of actual times is not included
/// in the output.
///
/// This also demonstrates that the first entry's units (ms) are used to set
/// the units for all entries when the time unit is not specified.
#[test]
fn test_markup_export_auto_ms() {
let results = [
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
"#);
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"
[cols="<,>,>,>,>"]
|===
| Command
| Mean [ms]
| Min [ms]
| Max [ms]
| Relative
| `sleep 0.1`
| 105.7 ± 1.6
| 102.3
| 108.0
| 1.00
| `sleep 2`
| 2005.0 ± 2.0
| 2002.0
| 2008.0
| 18.97 ± 0.29
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|--+--+--+--+--|
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
"#);
}
/// This (again) demonstrates that the first entry's units (s) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_markup_export_auto_s() {
let results = [
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, None, SortOrder::Command), @r#"
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
"#);
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, None, SortOrder::Command), @r#"
[cols="<,>,>,>,>"]
|===
| Command
| Mean [s]
| Min [s]
| Max [s]
| Relative
| `sleep 2`
| 2.005 ± 0.002
| 2.002
| 2.008
| 18.97 ± 0.29
| `sleep 0.1`
| 0.106 ± 0.002
| 0.102
| 0.108
| 1.00
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&results, None, SortOrder::Command), @r#"
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|--+--+--+--+--|
| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
"#);
}
/// This (again) demonstrates that the given time unit (ms) is used to set
/// the units for all entries.
#[test]
fn test_markup_export_manual_ms() {
let timing_results = [
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
"#);
insta::assert_snapshot!(get_output::<AsciidocExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"
[cols="<,>,>,>,>"]
|===
| Command
| Mean [ms]
| Min [ms]
| Max [ms]
| Relative
| `sleep 2`
| 2005.0 ± 2.0
| 2002.0
| 2008.0
| 18.97 ± 0.29
| `sleep 0.1`
| 105.7 ± 1.6
| 102.3
| 108.0
| 1.00
|===
"#);
insta::assert_snapshot!(get_output::<OrgmodeExporter>(&timing_results, Some(Unit::MilliSecond), SortOrder::Command), @r#"
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|--+--+--+--+--|
| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
"#);
}
/// The given time unit (s) is used to set the units for all entries.
#[test]
fn test_markup_export_manual_s() {
let results = [
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
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,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
memory_usage_byte: None,
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
"#);
insta::assert_snapshot!(get_output::<MarkdownExporter>(&results, Some(Unit::Second), SortOrder::MeanTime), @r#"
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
"#);
insta::assert_snapshot!(get_output::<AsciidocExporter>(&results, Some(Unit::Second), SortOrder::Command), @r#"
[cols="<,>,>,>,>"]
|===
| Command
| Mean [s]
| Min [s]
| Max [s]
| Relative
| `sleep 2`
| 2.005 ± 0.002
| 2.002
| 2.008
| 18.97 ± 0.29
| `sleep 0.1`
| 0.106 ± 0.002
| 0.102
| 0.108
| 1.00
|===
"#);
}
+5 -1
View File
@@ -34,7 +34,11 @@ fn run() -> Result<()> {
let cli_arguments = get_cli_arguments(env::args_os());
let mut options = Options::from_cli_arguments(&cli_arguments)?;
let commands = Commands::from_cli_arguments(&cli_arguments)?;
let export_manager = ExportManager::from_cli_arguments(&cli_arguments, options.time_unit)?;
let export_manager = ExportManager::from_cli_arguments(
&cli_arguments,
options.time_unit,
options.sort_order_exports,
)?;
options.validate_against_command_list(&commands)?;
+1
View File
@@ -178,6 +178,7 @@ impl CommandOutputPolicy {
}
}
#[derive(Debug, PartialEq)]
pub enum ExecutorKind {
Raw,
Shell(Shell),