mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-14 00:17:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36e57044c9 | ||
|
|
066cb732f9 | ||
|
|
79ca2297bb | ||
|
|
4c91b36259 | ||
|
|
9f4951ef6c | ||
|
|
b23134b371 | ||
|
|
a3d9642b2b | ||
|
|
5b2b5683b7 | ||
|
|
8cd5b1f6bc | ||
|
|
dda9b97078 | ||
|
|
288e9fb4c9 | ||
|
|
5470a08c85 | ||
|
|
8d815ababf | ||
|
|
ab03e250a4 |
No files matched your search
+67
-89
@@ -1,88 +1,60 @@
|
||||
use super::Exporter;
|
||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use crate::benchmark::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
|
||||
use crate::output::format::format_duration_value;
|
||||
use crate::util::units::Unit;
|
||||
use crate::export::markup::MarkupExporter;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use super::markup::Alignment;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MarkdownExporter {}
|
||||
|
||||
impl Exporter for MarkdownExporter {
|
||||
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
|
||||
let unit = if let Some(unit) = unit {
|
||||
// Use the given unit for all entries.
|
||||
unit
|
||||
} else if let Some(first_result) = results.first() {
|
||||
// Use the first BenchmarkResult entry to determine the unit for all entries.
|
||||
format_duration_value(first_result.mean, None).1
|
||||
} else {
|
||||
// Default to `Second`.
|
||||
Unit::Second
|
||||
};
|
||||
impl MarkupExporter for MarkdownExporter {
|
||||
fn table_row(&self, cells: &[&str]) -> String {
|
||||
format!("| {} |\n", cells.join(" | "))
|
||||
}
|
||||
|
||||
if let Some(annotated_results) = relative_speed::compute(results) {
|
||||
let mut destination = start_table(unit);
|
||||
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
|
||||
format!(
|
||||
"|{}\n",
|
||||
cell_aligmnents
|
||||
.iter()
|
||||
.map(|a| match a {
|
||||
Alignment::Left => ":---|",
|
||||
Alignment::Right => "---:|",
|
||||
})
|
||||
.collect::<String>()
|
||||
)
|
||||
}
|
||||
|
||||
for result in annotated_results {
|
||||
add_table_row(&mut destination, &result, unit);
|
||||
}
|
||||
|
||||
Ok(destination)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Relative speed comparison is not available for Markdown export."
|
||||
))
|
||||
}
|
||||
fn command(&self, cmd: &str) -> String {
|
||||
format!("`{}`", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
fn table_header(unit_short_name: String) -> String {
|
||||
/// Check Markdown-based data row formatting
|
||||
#[test]
|
||||
fn test_markdown_formatter_table_data() {
|
||||
let formatter = MarkdownExporter::default();
|
||||
|
||||
assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
|
||||
}
|
||||
|
||||
/// Check Markdown-based horizontal line formatting
|
||||
#[test]
|
||||
fn test_markdown_formatter_table_divider() {
|
||||
let formatter = MarkdownExporter::default();
|
||||
|
||||
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}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
|
||||
unit = unit_short_name
|
||||
)
|
||||
}
|
||||
|
||||
fn start_table(unit: Unit) -> Vec<u8> {
|
||||
table_header(unit.short_name()).bytes().collect()
|
||||
}
|
||||
|
||||
fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {
|
||||
let result = &entry.result;
|
||||
let mean_str = format_duration_value(result.mean, Some(unit)).0;
|
||||
let stddev_str = if let Some(stddev) = result.stddev {
|
||||
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
|
||||
} else {
|
||||
"".into()
|
||||
};
|
||||
let min_str = format_duration_value(result.min, Some(unit)).0;
|
||||
let max_str = format_duration_value(result.max, Some(unit)).0;
|
||||
let rel_str = format!("{:.2}", entry.relative_speed);
|
||||
let rel_stddev_str = if entry.is_fastest {
|
||||
"".into()
|
||||
} else if let Some(stddev) = entry.relative_speed_stddev {
|
||||
format!(" ± {:.2}", stddev)
|
||||
} else {
|
||||
"".into()
|
||||
};
|
||||
|
||||
dest.extend(
|
||||
format!(
|
||||
"| `{command}` | {mean}{stddev} | {min} | {max} | {rel}{rel_stddev} |\n",
|
||||
command = result.command.replace("|", "\\|"),
|
||||
mean = mean_str,
|
||||
stddev = stddev_str,
|
||||
min = min_str,
|
||||
max = max_str,
|
||||
rel = rel_str,
|
||||
rel_stddev = rel_stddev_str,
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -91,6 +63,8 @@ fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, u
|
||||
/// 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();
|
||||
|
||||
@@ -123,23 +97,24 @@ fn test_markdown_format_ms() {
|
||||
},
|
||||
];
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
let actual = String::from_utf8(exporter.serialize(&timing_results, None).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 |
|
||||
",
|
||||
table_header("ms".to_string())
|
||||
cfg_test_table_header("ms".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
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();
|
||||
|
||||
@@ -172,22 +147,24 @@ fn test_markdown_format_s() {
|
||||
},
|
||||
];
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
let actual = String::from_utf8(exporter.serialize(&timing_results, None).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 |
|
||||
",
|
||||
table_header("s".to_string())
|
||||
cfg_test_table_header("s".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
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();
|
||||
|
||||
@@ -220,28 +197,30 @@ fn test_markdown_format_time_unit_s() {
|
||||
},
|
||||
];
|
||||
|
||||
let formatted = String::from_utf8(
|
||||
let actual = String::from_utf8(
|
||||
exporter
|
||||
.serialize(&timing_results, Some(Unit::Second))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
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 |
|
||||
",
|
||||
table_header("s".to_string())
|
||||
cfg_test_table_header("s".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
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();
|
||||
|
||||
@@ -274,20 +253,19 @@ fn test_markdown_format_time_unit_ms() {
|
||||
},
|
||||
];
|
||||
|
||||
let formatted = String::from_utf8(
|
||||
let actual = String::from_utf8(
|
||||
exporter
|
||||
.serialize(&timing_results, Some(Unit::MilliSecond))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
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 |
|
||||
",
|
||||
table_header("ms".to_string())
|
||||
cfg_test_table_header("ms".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
assert_eq!(expect, actual);
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
|
||||
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
|
||||
use crate::output::format::format_duration_value;
|
||||
use crate::util::units::Unit;
|
||||
|
||||
use super::Exporter;
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub enum Alignment {
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
pub trait MarkupExporter {
|
||||
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
|
||||
// prepare table header strings
|
||||
let notation = format!("[{}]", unit.short_name());
|
||||
|
||||
// emit header
|
||||
let mut table = self.table_row(&[
|
||||
"Command",
|
||||
&format!("Mean {}", notation),
|
||||
&format!("Min {}", notation),
|
||||
&format!("Max {}", notation),
|
||||
"Relative",
|
||||
]);
|
||||
|
||||
// emit horizontal line
|
||||
table.push_str(&self.table_divider(&[
|
||||
Alignment::Left,
|
||||
Alignment::Right,
|
||||
Alignment::Right,
|
||||
Alignment::Right,
|
||||
Alignment::Right,
|
||||
]));
|
||||
|
||||
for entry in entries {
|
||||
let measurement = &entry.result;
|
||||
// prepare data row strings
|
||||
let cmd_str = measurement.command.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)
|
||||
} else {
|
||||
"".into()
|
||||
};
|
||||
let min_str = format_duration_value(measurement.min, Some(unit)).0;
|
||||
let max_str = format_duration_value(measurement.max, Some(unit)).0;
|
||||
let rel_str = format!("{:.2}", entry.relative_speed);
|
||||
let rel_stddev_str = if entry.is_fastest {
|
||||
"".into()
|
||||
} else if let Some(stddev) = entry.relative_speed_stddev {
|
||||
format!(" ± {:.2}", stddev)
|
||||
} else {
|
||||
"".into()
|
||||
};
|
||||
|
||||
// prepare table row entries
|
||||
table.push_str(&self.table_row(&[
|
||||
&self.command(&cmd_str),
|
||||
&format!("{}{}", mean_str, stddev_str),
|
||||
&min_str,
|
||||
&max_str,
|
||||
&format!("{}{}", rel_str, rel_stddev_str),
|
||||
]))
|
||||
}
|
||||
|
||||
table
|
||||
}
|
||||
|
||||
fn table_row(&self, cells: &[&str]) -> String;
|
||||
|
||||
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String;
|
||||
|
||||
fn command(&self, size: &str) -> String;
|
||||
}
|
||||
|
||||
fn determine_unit_from_results(results: &[BenchmarkResult]) -> Unit {
|
||||
if let Some(first_result) = results.first() {
|
||||
// Use the first BenchmarkResult entry to determine the unit for all entries.
|
||||
format_duration_value(first_result.mean, None).1
|
||||
} else {
|
||||
// Default to `Second`.
|
||||
Unit::Second
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: MarkupExporter> Exporter for T {
|
||||
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
|
||||
let unit = unit.unwrap_or_else(|| determine_unit_from_results(&results));
|
||||
let entries = relative_speed::compute(results);
|
||||
if entries.is_none() {
|
||||
return Err(anyhow!(
|
||||
"Relative speed comparison is not available for markup exporter."
|
||||
));
|
||||
}
|
||||
|
||||
let table = self.table_results(&entries.unwrap(), unit);
|
||||
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"),
|
||||
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]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: 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]),
|
||||
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"),
|
||||
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]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: 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]),
|
||||
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"),
|
||||
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]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: 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]),
|
||||
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"),
|
||||
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]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: 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]),
|
||||
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);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ mod asciidoc;
|
||||
mod csv;
|
||||
mod json;
|
||||
mod markdown;
|
||||
mod markup;
|
||||
|
||||
use self::asciidoc::AsciidocExporter;
|
||||
use self::csv::CsvExporter;
|
||||
|
||||
Reference in new issue
Block a user