mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-02 17:11:28 +00:00
Implement Exporter for all MarkupExporters
This commit is contained in:
1 parent
9a46bcc390
commit
4cdf2d5152
2 files changed
+35
-31
No files matched your search
+15
-30
@@ -1,15 +1,9 @@
|
|||||||
use super::{determine_unit_from_results, Exporter};
|
use crate::export::markup::MarkupExporter;
|
||||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
|
||||||
use crate::benchmark::relative_speed;
|
|
||||||
use crate::export::markup::MarkupFormatter;
|
|
||||||
use crate::util::units::Unit;
|
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MarkdownFormatter;
|
pub struct MarkdownExporter {}
|
||||||
|
|
||||||
impl MarkupFormatter for MarkdownFormatter {
|
impl MarkupExporter for MarkdownExporter {
|
||||||
fn table_data(&self, data: &[&str]) -> String {
|
fn table_data(&self, data: &[&str]) -> String {
|
||||||
format!("| {} |\n", data.join(" | "))
|
format!("| {} |\n", data.join(" | "))
|
||||||
}
|
}
|
||||||
@@ -23,29 +17,10 @@ impl MarkupFormatter for MarkdownFormatter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct MarkdownExporter {}
|
|
||||||
|
|
||||||
impl Exporter for MarkdownExporter {
|
|
||||||
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 Markdown export."
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let formatter = MarkdownFormatter::default();
|
|
||||||
let table = formatter.table_results(&entries.unwrap(), unit);
|
|
||||||
Ok(table.as_bytes().to_vec())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check Markdown-based data row formatting
|
/// Check Markdown-based data row formatting
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_formatter_table_data() {
|
fn test_markdown_formatter_table_data() {
|
||||||
let formatter = MarkdownFormatter::default();
|
let formatter = MarkdownExporter::default();
|
||||||
let data = vec!["a", "b", "c"];
|
let data = vec!["a", "b", "c"];
|
||||||
|
|
||||||
let actual = formatter.table_data(&data);
|
let actual = formatter.table_data(&data);
|
||||||
@@ -57,7 +32,7 @@ fn test_markdown_formatter_table_data() {
|
|||||||
/// Check Markdown-based horizontal line formatting
|
/// Check Markdown-based horizontal line formatting
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_formatter_table_line() {
|
fn test_markdown_formatter_table_line() {
|
||||||
let formatter = MarkdownFormatter::default();
|
let formatter = MarkdownExporter::default();
|
||||||
let size = 5;
|
let size = 5;
|
||||||
|
|
||||||
let actual = formatter.table_line(size);
|
let actual = formatter.table_line(size);
|
||||||
@@ -84,6 +59,8 @@ fn cfg_test_table_header(unit_short_name: String) -> String {
|
|||||||
/// the units for all entries when the time unit is not given.
|
/// the units for all entries when the time unit is not given.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_format_ms() {
|
fn test_markdown_format_ms() {
|
||||||
|
use super::Exporter;
|
||||||
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
let exporter = MarkdownExporter::default();
|
let exporter = MarkdownExporter::default();
|
||||||
|
|
||||||
@@ -132,6 +109,8 @@ fn test_markdown_format_ms() {
|
|||||||
/// the units for all entries when the time unit is not given.
|
/// the units for all entries when the time unit is not given.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_format_s() {
|
fn test_markdown_format_s() {
|
||||||
|
use super::Exporter;
|
||||||
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
let exporter = MarkdownExporter::default();
|
let exporter = MarkdownExporter::default();
|
||||||
|
|
||||||
@@ -179,6 +158,9 @@ fn test_markdown_format_s() {
|
|||||||
/// The given time unit (s) is used to set the units for all entries.
|
/// The given time unit (s) is used to set the units for all entries.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_format_time_unit_s() {
|
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;
|
use std::collections::BTreeMap;
|
||||||
let exporter = MarkdownExporter::default();
|
let exporter = MarkdownExporter::default();
|
||||||
|
|
||||||
@@ -232,6 +214,9 @@ fn test_markdown_format_time_unit_s() {
|
|||||||
/// the units for all entries.
|
/// the units for all entries.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_markdown_format_time_unit_ms() {
|
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;
|
use std::collections::BTreeMap;
|
||||||
let exporter = MarkdownExporter::default();
|
let exporter = MarkdownExporter::default();
|
||||||
|
|
||||||
|
|||||||
+20
-1
@@ -1,8 +1,12 @@
|
|||||||
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
|
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
|
||||||
|
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
|
||||||
use crate::output::format::format_duration_value;
|
use crate::output::format::format_duration_value;
|
||||||
use crate::util::units::Unit;
|
use crate::util::units::Unit;
|
||||||
|
|
||||||
pub trait MarkupFormatter {
|
use super::{determine_unit_from_results, Exporter};
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
|
||||||
|
pub trait MarkupExporter {
|
||||||
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
|
fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String {
|
||||||
// prepare table header strings
|
// prepare table header strings
|
||||||
let notation = format!("[{}]", unit.short_name());
|
let notation = format!("[{}]", unit.short_name());
|
||||||
@@ -60,3 +64,18 @@ pub trait MarkupFormatter {
|
|||||||
|
|
||||||
fn command(&self, size: &str) -> String;
|
fn command(&self, size: &str) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user