Skip to content
Myr

Add Asciidoc export option

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

authored by Chloe Kudryavtsev · committed by David Peter · parent 7e18096 · Browse files · 4 files+223 added−0 removed
This commit

State

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

Trailers

  • Signed-off-by: Chloe Kudryavtsev <toast@toastin.space>

Diff stats

4 files+223 added−0 removed

  • other:+223 added−0 removed

4 files+223 added−0 removed

added:src/hyperfine/export/asciidoc.rs+207 added
1use super::Exporter;23use hyperfine::format::format_duration_value;4use hyperfine::types::BenchmarkResult;5use hyperfine::units::Unit;67use std::io::Result;89#[derive(Default)]10pub struct AsciidocExporter {}1112impl Exporter for AsciidocExporter {13 fn serialize(&self, results: &Vec<BenchmarkResult>, unit: Option<Unit>) -> Result<Vec<u8>> {14 let unit = if let Some(unit) = unit {15 // Use the given unit for all entries.16 unit17 } else if let Some(first_result) = results.first() {18 // Use the first BenchmarkResult entry to determine the unit for all entries.19 format_duration_value(first_result.mean, None).120 } else {21 // Default to `Second`.22 Unit::Second23 };2425 let mut res: Vec<u8> = Vec::new();26 res.append(&mut table_open());27 res.append(&mut table_startend());28 res.append(&mut table_header(unit));29 for result in results {30 res.push('\n' as u8);31 res.append(&mut table_row(result, unit));32 }33 res.append(&mut table_startend());3435 Ok(res)36 }37}3839fn table_open() -> Vec<u8> {40 "[cols=\"<,>,>\"]\n".bytes().collect()41}4243fn table_startend() -> Vec<u8> {44 "|===\n".bytes().collect()45}4647fn table_header(unittype: Unit) -> Vec<u8> {48 let unit_short_name = unittype.short_name();49 format!(50 "| Command | Mean [{unit}] | Min…Max [{unit}]\n",51 unit = unit_short_name52 ).into_bytes()53}5455fn table_row(entry: &BenchmarkResult, unit: Unit) -> Vec<u8> {56 let form = |val| format_duration_value(val, Some(unit));57 format!(58 "| `{}`\n\59 | {} ± {}\n\60 | {}…{}\n",61 entry.command.replace("|", "\\|"),62 form(entry.mean).0, form(entry.stddev).0,63 form(entry.min).0, form(entry.max).064 ).into_bytes()65}6667/// Ensure various options for the header generate correct results68#[test]69fn test_asciidoc_header() {70 let conms: Vec<u8> = "| Command | Mean [ms] | Min…Max [ms]\n".bytes().collect();71 let cons: Vec<u8> = "| Command | Mean [s] | Min…Max [s]\n".bytes().collect();72 let genms = table_header(Unit::MilliSecond);73 let gens = table_header(Unit::Second);7475 assert_eq!(conms, genms);76 assert_eq!(cons, gens);77}7879/// Ensure each table row is generated properly80#[test]81fn test_asciidoc_table_row() {82 let result = BenchmarkResult::new(83 String::from("sleep 1"), // command84 0.10491992406666667, // mean85 0.00397851689425097, // stddev86 0.005182013333333333, // user87 0.0, // system88 0.1003342584, // min89 0.10745223440000001, // max90 vec![ // times91 0.1003342584,92 0.10745223440000001,93 0.1069732794000000194 ],95 None // param96 );9798 let expms = format!(99 "| `{}`\n\100 | {} ± {}\n\101 | {}…{}\n",102 result.command,103 Unit::MilliSecond.format(result.mean), Unit::MilliSecond.format(result.stddev),104 Unit::MilliSecond.format(result.min), Unit::MilliSecond.format(result.max)105 ).into_bytes();106 let exps = format!(107 "| `{}`\n\108 | {} ± {}\n\109 | {}…{}\n",110 result.command,111 Unit::Second.format(result.mean), Unit::Second.format(result.stddev),112 Unit::Second.format(result.min), Unit::Second.format(result.max)113 ).into_bytes();114115 let genms = table_row(&result, Unit::MilliSecond);116 let gens = table_row(&result, Unit::Second);117118 assert_eq!(expms, genms);119 assert_eq!(exps, gens);120}121122/// Ensure commands get properly escaped123#[test]124fn test_asciidoc_table_row_command_escape() {125 let result = BenchmarkResult::new(126 String::from("sleep 1|"), // command127 0.10491992406666667, // mean128 0.00397851689425097, // stddev129 0.005182013333333333, // user130 0.0, // system131 0.1003342584, // min132 0.10745223440000001, // max133 vec![ // times134 0.1003342584,135 0.10745223440000001,136 0.10697327940000001137 ],138 None // param139 );140 let exps = format!(141 "| `sleep 1\\|`\n\142 | {} ± {}\n\143 | {}…{}\n",144 Unit::Second.format(result.mean), Unit::Second.format(result.stddev),145 Unit::Second.format(result.min), Unit::Second.format(result.max)146 ).into_bytes();147 let gens = table_row(&result, Unit::Second);148149 assert_eq!(exps, gens);150}151152/// Integration test153#[test]154fn test_asciidoc() {155 let exporter = AsciidocExporter::default();156 // NOTE: results are fabricated, unlike above157 let results = vec![158 BenchmarkResult::new(159 String::from("command | 1"),160 1.0,161 2.0,162 3.0,163 4.0,164 5.0,165 6.0,166 vec![167 7.0,168 8.0,169 9.0170 ],171 None172 ),173 BenchmarkResult::new(174 String::from("command | 2"),175 11.0,176 12.0,177 13.0,178 14.0,179 15.0,180 16.0,181 vec![182 17.0,183 18.0,184 19.0185 ],186 None187 )188 ];189 // NOTE: only testing with s, s/ms is tested elsewhere190 let exps: String = String::from(191 "[cols=\"<,>,>\"]\n\192 |===\n\193 | Command | Mean [s] | Min…Max [s]\n\194 \n\195 | `command \\| 1`\n\196 | 1.000 ± 2.000\n\197 | 5.000…6.000\n\198 \n\199 | `command \\| 2`\n\200 | 11.000 ± 12.000\n\201 | 15.000…16.000\n\202 |===\n\203 ");204 let gens = String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();205206 assert_eq!(exps, gens);207}
modified:src/hyperfine/export/mod.rs+6 added
1mod asciidoc;2mod csv;3mod json;4mod markdown;56use self::asciidoc::AsciidocExporter;7use self::csv::CsvExporter;8use self::json::JsonExporter;9use self::markdown::MarkdownExporter;10
5 unmodified lines
1617/// The desired form of exporter to use for a given file.18#[derive(Clone)]19pub enum ExportType {20 /// Asciidoc Table21 Asciidoc,2223 /// CSV (comma separated values) format24 Csv,2526 /// JSON format
29 unmodified lines
5657 /// Add an additional exporter to the ExportManager58 pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) {59 let exporter: Box<Exporter> = match export_type {60 ExportType::Asciidoc => Box::new(AsciidocExporter::default()),61 ExportType::Csv => Box::new(CsvExporter::default()),62 ExportType::Json => Box::new(JsonExporter::default()),63 ExportType::Markdown => Box::new(MarkdownExporter::default()),64 };
21 unmodified lines
modified:src/hyperfine/app.rs+7 added
145 unmodified lines
146 .possible_values(&["millisecond", "second"])147 .help("Set the time unit used. Possible values: millisecond, second."),148 )149 .arg(150 Arg::with_name("export-asciidoc")151 .long("export-asciidoc")152 .takes_value(true)153 .value_name("FILE")154 .help("Export the timing summary statistics as an Asciidoc table to the given FILE."),155 )156 .arg(157 Arg::with_name("export-csv")158 .long("export-csv")159 .takes_value(true)160 .value_name("FILE")
30 unmodified lines
modified:src/main.rs+3 added
214 unmodified lines
215/// Build the ExportManager that will export the results specified216/// in the given ArgMatches217fn build_export_manager(matches: &ArgMatches) -> ExportManager {218 let mut export_manager = ExportManager::new();219 if let Some(filename) = matches.value_of("export-asciidoc") {220 export_manager.add_exporter(ExportType::Asciidoc, filename);221 }222 if let Some(filename) = matches.value_of("export-json") {223 export_manager.add_exporter(ExportType::Json, filename);224 }225 if let Some(filename) = matches.value_of("export-csv") {
31 unmodified lines