From 8d815ababff4d138d4c041960f282bf5a5ee6203 Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Mon, 21 Mar 2022 20:33:44 +0100 Subject: [PATCH] Refactor Markdown table contents generation to generic Markup function --- src/export/markdown.rs | 45 +++------------------------------------- src/export/markup.rs | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/export/markdown.rs b/src/export/markdown.rs index d1c5438..4c91dd7 100644 --- a/src/export/markdown.rs +++ b/src/export/markdown.rs @@ -5,7 +5,7 @@ use crate::export::markup::markup_results_unit; use crate::export::markup::markup_table_data; use crate::export::markup::markup_table_line; use crate::export::markup::MarkupType; -use crate::output::format::format_duration_value; +use crate::export::markup::markup_results; use crate::util::units::Unit; use anyhow::{anyhow, Result}; @@ -24,49 +24,10 @@ impl Exporter for MarkdownExporter { )); } - // prepare table header strings - let notation = format!("[{}]", unit.short_name()); - let mut data: Vec> = vec![vec![ - format!("Command"), - format!("Mean {}", notation), - format!("Min {}", notation), - format!("Max {}", notation), - format!("Relative"), - ]]; - - for entry in entries.unwrap() { - 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 - data.push(vec![ - format!("`{}`", cmd_str), - format!("{}{}", mean_str, stddev_str), - format!("{}", min_str), - format!("{}", max_str), - format!("{}{}", rel_str, rel_stddev_str), - ]) - } - + let kind = MarkupType::Markdown; + let data = markup_results( &kind, &entries.unwrap(), unit ); let head: &Vec = data.first().unwrap(); let tail: &[Vec] = &data[1..]; - let kind = MarkupType::Markdown; // emit header let mut table = markup_table_data(&kind, head); diff --git a/src/export/markup.rs b/src/export/markup.rs index d8df297..274e455 100644 --- a/src/export/markup.rs +++ b/src/export/markup.rs @@ -1,6 +1,7 @@ use crate::benchmark::benchmark_result::BenchmarkResult; use crate::output::format::format_duration_value; use crate::util::units::Unit; +use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed; #[derive(Clone)] pub enum MarkupType { @@ -20,6 +21,52 @@ pub fn markup_table_line(kind: &MarkupType, size: usize) -> String { }; } +pub fn markup_results(kind: &MarkupType, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> Vec> { + // prepare table header strings + let notation = format!("[{}]", unit.short_name()); + let mut data: Vec> = vec![vec![ + format!("Command"), + format!("Mean {}", notation), + format!("Min {}", notation), + format!("Max {}", notation), + format!("Relative"), + ]]; + + 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 + data.push(vec![ + match kind { + MarkupType::Markdown => format!( "`{}`", cmd_str ), + }, + format!("{}{}", mean_str, stddev_str), + format!("{}", min_str), + format!("{}", max_str), + format!("{}{}", rel_str, rel_stddev_str), + ]) + } + + return data; +} + pub fn markup_results_unit(results: &[BenchmarkResult], unit: Option) -> Unit { return if let Some(unit) = unit { // Use the given unit for all entries.