Skip to content
Myr

markup.rs

This file

Path

src/export/markup.rs

106 lines · 3.3 KiB · Rust

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

History of this file

Take 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(&self, results: &[BenchmarkResult], primary: MetricSelection) -> Result<Vec<u8>> {81        let stats: Vec<_> = results82            .iter()83            .map(|result| {84                primary85                    .metric86                    .summarize(&result.measurements)87                    .with_context(|| {88                        format!(89                            "Metric '{}' is unavailable for '{}'",90                            primary.metric.name(),91                            result.get_name()92                        )93                    })94            })95            .collect::<Result<_>>()?;96        // Use one common unit, avoiding an unscaled column if the reference is zero.97        let mean = stats98            .iter()99            .find(|stats| stats.mean != 0.0)100            .map_or(0.0, |stats| stats.mean);101        let unit = primary.display_unit(mean);102        Ok(self103            .table_results(results, &stats, primary, unit)104            .into_bytes())105    }106}