Compare commits

...
Author SHA1 Message Date
David Peter ec46716179 Use cross to build x86_64-unknown-linux-gnu executables
We do this in order to link against older versions of glibc.

closes #508
2022-05-16 21:05:42 +02:00
David Peter 692469659b Add CHANGELOG entry for Emacs org-mode 2022-05-15 21:58:47 +02:00
David Peter eb0ccc85f1 Adapt to new markup exporter changes 2022-05-15 21:57:35 +02:00
Philipp Paulweber 2443383fc8 Export of Emacs org-mode table support
* 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
2022-05-15 21:57:35 +02:00
dependabot[bot] cb3a91ea2c Bump thiserror from 1.0.30 to 1.0.31
Bumps [thiserror](https://github.com/dtolnay/thiserror) from 1.0.30 to 1.0.31.
- [Release notes](https://github.com/dtolnay/thiserror/releases)
- [Commits](https://github.com/dtolnay/thiserror/compare/1.0.30...1.0.31)

---
updated-dependencies:
- dependency-name: thiserror
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-15 21:50:04 +02:00
dependabot[bot] 251a697e30 Bump libc from 0.2.121 to 0.2.125
Bumps [libc](https://github.com/rust-lang/libc) from 0.2.121 to 0.2.125.
- [Release notes](https://github.com/rust-lang/libc/releases)
- [Commits](https://github.com/rust-lang/libc/compare/0.2.121...0.2.125)

---
updated-dependencies:
- dependency-name: libc
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-15 21:49:56 +02:00
dependabot[bot] b9240335f5 Bump anyhow from 1.0.56 to 1.0.57
Bumps [anyhow](https://github.com/dtolnay/anyhow) from 1.0.56 to 1.0.57.
- [Release notes](https://github.com/dtolnay/anyhow/releases)
- [Commits](https://github.com/dtolnay/anyhow/compare/1.0.56...1.0.57)

---
updated-dependencies:
- dependency-name: anyhow
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-15 21:49:48 +02:00
dependabot[bot] 75460bb9b4 Bump serde_json from 1.0.79 to 1.0.80
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.79 to 1.0.80.
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.79...v1.0.80)

---
updated-dependencies:
- dependency-name: serde_json
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-15 21:49:39 +02:00
dependabot[bot] 97163ce830 Bump clap from 3.1.7 to 3.1.14
Bumps [clap](https://github.com/clap-rs/clap) from 3.1.7 to 3.1.14.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v3.1.7...v3.1.14)

---
updated-dependencies:
- dependency-name: clap
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-15 21:49:33 +02:00
David Peter e3bc5b8b25 Simplify test 2022-05-15 21:37:40 +02:00
David Peter 8c05fd030d table_line => table_divider 2022-05-15 21:37:40 +02:00
David Peter 5cd2821266 table_data => table_row 2022-05-15 21:37:40 +02:00
David Peter 7fdb32d202 Remove superfluous newline 2022-05-15 21:37:40 +02:00
David Peter 339f6661ce Move determine_unit_from_results 2022-05-15 21:37:40 +02:00
David Peter 4cdf2d5152 Implement Exporter for all MarkupExporters 2022-05-15 21:37:40 +02:00
David Peter 9a46bcc390 Remove the TestExporter 2022-05-15 21:37:40 +02:00
David Peter 6fe7738165 Do not pass 'unit' into determine_unit_from_results 2022-05-15 21:37:40 +02:00
David Peter 8741d05d11 Move Exporter::unit => determine_unit_from_results 2022-05-15 21:37:40 +02:00
Philipp Paulweber 69d38a5f8e Refactor MarkupExporter to omit intermediate table result creation and simplify MarkupFormatter emitting
* see: https://github.com/sharkdp/hyperfine/pull/490#pullrequestreview-932424652
2022-05-15 21:37:40 +02:00
Philipp Paulweber 9dc87ac349 Refactor MarkdownExporter emitting using a generic MarkupFormatter trait
* see: https://github.com/sharkdp/hyperfine/pull/490#issuecomment-1076784074
2022-05-15 21:37:40 +02:00
Philipp Paulweber 50cf7de915 Refactor Markdown table emitting to generic Markup function 2022-05-15 21:37:40 +02:00
Philipp Paulweber 3b1fc32626 Refactor Markdown table contents generation to generic Markup function 2022-05-15 21:37:40 +02:00
Philipp Paulweber 23d9fe2595 Refactor Markdown with Markup by using generic table emitter 2022-05-15 21:37:40 +02:00
8 changed files with 547 additions and 106 deletions

No files matched your search

+1 -1
View File
@@ -60,7 +60,7 @@ jobs:
- { target: x86_64-apple-darwin , os: macos-10.15 }
- { target: x86_64-pc-windows-gnu , os: windows-2019 }
- { target: x86_64-pc-windows-msvc , os: windows-2019 }
- { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04 }
- { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04, use-cross: true }
- { target: x86_64-unknown-linux-musl , os: ubuntu-20.04, use-cross: true }
steps:
- name: Checkout source code
+1
View File
@@ -2,6 +2,7 @@
## Features
- Add Emacs org-mode as a new export format, see #491 (@ppaulweber)
## Changes
Generated
+22 -16
View File
@@ -13,9 +13,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.56"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27"
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
[[package]]
name = "approx"
@@ -98,15 +98,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.1.7"
version = "3.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c67e7973e74896f4bba06ca2dcfd28d54f9cb8c035e940a32b88ed48f5f5ecf2"
checksum = "535434c063ced786eb04aaf529308092c5ab60889e8fe24275d15de07b01fa97"
dependencies = [
"atty",
"bitflags",
"clap_lex",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"terminal_size",
@@ -122,6 +122,15 @@ dependencies = [
"clap",
]
[[package]]
name = "clap_lex"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
@@ -337,9 +346,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.121"
version = "0.2.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
[[package]]
name = "memchr"
@@ -447,9 +456,6 @@ name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "ppv-lite86"
@@ -730,9 +736,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.79"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
checksum = "f972498cf015f7c0746cac89ebe1d6ef10c293b94175a243a2d9442c163d9944"
dependencies = [
"itoa 1.0.1",
"ryu",
@@ -822,18 +828,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.30"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
dependencies = [
"proc-macro2",
"quote",
+7
View File
@@ -241,6 +241,13 @@ fn build_command() -> Command<'static> {
.value_name("FILE")
.help("Export the timing summary statistics as a Markdown table to the given FILE."),
)
.arg(
Arg::new("export-orgmode")
.long("export-orgmode")
.takes_value(true)
.value_name("FILE")
.help("Export the timing summary statistics as a Emacs org-mode table to the given FILE."),
)
.arg(
Arg::new("show-output")
.long("show-output")
+67 -89
View File
@@ -1,88 +1,60 @@
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::benchmark::relative_speed::{self, BenchmarkResultWithRelativeSpeed};
use crate::output::format::format_duration_value;
use crate::util::units::Unit;
use crate::export::markup::MarkupExporter;
use anyhow::{anyhow, Result};
use super::markup::Alignment;
#[derive(Default)]
pub struct MarkdownExporter {}
impl Exporter for MarkdownExporter {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = if let Some(unit) = unit {
// Use the given unit for all entries.
unit
} else if let Some(first_result) = results.first() {
// Use the first BenchmarkResult entry to determine the unit for all entries.
format_duration_value(first_result.mean, None).1
} else {
// Default to `Second`.
Unit::Second
};
impl MarkupExporter for MarkdownExporter {
fn table_row(&self, cells: &[&str]) -> String {
format!("| {} |\n", cells.join(" | "))
}
if let Some(annotated_results) = relative_speed::compute(results) {
let mut destination = start_table(unit);
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
format!(
"|{}\n",
cell_aligmnents
.iter()
.map(|a| match a {
Alignment::Left => ":---|",
Alignment::Right => "---:|",
})
.collect::<String>()
)
}
for result in annotated_results {
add_table_row(&mut destination, &result, unit);
}
Ok(destination)
} else {
Err(anyhow!(
"Relative speed comparison is not available for Markdown export."
))
}
fn command(&self, cmd: &str) -> String {
format!("`{}`", cmd)
}
}
fn table_header(unit_short_name: String) -> String {
/// Check Markdown-based data row formatting
#[test]
fn test_markdown_formatter_table_data() {
let formatter = MarkdownExporter::default();
assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
}
/// Check Markdown-based horizontal line formatting
#[test]
fn test_markdown_formatter_table_divider() {
let formatter = MarkdownExporter::default();
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
assert_eq!(divider, "|:---|---:|:---|\n");
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Markdown.
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|:---|---:|---:|---:|---:|\n",
unit = unit_short_name
)
}
fn start_table(unit: Unit) -> Vec<u8> {
table_header(unit.short_name()).bytes().collect()
}
fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, unit: Unit) {
let result = &entry.result;
let mean_str = format_duration_value(result.mean, Some(unit)).0;
let stddev_str = if let Some(stddev) = result.stddev {
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
} else {
"".into()
};
let min_str = format_duration_value(result.min, Some(unit)).0;
let max_str = format_duration_value(result.max, Some(unit)).0;
let rel_str = format!("{:.2}", entry.relative_speed);
let rel_stddev_str = if entry.is_fastest {
"".into()
} else if let Some(stddev) = entry.relative_speed_stddev {
format!(" ± {:.2}", stddev)
} else {
"".into()
};
dest.extend(
format!(
"| `{command}` | {mean}{stddev} | {min} | {max} | {rel}{rel_stddev} |\n",
command = result.command.replace("|", "\\|"),
mean = mean_str,
stddev = stddev_str,
min = min_str,
max = max_str,
rel = rel_str,
rel_stddev = rel_stddev_str,
)
.as_bytes(),
);
}
/// Ensure the markdown output includes the table header and the multiple
/// benchmark results as a table. The list of actual times is not included
/// in the output.
@@ -91,6 +63,8 @@ fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResultWithRelativeSpeed, u
/// the units for all entries when the time unit is not given.
#[test]
fn test_markdown_format_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
@@ -123,23 +97,24 @@ fn test_markdown_format_ms() {
},
];
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
",
table_header("ms".to_string())
cfg_test_table_header("ms".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// This (again) demonstrates that the first entry's units (s) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_markdown_format_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
@@ -172,22 +147,24 @@ fn test_markdown_format_s() {
},
];
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let formatted_expected = format!(
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
table_header("s".to_string())
cfg_test_table_header("s".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// The given time unit (s) is used to set the units for all entries.
#[test]
fn test_markdown_format_time_unit_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
@@ -220,28 +197,30 @@ fn test_markdown_format_time_unit_s() {
},
];
let formatted = String::from_utf8(
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
let expect = format!(
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
",
table_header("s".to_string())
cfg_test_table_header("s".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
/// This (again) demonstrates that the given time unit (ms) is used to set
/// the units for all entries.
#[test]
fn test_markdown_format_time_unit_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = MarkdownExporter::default();
@@ -274,20 +253,19 @@ fn test_markdown_format_time_unit_ms() {
},
];
let formatted = String::from_utf8(
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond))
.unwrap(),
)
.unwrap();
let formatted_expected = format!(
let expect = format!(
"{}\
| `sleep 2` | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
",
table_header("ms".to_string())
cfg_test_table_header("ms".to_string())
);
assert_eq!(formatted_expected, formatted);
assert_eq!(expect, actual);
}
+272
View File
@@ -0,0 +1,272 @@
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
use crate::output::format::format_duration_value;
use crate::util::units::Unit;
use super::Exporter;
use anyhow::{anyhow, Result};
pub enum Alignment {
Left,
Right,
}
pub trait MarkupExporter {
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
// prepare table header strings
let notation = format!("[{}]", unit.short_name());
// emit header
let mut table = self.table_row(&[
"Command",
&format!("Mean {}", notation),
&format!("Min {}", notation),
&format!("Max {}", notation),
"Relative",
]);
// emit horizontal line
table.push_str(&self.table_divider(&[
Alignment::Left,
Alignment::Right,
Alignment::Right,
Alignment::Right,
Alignment::Right,
]));
for entry in entries {
let measurement = &entry.result;
// prepare data row strings
let cmd_str = measurement.command.replace('|', "\\|");
let mean_str = format_duration_value(measurement.mean, Some(unit)).0;
let stddev_str = if let Some(stddev) = measurement.stddev {
format!(" ± {}", format_duration_value(stddev, Some(unit)).0)
} else {
"".into()
};
let min_str = format_duration_value(measurement.min, Some(unit)).0;
let max_str = format_duration_value(measurement.max, Some(unit)).0;
let rel_str = format!("{:.2}", entry.relative_speed);
let rel_stddev_str = if entry.is_fastest {
"".into()
} else if let Some(stddev) = entry.relative_speed_stddev {
format!(" ± {:.2}", stddev)
} else {
"".into()
};
// prepare table row entries
table.push_str(&self.table_row(&[
&self.command(&cmd_str),
&format!("{}{}", mean_str, stddev_str),
&min_str,
&max_str,
&format!("{}{}", rel_str, rel_stddev_str),
]))
}
table
}
fn table_row(&self, cells: &[&str]) -> String;
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String;
fn command(&self, size: &str) -> String;
}
fn determine_unit_from_results(results: &[BenchmarkResult]) -> Unit {
if let Some(first_result) = results.first() {
// Use the first BenchmarkResult entry to determine the unit for all entries.
format_duration_value(first_result.mean, None).1
} else {
// Default to `Second`.
Unit::Second
}
}
impl<T: MarkupExporter> Exporter for T {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let entries = relative_speed::compute(results);
if entries.is_none() {
return Err(anyhow!(
"Relative speed comparison is not available for markup exporter."
));
}
let table = self.table_results(&entries.unwrap(), unit);
Ok(table.as_bytes().to_vec())
}
}
/// Check unit resolving for timing results and given unit 's'
#[test]
fn test_determine_unit_from_results_unit_given_s() {
use std::collections::BTreeMap;
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = Some(Unit::Second);
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results and given unit 'ms'
#[test]
fn test_determine_unit_from_results_unit_given_ms() {
use std::collections::BTreeMap;
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = Some(Unit::MilliSecond);
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results using the first result entry as 's'
#[test]
fn test_determine_unit_from_results_unit_first_s() {
use std::collections::BTreeMap;
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for timing results using the first result entry as 'ms'
#[test]
fn test_determine_unit_from_results_unit_first_ms() {
use std::collections::BTreeMap;
let results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
}
/// Check unit resolving for not timing results and no given unit defaulting to 's'
#[test]
fn test_determine_unit_from_results_unit_default_s() {
let results: Vec<BenchmarkResult> = vec![];
let unit = None;
let markup_actual = unit.unwrap_or_else(|| determine_unit_from_results(&results));
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
}
+8
View File
@@ -5,11 +5,14 @@ mod asciidoc;
mod csv;
mod json;
mod markdown;
mod markup;
mod orgmode;
use self::asciidoc::AsciidocExporter;
use self::csv::CsvExporter;
use self::json::JsonExporter;
use self::markdown::MarkdownExporter;
use self::orgmode::OrgmodeExporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
@@ -31,6 +34,9 @@ pub enum ExportType {
/// Markdown table
Markdown,
/// Emacs org-mode tables
Orgmode,
}
/// Interface for different exporters.
@@ -66,6 +72,7 @@ impl ExportManager {
add_exporter("export-json", ExportType::Json)?;
add_exporter("export-csv", ExportType::Csv)?;
add_exporter("export-markdown", ExportType::Markdown)?;
add_exporter("export-orgmode", ExportType::Orgmode)?;
}
Ok(export_manager)
}
@@ -80,6 +87,7 @@ impl ExportManager {
ExportType::Csv => Box::new(CsvExporter::default()),
ExportType::Json => Box::new(JsonExporter::default()),
ExportType::Markdown => Box::new(MarkdownExporter::default()),
ExportType::Orgmode => Box::new(OrgmodeExporter::default()),
};
self.exporters.push(ExporterWithFilename {
exporter,
+169
View File
@@ -0,0 +1,169 @@
use super::markup::Alignment;
use crate::export::markup::MarkupExporter;
#[derive(Default)]
pub struct OrgmodeExporter {}
impl MarkupExporter for OrgmodeExporter {
fn table_row(&self, cells: &[&str]) -> String {
format!(
"| {} | {} |\n",
cells.first().unwrap(),
&cells[1..].join(" | ")
)
}
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
format!("|{}--|\n", "--+".repeat(cell_aligmnents.len() - 1))
}
fn command(&self, cmd: &str) -> String {
format!("={}=", cmd)
}
}
/// Check Emacs org-mode data row formatting
#[test]
fn test_orgmode_formatter_table_data() {
let exporter = OrgmodeExporter::default();
let actual = exporter.table_row(&["a", "b", "c"]);
let expect = "| a | b | c |\n";
assert_eq!(expect, actual);
}
/// Check Emacs org-mode horizontal line formatting
#[test]
fn test_orgmode_formatter_table_line() {
let exporter = OrgmodeExporter::default();
let actual = exporter.table_divider(&[
Alignment::Left,
Alignment::Left,
Alignment::Left,
Alignment::Left,
Alignment::Left,
]);
let expect = "|--+--+--+--+--|\n";
assert_eq!(expect, actual);
}
/// Test helper function to create unit-based header and horizontal line
/// independently from the markup functionality for Emacs org-mode.
#[cfg(test)]
fn cfg_test_table_header(unit_short_name: String) -> String {
format!(
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n",
unit = unit_short_name
)
}
/// Ensure the Emacs org-mode output includes the table header and the multiple
/// benchmark results as a table. The list of actual times is not included
/// in the output.
///
/// This also demonstrates that the first entry's units (ms) are used to set
/// the units for all entries when the time unit is not given.
#[test]
fn test_orgmode_format_ms() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use std::collections::BTreeMap;
let exporter = OrgmodeExporter::default();
let results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(exporter.serialize(&results, None).unwrap()).unwrap();
let expect = format!(
"{}\
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
",
cfg_test_table_header("ms".to_string())
);
assert_eq!(expect, actual);
}
/// This test demonstrates that the given unit (s) is used to set
/// the units for all entries.
#[test]
fn test_orgmode_format_s() {
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::util::units::Unit;
use std::collections::BTreeMap;
let exporter = OrgmodeExporter::default();
let results = vec![
BenchmarkResult {
command: String::from("sleep 2"),
mean: 2.0050,
stddev: Some(0.0020),
median: 2.0050,
user: 0.0009,
system: 0.0012,
min: 2.0020,
max: 2.0080,
times: Some(vec![2.0, 2.0, 2.0]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual =
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
let expect = format!(
"{}\
| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}