Files
hyperfine/src/export/markup.rs
T

301 lines
9.6 KiB
Rust
Raw Normal View History

use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
2022-05-15 21:09:33 +02:00
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
2023-06-03 21:06:50 +02:00
use crate::options::SortOrder;
use crate::output::format::format_duration_value;
use crate::util::units::Unit;
2022-05-15 21:11:22 +02:00
use super::Exporter;
use anyhow::Result;
2022-05-15 21:09:33 +02:00
2022-05-15 21:28:35 +02:00
pub enum Alignment {
Left,
Right,
}
2022-05-15 21:09:33 +02:00
pub trait MarkupExporter {
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
2022-05-15 21:28:35 +02:00
// prepare table cells alignment
let cells_alignment = [
2022-05-15 21:28:35 +02:00
Alignment::Left,
Alignment::Right,
Alignment::Right,
Alignment::Right,
Alignment::Right,
];
// emit table header format
let mut table = self.table_header(&cells_alignment);
// emit table header data
table.push_str(&self.table_row(&[
"Command",
2024-05-31 13:26:35 -07:00
&format!("Mean {notation}"),
&format!("Min {notation}"),
&format!("Max {notation}"),
"Relative",
2022-05-15 21:28:35 +02:00
]));
// emit horizontal line
table.push_str(&self.table_divider(&cells_alignment));
for entry in entries {
let measurement = &entry.result;
// prepare data row strings
2023-04-20 21:32:06 +02:00
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)
} 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 {
2024-05-31 13:26:35 -07:00
format!(" ± {stddev:.2}")
} else {
"".into()
};
2022-05-15 21:14:56 +02:00
// prepare table row entries
2022-05-15 21:14:56 +02:00
table.push_str(&self.table_row(&[
&self.command(&cmd_str),
2024-05-31 13:26:35 -07:00
&format!("{mean_str}{stddev_str}"),
&min_str,
&max_str,
2024-05-31 13:26:35 -07:00
&format!("{rel_str}{rel_stddev_str}"),
2022-05-15 21:14:56 +02:00
]))
}
// emit table footer format
table.push_str(&self.table_footer(&cells_alignment));
table
}
2022-05-15 21:28:35 +02:00
fn table_row(&self, cells: &[&str]) -> String;
2022-05-15 21:28:35 +02:00
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String;
fn table_header(&self, _cell_aligmnents: &[Alignment]) -> String {
"".to_string()
}
fn table_footer(&self, _cell_aligmnents: &[Alignment]) -> String {
"".to_string()
}
fn command(&self, size: &str) -> String;
}
2022-05-15 21:09:33 +02:00
2022-05-15 21:11:22 +02:00
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
}
}
2022-05-15 21:09:33 +02:00
impl<T: MarkupExporter> Exporter for T {
2023-06-03 21:06:50 +02:00
fn serialize(
&self,
results: &[BenchmarkResult],
unit: Option<Unit>,
sort_order: SortOrder,
) -> Result<Vec<u8>> {
2022-05-17 22:57:10 +02:00
let unit = unit.unwrap_or_else(|| determine_unit_from_results(results));
2023-06-03 21:06:50 +02:00
let entries = relative_speed::compute(results, sort_order);
2022-05-15 21:09:33 +02:00
let table = self.table_results(&entries, unit);
2022-05-15 21:09:33 +02:00
Ok(table.as_bytes().to_vec())
}
}
2022-05-15 21:11:22 +02:00
/// 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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 2"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 0.1"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 2"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 0.1"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 2"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 0.1"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 0.1"),
2022-05-15 21:11:22 +02:00
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"),
2023-04-20 21:32:06 +02:00
command_with_unused_parameters: String::from("sleep 2"),
2022-05-15 21:11:22 +02:00
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);
}