Skip to content
Myr

Refactor Markdown table emitting to generic Markup function

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Philipp Paulweber · committed by David Peter · parent 3b1fc32 · Browse files · 2 files+45 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+45 added−38 removed

  • other:+45 added−38 removed

2 files+45 added−38 removed

modified:src/export/markdown.rs+7 added-22 removed
1use super::Exporter;2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::benchmark::relative_speed;4use crate::export::markup::markup_results_unit;5use crate::export::markup::markup_table_data;6use crate::export::markup::markup_table_line;7use crate::export::markup::MarkupType;4use crate::export::markup::markup_results;5use crate::export::markup::markup_table;6use crate::export::markup::markup_unit;7use crate::export::markup::MarkupType;8use crate::util::units::Unit;910use anyhow::{anyhow, Result};11
1 unmodified line
13pub struct MarkdownExporter {}1415impl Exporter for MarkdownExporter {16 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {18 let unit = markup_results_unit(results, unit);1917 let entries = relative_speed::compute(results);18 if entries.is_none() {19 return Err(anyhow!(20 "Relative speed comparison is not available for Markdown export."21 ));22 }2324 let kind = MarkupType::Markdown;28 let data = markup_results( &kind, &entries.unwrap(), unit );29 let head: &Vec<String> = data.first().unwrap();30 let tail: &[Vec<String>] = &data[1..];3132 // emit header33 let mut table = markup_table_data(&kind, head);3435 // emit horizontal line36 table.push_str(&markup_table_line(&kind, head.len()));3738 // emit data rows39 for row in tail {40 table.push_str(&markup_table_data(&kind, row))41 }4225 let unit = markup_unit(results, unit);26 let data = markup_results(&kind, &entries.unwrap(), unit);27 let table = markup_table(&kind, &data);28 Ok(table.as_bytes().to_vec())29 }30}3132/// Test helper function to create unit-based header and horizontal line48/// independently from the markup functionality.33/// independently from the markup functionality for Markdown.34#[cfg(test)]35fn test_table_header(unit_short_name: String) -> String {36 format!(37 "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
212 unmodified lines
modified:src/export/markup.rs+38 added-16 removed
1use crate::benchmark::benchmark_result::BenchmarkResult;2use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;3use crate::output::format::format_duration_value;4use crate::util::units::Unit;4use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;56#[derive(Clone)]7pub enum MarkupType {8 /// Markdown table9 Markdown,10}1112pub fn markup_table(kind: &MarkupType, data: &[Vec<String>]) -> String {13 let head: &Vec<String> = data.first().unwrap();14 let tail: &[Vec<String>] = &data[1..];1516 // emit header17 let mut table = markup_table_data(&kind, head);1819 // emit horizontal line20 table.push_str(&markup_table_line(&kind, head.len()));2122 // emit data rows23 for row in tail {24 table.push_str(&markup_table_data(&kind, row))25 }2627 return table;28}2912pub fn markup_table_data(kind: &MarkupType, data: &Vec<String>) -> String {30fn markup_table_data(kind: &MarkupType, data: &Vec<String>) -> String {31 return match kind {32 MarkupType::Markdown => format!("| {} |\n", data.join(" | ")),33 };34}3518pub fn markup_table_line(kind: &MarkupType, size: usize) -> String {36fn markup_table_line(kind: &MarkupType, size: usize) -> String {37 return match kind {38 MarkupType::Markdown => format!("|:---|{}\n", "---:|".repeat(size - 1)),39 };40}4124pub fn markup_results(kind: &MarkupType, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> Vec<Vec<String>> {42pub fn markup_results(43 kind: &MarkupType,44 entries: &[BenchmarkResultWithRelativeSpeed],45 unit: Unit,46) -> Vec<Vec<String>> {47 // prepare table header strings48 let notation = format!("[{}]", unit.short_name());49 let mut data: Vec<Vec<_>> = vec![vec![50 format!("Command"),
25 unmodified lines
76 };77 // prepare table row entries78 data.push(vec![79 match kind {58 MarkupType::Markdown => format!( "`{}`", cmd_str ),80 MarkupType::Markdown => format!("`{}`", cmd_str),81 },82 format!("{}{}", mean_str, stddev_str),83 format!("{}", min_str),84 format!("{}", max_str),
3 unmodified lines
8889 return data;90}9170pub fn markup_results_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {92pub fn markup_unit(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {93 return if let Some(unit) = unit {94 // Use the given unit for all entries.95 unit96 } else if let Some(first_result) = results.first() {
30 unmodified lines
127}128129/// Check unit resolving for timing results and given unit 's'130#[test]109fn test_markup_table_results_unit_given_s() {131fn test_markup_table_unit_given_s() {132 use std::collections::BTreeMap;133 let results = vec![134 BenchmarkResult {135 command: String::from("sleep 2"),
23 unmodified lines
159 },160 ];161 let unit = Some(Unit::Second);162141 let markup_actual = markup_results_unit(&results, unit);163 let markup_actual = markup_unit(&results, unit);164 let markup_expected = Unit::Second;165166 assert_eq!(markup_expected, markup_actual);167}168169/// Check unit resolving for timing results and given unit 'ms'170#[test]149fn test_markup_table_results_unit_given_ms() {171fn test_markup_table_unit_given_ms() {172 use std::collections::BTreeMap;173 let results = vec![174 BenchmarkResult {175 command: String::from("sleep 2"),
23 unmodified lines
199 },200 ];201 let unit = Some(Unit::MilliSecond);202181 let markup_actual = markup_results_unit(&results, unit);203 let markup_actual = markup_unit(&results, unit);204 let markup_expected = Unit::MilliSecond;205206 assert_eq!(markup_expected, markup_actual);207}208209/// Check unit resolving for timing results using the first result entry as 's'210#[test]189fn test_markup_table_results_unit_first_s() {211fn test_markup_table_unit_first_s() {212 use std::collections::BTreeMap;213 let results = vec![214 BenchmarkResult {215 command: String::from("sleep 2"),
23 unmodified lines
239 },240 ];241 let unit = None;242221 let markup_actual = markup_results_unit(&results, unit);243 let markup_actual = markup_unit(&results, unit);244 let markup_expected = Unit::Second;245246 assert_eq!(markup_expected, markup_actual);247}248249/// Check unit resolving for timing results using the first result entry as 'ms'250#[test]229fn test_markup_table_results_unit_first_ms() {251fn test_markup_table_unit_first_ms() {252 use std::collections::BTreeMap;253 let results = vec![254 BenchmarkResult {255 command: String::from("sleep 0.1"),
23 unmodified lines
279 },280 ];281 let unit = None;282261 let markup_actual = markup_results_unit(&results, unit);283 let markup_actual = markup_unit(&results, unit);284 let markup_expected = Unit::MilliSecond;285286 assert_eq!(markup_expected, markup_actual);287}288289/// Check unit resolving for not timing results and no given unit defaulting to 's'290#[test]269fn test_markup_table_results_unit_default_s() {291fn test_markup_table_unit_default_s() {292 let results: Vec<BenchmarkResult> = vec![];293 let unit = None;294273 let markup_actual = markup_results_unit(&results, unit);295 let markup_actual = markup_unit(&results, unit);296 let markup_expected = Unit::Second;297298 assert_eq!(markup_expected, markup_actual);299}