Skip to content
Myr

Fixed #452 - Difference between markdown and asciidoctor output

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

authored by Karl Heinz Marbaise · committed by David Peter · parent 63d7442 · Browse files · 1 file+160 added−110 removed
This commit

State

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

Diff stats

1 file+160 added−110 removed

  • other:+160 added−110 removed

1 file+160 added−110 removed

modified:src/export/asciidoc.rs+160 added-110 removed
1use std::io::Result;1use std::io::{Error, ErrorKind, Result};23use super::Exporter;3use crate::benchmark_result::BenchmarkResult;4use crate::format::format_duration_value;5use crate::relative_speed::{self, BenchmarkResultWithRelativeSpeed};6use crate::units::Unit;78use super::Exporter;910#[derive(Default)]11pub struct AsciidocExporter {}1213impl Exporter for AsciidocExporter {
8 unmodified lines
22 // Default to `Second`.23 Unit::Second24 };2526 if let Some(annotated_results) = relative_speed::compute(results) {24 let mut res: Vec<u8> = Vec::new();25 res.append(&mut table_open());26 res.append(&mut table_startend());27 res.append(&mut table_header(unit));28 for result in results {29 res.push(b'\n');30 res.append(&mut table_row(result, unit));31 }32 res.append(&mut table_startend());27 let mut res: Vec<u8> = Vec::new();28 res.append(&mut table_open());29 res.append(&mut table_startend());30 res.append(&mut table_header(unit));31 for result in annotated_results {32 res.push(b'\n');33 res.append(&mut table_row(&result, unit));34 }35 res.append(&mut table_startend());3634 Ok(res)37 Ok(res)38 } else {39 Err(Error::new(40 ErrorKind::Other,41 "Relative speed comparison is not available for Asciidoctor export.",42 ))43 }44 }45}4647fn table_open() -> Vec<u8> {39 "[cols=\"<,>,>\"]\n".bytes().collect()48 "[cols=\"<,>,>,>,>\"]\n".bytes().collect()49}5051fn table_startend() -> Vec<u8> {52 "|===\n".bytes().collect()
1 unmodified line
5455fn table_header(unittype: Unit) -> Vec<u8> {56 let unit_short_name = unittype.short_name();57 format!(49 "| Command | Mean [{unit}] | Min…Max [{unit}]\n",58 "| Command \n| Mean [{unit}] \n| Min [{unit}] \n| Max [{unit}] \n| Relative \n",59 unit = unit_short_name60 )61 .into_bytes()62}6355fn table_row(entry: &BenchmarkResult, unit: Unit) -> Vec<u8> {56 let form = |val| format_duration_value(val, Some(unit));64fn table_row(entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) -> Vec<u8> {65 let result = &entry.result;66 let mean_str = format_duration_value(result.mean, Some(unit)).0;67 let stddev_str = if let Some(stddev) = result.stddev {68 format!(" ± {}", format_duration_value(stddev, Some(unit)).0)69 } else {70 "".into()71 };72 let min_str = format_duration_value(result.min, Some(unit)).0;73 let max_str = format_duration_value(result.max, Some(unit)).0;74 let rel_str = format!("{:.2}", entry.relative_speed);75 let rel_stddev_str = if entry.is_fastest {76 "".into()77 } else {78 if let Some(stddev) = entry.relative_speed_stddev {79 format!(" ± {:.2}", stddev)80 } else {81 "".into()82 }83 };8485 format!(58 "| `{}`\n\59 | {} ± {}\n\60 | {}…{}\n",61 entry.command.replace("|", "\\|"),62 form(entry.mean).0,63 entry.stddev.map(|s| form(s).0).unwrap_or("?".into()),64 form(entry.min).0,65 form(entry.max).086 "| `{command}` \n\87 | {mean}{stddev} \n\88 | {min} \n\89 | {max} \n\90 | {rel}{rel_stddev} \n",91 command = result.command.replace("|", "\\|"),92 mean = mean_str,93 stddev = stddev_str,94 min = min_str,95 max = max_str,96 rel = rel_str,97 rel_stddev = rel_stddev_str98 )99 .into_bytes()100}101102/// Ensure various options for the header generate correct results103#[test]104fn test_asciidoc_header() {73 let conms: Vec<u8> = "| Command | Mean [ms] | Min…Max [ms]\n".bytes().collect();74 let cons: Vec<u8> = "| Command | Mean [s] | Min…Max [s]\n".bytes().collect();105 let conms: Vec<u8> = "| Command \n| Mean [ms] \n| Min [ms] \n| Max [ms] \n| Relative \n"106 .bytes()107 .collect();108 let cons: Vec<u8> = "| Command \n| Mean [s] \n| Min [s] \n| Max [s] \n| Relative \n"109 .bytes()110 .collect();111 let genms = table_header(Unit::MilliSecond);112 let gens = table_header(Unit::Second);113114 assert_eq!(conms, genms);
3 unmodified lines
118/// Ensure each table row is generated properly119#[test]120fn test_asciidoc_table_row() {121 use std::collections::BTreeMap;86 let result = BenchmarkResult::new(87 String::from("sleep 1"), // command88 0.10491992406666667, // mean89 Some(0.00397851689425097), // stddev90 0.10491992406666667, // median91 0.005182013333333333, // user92 0.0, // system93 0.1003342584, // min94 0.10745223440000001, // max95 vec![96 // times97 0.1003342584,98 0.10745223440000001,99 0.10697327940000001,100 ],101 vec![Some(0), Some(0), Some(0)], // exit codes102 BTreeMap::new(), // param103 );122105 let expms = format!(106 "| `{}`\n\107 | {} ± {}\n\108 | {}…{}\n",109 result.command,110 Unit::MilliSecond.format(result.mean),111 Unit::MilliSecond.format(result.stddev.unwrap()),112 Unit::MilliSecond.format(result.min),113 Unit::MilliSecond.format(result.max)114 )115 .into_bytes();116 let exps = format!(117 "| `{}`\n\118 | {} ± {}\n\119 | {}…{}\n",120 result.command,121 Unit::Second.format(result.mean),122 Unit::Second.format(result.stddev.unwrap()),123 Unit::Second.format(result.min),124 Unit::Second.format(result.max)125 )126 .into_bytes();123 let timing_result = BenchmarkResultWithRelativeSpeed {124 result: &BenchmarkResult::new(125 String::from("sleep 1"), // command126 0.10491992406666667, // mean127 Some(0.00397851689425097), // stddev128 0.10491992406666667, // median129 0.005182013333333333, // user130 0.0, // system131 0.1003342584, // min132 0.10745223440000001, // max133 vec![134 // times135 0.1003342584,136 0.10745223440000001,137 0.10697327940000001,138 ],139 vec![Some(0), Some(0), Some(0)], // exit codes140 BTreeMap::new(), // param141 ),142 relative_speed: 1.000, // relative_speed143 relative_speed_stddev: Option::from(1.03), // relative_speed_stddev144 is_fastest: true, // is_fastest145 };146128 let genms = table_row(&result, Unit::MilliSecond);129 let gens = table_row(&result, Unit::Second);147 let formatted = String::from_utf8(table_row(&timing_result, Unit::MilliSecond)).unwrap();148131 assert_eq!(expms, genms);132 assert_eq!(exps, gens);149 let formatted_expected = "| `sleep 1` \n\150 | 104.9 ± 4.0 \n\151 | 100.3 \n\152 | 107.5 \n\153 | 1.00 \n";154155 assert_eq!(formatted_expected, formatted);156157 let formatted_seconds = String::from_utf8(table_row(&timing_result, Unit::Second)).unwrap();158 let formatted_expected_seconds = "| `sleep 1` \n\159 | 0.105 ± 0.004 \n\160 | 0.100 \n\161 | 0.107 \n\162 | 1.00 \n";163164 assert_eq!(formatted_expected_seconds, formatted_seconds);165}166167/// Ensure commands get properly escaped168#[test]169fn test_asciidoc_table_row_command_escape() {170 use std::collections::BTreeMap;171 let benchmark_result = BenchmarkResultWithRelativeSpeed {139 let result = BenchmarkResult::new(140 String::from("sleep 1|"), // command141 0.10491992406666667, // mean142 Some(0.00397851689425097), // stddev143 0.10491992406666667, // median144 0.005182013333333333, // user145 0.0, // system146 0.1003342584, // min147 0.10745223440000001, // max148 vec![149 // times150 0.1003342584,151 0.10745223440000001,152 0.10697327940000001,153 ],154 vec![Some(0), Some(0), Some(0)], // exit codes155 BTreeMap::new(), // param172 result: &BenchmarkResult::new(173 String::from("sleep 1|"), // command174 0.10491992406666667, // mean175 Some(0.00397851689425097), // stddev176 0.10491992406666667, // median177 0.005182013333333333, // user178 0.0, // system179 0.1003342584, // min180 0.10745223440000001, // max181 vec![182 // times183 0.1003342584,184 0.10745223440000001,185 0.10697327940000001,186 ],187 vec![Some(0), Some(0), Some(0)], // exit codes188 BTreeMap::new(), // param189 ),190 relative_speed: 1.000, // relative_speed191 relative_speed_stddev: Option::from(1.03), // relative_speed_stddev192 is_fastest: true, // is_fastest193 };194 let expected = String::from_utf8(195 format!(196 "| `sleep 1\\|` \n\197 | {} ± {} \n\198 | {} \n\199 | {} \n\200 | {:.2} \n",201 Unit::Second.format(benchmark_result.result.mean),202 Unit::Second.format(benchmark_result.result.stddev.unwrap()),203 Unit::Second.format(benchmark_result.result.min),204 Unit::Second.format(benchmark_result.result.max),205 benchmark_result.relative_speed206 )207 .into_bytes(),208 );157 let exps = format!(158 "| `sleep 1\\|`\n\159 | {} ± {}\n\160 | {}…{}\n",161 Unit::Second.format(result.mean),162 Unit::Second.format(result.stddev.unwrap()),163 Unit::Second.format(result.min),164 Unit::Second.format(result.max)165 )166 .into_bytes();167 let gens = table_row(&result, Unit::Second);209 let generated_seconds = String::from_utf8(table_row(&benchmark_result, Unit::Second));210169 assert_eq!(exps, gens);211 assert_eq!(expected, generated_seconds);212}213214/// Integration test215#[test]
39 unmodified lines
255 },256 ),257 ];258 // NOTE: only testing with s, s/ms is tested elsewhere217 let exps: String = String::from(218 "[cols=\"<,>,>\"]\n\259 let expected: String = String::from(260 "[cols=\"<,>,>,>,>\"]\n\261 |===\n\220 | Command | Mean [s] | Min…Max [s]\n\262 | Command \n\263 | Mean [s] \n\264 | Min [s] \n\265 | Max [s] \n\266 | Relative \n\267 \n\222 | `FOO=1 BAR=2 command \\| 1`\n\223 | 1.000 ± 2.000\n\224 | 5.000…6.000\n\268 | `FOO=1 BAR=2 command \\| 1` \n\269 | 1.000 ± 2.000 \n\270 | 5.000 \n\271 | 6.000 \n\272 | 1.00 \n\273 \n\226 | `FOO=1 BAR=7 command \\| 2`\n\227 | 11.000 ± 12.000\n\228 | 15.000…16.000\n\274 | `FOO=1 BAR=7 command \\| 2` \n\275 | 11.000 ± 12.000 \n\276 | 15.000 \n\277 | 16.000 \n\278 | 11.00 ± 25.06 \n\279 |===\n\280 ",281 );232 let gens =282 let given =283 String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();284235 assert_eq!(exps, gens);285 assert_eq!(expected, given);286}