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
Provenance
- Commit
2443383- Tree
b9bb804- Author
Philipp Paulweber <p.paulweber@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
cb3a91e
3 files changed+196 added−0 removed
modified:src/export/mod.rs+7 added
4 unmodified lines5 mod csv;6 mod json;7 mod markdown;8 mod markup;9+mod orgmode;10 11 use self::asciidoc::AsciidocExporter;12 use self::csv::CsvExporter;13 use self::json::JsonExporter;14 use self::markdown::MarkdownExporter;15+use self::orgmode::OrgmodeExporter;16 17 use crate::benchmark::benchmark_result::BenchmarkResult;18 use crate::util::units::Unit;19 13 unmodified lines33 Json,34 35 /// Markdown table36 Markdown,37+38+ /// Emacs org-mode tables39+ Orgmode,40 }41 42 /// Interface for different exporters.43 trait Exporter {27 unmodified lines71 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 lines86 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 linesadded:src/export/orgmode.rs+182 added
1+use super::Exporter;2+use crate::benchmark::benchmark_result::BenchmarkResult;3+use crate::benchmark::relative_speed;4+use crate::export::markup::MarkupFormatter;5+use crate::util::units::Unit;6+7+use anyhow::{anyhow, Result};8+9+#[derive(Default)]10+pub struct OrgmodeFormatter;11+12+impl 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+ }20+21+ fn table_line(&self, size: usize) -> String {22+ format!("|{}--|\n", "--+".repeat(size - 1))23+ }24+25+ fn command(&self, cmd: &str) -> String {26+ format!("={}=", cmd)27+ }28+}29+30+#[derive(Default)]31+pub struct OrgmodeExporter {}32+33+impl 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+ }42+43+ let formatter = OrgmodeFormatter::default();44+ let table = formatter.table_results(&entries.unwrap(), unit);45+ Ok(table.as_bytes().to_vec())46+ }47+}48+49+/// Check Emacs org-mode data row formatting50+#[test]51+fn test_orgmode_formatter_table_data() {52+ let formatter = OrgmodeFormatter::default();53+ let data = vec!["a", "b", "c"];54+55+ let actual = formatter.table_data(&data);56+ let expect = "| a | b | c |\n";57+58+ assert_eq!(expect, actual);59+}60+61+/// Check Emacs org-mode horizontal line formatting62+#[test]63+fn test_orgmode_formatter_table_line() {64+ let formatter = OrgmodeFormatter::default();65+ let size = 5;66+67+ let actual = formatter.table_line(size);68+ let expect = "|--+--+--+--+--|\n";69+70+ assert_eq!(expect, actual);71+}72+73+/// Test helper function to create unit-based header and horizontal line74+/// independently from the markup functionality for Emacs org-mode.75+#[cfg(test)]76+fn 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+}82+83+/// 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]90+fn test_orgmode_format_ms() {91+ use std::collections::BTreeMap;92+ let exporter = OrgmodeExporter::default();93+94+ 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+ ];122+123+ 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+ );131+132+ assert_eq!(expect, actual);133+}134+135+/// This test demonstrates that the given unit (s) is used to set136+/// the units for all entries.137+#[test]138+fn test_orgmode_format_s() {139+ use std::collections::BTreeMap;140+ let exporter = OrgmodeExporter::default();141+142+ 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+ ];170+171+ 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+ );180+181+ assert_eq!(expect, actual);182+}modified:src/cli.rs+7 added
240 unmodified lines241 .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