Skip to content
Myr

Export of Emacs org-mode table support

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

* added new command-line option '--export-orgmode <FILE>' to create Emacs org-mode table output by reusing markup result formatting * based on the new MarkdownFormatter trait, see PR #490

authored by Philipp Paulweber · committed by David Peter · parent cb3a91e · Browse files · 3 files+196 added−0 removed
This commit

State

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

Diff stats

3 files+196 added−0 removed

  • other:+196 added−0 removed

3 files+196 added−0 removed

modified:src/export/mod.rs+7 added
4 unmodified lines
5mod csv;6mod json;7mod markdown;8mod markup;9mod orgmode;1011use self::asciidoc::AsciidocExporter;12use self::csv::CsvExporter;13use self::json::JsonExporter;14use self::markdown::MarkdownExporter;15use self::orgmode::OrgmodeExporter;1617use crate::benchmark::benchmark_result::BenchmarkResult;18use crate::util::units::Unit;19
13 unmodified lines
33 Json,3435 /// Markdown table36 Markdown,3738 /// Emacs org-mode tables39 Orgmode,40}4142/// Interface for different exporters.43trait Exporter {
27 unmodified lines
71 add_exporter("export-asciidoc", ExportType::Asciidoc)?;72 add_exporter("export-json", ExportType::Json)?;73 add_exporter("export-csv", ExportType::Csv)?;74 add_exporter("export-markdown", ExportType::Markdown)?;75 add_exporter("export-orgmode", ExportType::Orgmode)?;76 }77 Ok(export_manager)78 }79
6 unmodified lines
86 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),87 ExportType::Csv => Box::new(CsvExporter::default()),88 ExportType::Json => Box::new(JsonExporter::default()),89 ExportType::Markdown => Box::new(MarkdownExporter::default()),90 ExportType::Orgmode => Box::new(OrgmodeExporter::default()),91 };92 self.exporters.push(ExporterWithFilename {93 exporter,94 filename: filename.to_string(),
21 unmodified lines
added:src/export/orgmode.rs+182 added
1use super::Exporter;2use crate::benchmark::benchmark_result::BenchmarkResult;3use crate::benchmark::relative_speed;4use crate::export::markup::MarkupFormatter;5use crate::util::units::Unit;67use anyhow::{anyhow, Result};89#[derive(Default)]10pub struct OrgmodeFormatter;1112impl MarkupFormatter for OrgmodeFormatter {13 fn table_data(&self, data: &[&str]) -> String {14 format!(15 "| {} | {} |\n",16 data.first().unwrap(),17 &data[1..].join(" | ")18 )19 }2021 fn table_line(&self, size: usize) -> String {22 format!("|{}--|\n", "--+".repeat(size - 1))23 }2425 fn command(&self, cmd: &str) -> String {26 format!("={}=", cmd)27 }28}2930#[derive(Default)]31pub struct OrgmodeExporter {}3233impl Exporter for OrgmodeExporter {34 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {35 let unit = self.unit(results, unit);36 let entries = relative_speed::compute(results);37 if entries.is_none() {38 return Err(anyhow!(39 "Relative speed comparison is not available for Emacs org-mode export."40 ));41 }4243 let formatter = OrgmodeFormatter::default();44 let table = formatter.table_results(&entries.unwrap(), unit);45 Ok(table.as_bytes().to_vec())46 }47}4849/// Check Emacs org-mode data row formatting50#[test]51fn test_orgmode_formatter_table_data() {52 let formatter = OrgmodeFormatter::default();53 let data = vec!["a", "b", "c"];5455 let actual = formatter.table_data(&data);56 let expect = "| a | b | c |\n";5758 assert_eq!(expect, actual);59}6061/// Check Emacs org-mode horizontal line formatting62#[test]63fn test_orgmode_formatter_table_line() {64 let formatter = OrgmodeFormatter::default();65 let size = 5;6667 let actual = formatter.table_line(size);68 let expect = "|--+--+--+--+--|\n";6970 assert_eq!(expect, actual);71}7273/// Test helper function to create unit-based header and horizontal line74/// independently from the markup functionality for Emacs org-mode.75#[cfg(test)]76fn cfg_test_table_header(unit_short_name: String) -> String {77 format!(78 "| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n",79 unit = unit_short_name80 )81}8283/// Ensure the Emacs org-mode output includes the table header and the multiple84/// benchmark results as a table. The list of actual times is not included85/// in the output.86///87/// This also demonstrates that the first entry's units (ms) are used to set88/// the units for all entries when the time unit is not given.89#[test]90fn test_orgmode_format_ms() {91 use std::collections::BTreeMap;92 let exporter = OrgmodeExporter::default();9394 let results = vec![95 BenchmarkResult {96 command: String::from("sleep 0.1"),97 mean: 0.1057,98 stddev: Some(0.0016),99 median: 0.1057,100 user: 0.0009,101 system: 0.0011,102 min: 0.1023,103 max: 0.1080,104 times: Some(vec![0.1, 0.1, 0.1]),105 exit_codes: vec![Some(0), Some(0), Some(0)],106 parameters: BTreeMap::new(),107 },108 BenchmarkResult {109 command: String::from("sleep 2"),110 mean: 2.0050,111 stddev: Some(0.0020),112 median: 2.0050,113 user: 0.0009,114 system: 0.0012,115 min: 2.0020,116 max: 2.0080,117 times: Some(vec![2.0, 2.0, 2.0]),118 exit_codes: vec![Some(0), Some(0), Some(0)],119 parameters: BTreeMap::new(),120 },121 ];122123 let actual = String::from_utf8(exporter.serialize(&results, None).unwrap()).unwrap();124 let expect = format!(125 "{}\126| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |127| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |128",129 cfg_test_table_header("ms".to_string())130 );131132 assert_eq!(expect, actual);133}134135/// This test demonstrates that the given unit (s) is used to set136/// the units for all entries.137#[test]138fn test_orgmode_format_s() {139 use std::collections::BTreeMap;140 let exporter = OrgmodeExporter::default();141142 let results = vec![143 BenchmarkResult {144 command: String::from("sleep 2"),145 mean: 2.0050,146 stddev: Some(0.0020),147 median: 2.0050,148 user: 0.0009,149 system: 0.0012,150 min: 2.0020,151 max: 2.0080,152 times: Some(vec![2.0, 2.0, 2.0]),153 exit_codes: vec![Some(0), Some(0), Some(0)],154 parameters: BTreeMap::new(),155 },156 BenchmarkResult {157 command: String::from("sleep 0.1"),158 mean: 0.1057,159 stddev: Some(0.0016),160 median: 0.1057,161 user: 0.0009,162 system: 0.0011,163 min: 0.1023,164 max: 0.1080,165 times: Some(vec![0.1, 0.1, 0.1]),166 exit_codes: vec![Some(0), Some(0), Some(0)],167 parameters: BTreeMap::new(),168 },169 ];170171 let actual =172 String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();173 let expect = format!(174 "{}\175| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |176| =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |177",178 cfg_test_table_header("s".to_string())179 );180181 assert_eq!(expect, actual);182}
modified:src/cli.rs+7 added
240 unmodified lines
241 .value_name("FILE")242 .help("Export the timing summary statistics as a Markdown table to the given FILE."),243 )244 .arg(245 Arg::new("export-orgmode")246 .long("export-orgmode")247 .takes_value(true)248 .value_name("FILE")249 .help("Export the timing summary statistics as a Emacs org-mode table to the given FILE."),250 )251 .arg(252 Arg::new("show-output")253 .long("show-output")254 .conflicts_with("style")255 .help(
29 unmodified lines