markup.rs
This file
Path
src/export/markup.rs
111 lines · 3.4 KiB · Rust
Last changed by
Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
History of this fileTake it
1use crate::benchmark::benchmark_result::BenchmarkResult;2use crate::metric::{MetricSelection, Stats, Unit};34use super::Exporter;5use anyhow::{Context, Result};67pub enum Alignment {8 Left,9 Right,10}1112pub trait MarkupExporter {13 fn table_results(14 &self,15 results: &[BenchmarkResult],16 summaries: &[Stats],17 primary: MetricSelection,18 unit: Unit,19 ) -> String {20 let notation = format!("[{}]", unit.symbol);21 let cells_alignment = [22 Alignment::Left,23 Alignment::Right,24 Alignment::Right,25 Alignment::Left,26 ];27 let mut table = self.table_header(&cells_alignment);28 table.push_str(&self.table_row(&[29 "Command",30 &format!("Mean {} {notation}", primary.metric.label()),31 "Change",32 "Factor",33 ]));34 table.push_str(&self.table_divider(&cells_alignment));3536 for (index, (result, stats)) in results.iter().zip(summaries).enumerate() {37 let command = result.display_name.replace('|', "\\|");38 let mean = unit.format_value(stats.mean);39 let stddev = stats40 .stddev41 .map(|value| format!(" ± {}", unit.format_value(value)))42 .unwrap_or_default();43 let change = if index == 0 {44 String::new()45 } else {46 stats.format_change_from(&summaries[0])47 };48 let factor = if index == 0 {49 String::new()50 } else {51 stats.format_factor_from(&summaries[0], primary.metric)52 };53 table.push_str(&self.table_row(&[54 &self.command(&command),55 &format!("{mean}{stddev}"),56 &change,57 &factor,58 ]));59 }60 table.push_str(&self.table_footer(&cells_alignment));61 table62 }6364 fn table_row(&self, cells: &[&str]) -> String;6566 fn table_divider(&self, cell_alignments: &[Alignment]) -> String;6768 fn table_header(&self, _cell_alignments: &[Alignment]) -> String {69 String::new()70 }7172 fn table_footer(&self, _cell_alignments: &[Alignment]) -> String {73 String::new()74 }7576 fn command(&self, command: &str) -> String;77}7879impl<T: MarkupExporter> Exporter for T {80 fn serialize(81 &self,82 results: &[BenchmarkResult],83 metrics: &[MetricSelection],84 ) -> Result<Vec<u8>> {85 let primary = metrics[0];86 let stats: Vec<_> = results87 .iter()88 .map(|result| {89 primary90 .metric91 .summarize(&result.measurements)92 .with_context(|| {93 format!(94 "Metric '{}' is unavailable for '{}'",95 primary.metric.name(),96 result.get_name()97 )98 })99 })100 .collect::<Result<_>>()?;101 // Use one common unit, avoiding an unscaled column if the reference is zero.102 let mean = stats103 .iter()104 .find(|stats| stats.mean != 0.0)105 .map_or(0.0, |stats| stats.mean);106 let unit = primary.display_unit(mean);107 Ok(self108 .table_results(results, &stats, primary, unit)109 .into_bytes())110 }111}