Files
hyperfine/src/export/markup.rs
T

146 lines
4.9 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;
2026-10-05 14:59:18 +02:00
use crate::quantity::{FormatQuantity, IsUnit, Time, TimeUnit, Zero};
2022-05-15 21:11:22 +02:00
use super::Exporter;
use anyhow::Result;
2026-10-05 14:59:18 +02:00
use std::cmp::Ordering;
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],
2026-10-05 14:18:33 +02:00
time_unit: TimeUnit,
2026-10-05 14:59:18 +02:00
reference_pending: bool,
explicit_reference: bool,
) -> String {
// prepare table header strings
2026-10-05 14:18:33 +02:00
let notation = format!("[{}]", time_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 {
2026-10-05 14:15:17 +02:00
let result = &entry.result;
// prepare data row strings
2026-10-05 14:15:17 +02:00
let cmd_str = result.command_with_unused_parameters().replace('|', "\\|");
2026-10-05 14:18:33 +02:00
let mean_str = format_duration_value(result.mean_wall_clock_time(), Some(time_unit)).0;
2026-10-05 14:15:34 +02:00
let stddev_str = if let Some(stddev) = result.measurements.stddev() {
2026-10-05 14:18:33 +02:00
format!(" ± {}", format_duration_value(stddev, Some(time_unit)).0)
} else {
"".into()
};
2026-10-05 14:18:33 +02:00
let min_str = result.measurements.min().format_value(time_unit);
let max_str = result.measurements.max().format_value(time_unit);
2026-10-05 14:59:18 +02:00
// The ratio of two zero times is undefined, even if they compare equal.
let relative_unavailable = reference_pending
|| (explicit_reference
&& !entry.is_reference
&& entry.relative_ordering == Ordering::Equal
2026-10-05 14:15:17 +02:00
&& result.mean_wall_clock_time() == Time::zero());
2026-10-05 15:07:46 +02:00
let relative = if relative_unavailable {
2026-10-05 14:59:18 +02:00
"N/A".to_string()
} else {
2026-10-05 15:07:46 +02:00
let stddev = match (entry.is_reference, entry.relative_speed_stddev) {
(false, Some(stddev)) => format!(" ± {stddev:.2}"),
_ => String::new(),
};
let label = if explicit_reference {
entry.reference_label()
} else {
""
};
format!("{:.2}{stddev}{label}", entry.relative_speed)
2026-10-05 14:59:18 +02:00
};
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,
2026-10-05 15:07:46 +02:00
&relative,
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
fn determine_unit_from_results(results: &[BenchmarkResult]) -> TimeUnit {
2022-05-15 21:11:22 +02:00
if let Some(first_result) = results.first() {
// Use the first BenchmarkResult entry to determine the unit for all entries.
2026-10-05 14:15:17 +02:00
format_duration_value(first_result.mean_wall_clock_time(), None).1
2022-05-15 21:11:22 +02:00
} else {
// Default to `Second`.
TimeUnit::Second
2022-05-15 21:11:22 +02:00
}
}
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<TimeUnit>,
2023-06-03 21:06:50 +02:00
sort_order: SortOrder,
2026-10-05 14:59:18 +02:00
reference_index: Option<usize>,
2023-06-03 21:06:50 +02:00
) -> Result<Vec<u8>> {
2022-05-17 22:57:10 +02:00
let unit = unit.unwrap_or_else(|| determine_unit_from_results(results));
2026-10-05 14:59:18 +02:00
// Do not report ratios against another benchmark while the selected
// reference is still pending in an intermediate export.
let reference_pending = reference_index.is_some_and(|i| i >= results.len());
2026-10-05 15:07:46 +02:00
let entries = relative_speed::compute(
results,
sort_order,
reference_index.and_then(|i| results.get(i)),
);
2022-05-15 21:09:33 +02:00
2026-10-05 14:59:18 +02:00
let table =
self.table_results(&entries, unit, reference_pending, reference_index.is_some());
2022-05-15 21:09:33 +02:00
Ok(table.as_bytes().to_vec())
}
}