Move Exporter::unit => determine_unit_from_results

This commit is contained in:
David Peter 2022-05-15 20:41:55 +02:00
parent dda9b97078
commit 8cd5b1f6bc
2 changed files with 20 additions and 25 deletions

View File

@ -1,4 +1,4 @@
use super::Exporter;
use super::{determine_unit_from_results, Exporter};
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::benchmark::relative_speed;
use crate::export::markup::MarkupFormatter;
@ -28,7 +28,7 @@ pub struct MarkdownExporter {}
impl Exporter for MarkdownExporter {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
let unit = self.unit(results, unit);
let unit = determine_unit_from_results(results, unit);
let entries = relative_speed::compute(results);
if entries.is_none() {
return Err(anyhow!(

View File

@ -19,6 +19,19 @@ use crate::util::units::Unit;
use anyhow::{Context, Result};
use clap::ArgMatches;
pub fn determine_unit_from_results(results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {
return 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
};
}
/// The desired form of exporter to use for a given file.
#[derive(Clone)]
pub enum ExportType {
@ -39,19 +52,6 @@ pub enum ExportType {
trait Exporter {
/// Export the given entries in the serialized form.
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;
fn unit(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Unit {
return 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
};
}
}
struct ExporterWithFilename {
@ -170,8 +170,7 @@ fn test_markup_table_unit_given_s() {
];
let unit = Some(Unit::Second);
let exporter = TestExporter::default();
let markup_actual = exporter.unit(&results, unit);
let markup_actual = determine_unit_from_results(&results, unit);
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
@ -211,8 +210,7 @@ fn test_markup_table_unit_given_ms() {
];
let unit = Some(Unit::MilliSecond);
let exporter = TestExporter::default();
let markup_actual = exporter.unit(&results, unit);
let markup_actual = determine_unit_from_results(&results, unit);
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
@ -252,8 +250,7 @@ fn test_markup_table_unit_first_s() {
];
let unit = None;
let exporter = TestExporter::default();
let markup_actual = exporter.unit(&results, unit);
let markup_actual = determine_unit_from_results(&results, unit);
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);
@ -293,8 +290,7 @@ fn test_markup_table_unit_first_ms() {
];
let unit = None;
let exporter = TestExporter::default();
let markup_actual = exporter.unit(&results, unit);
let markup_actual = determine_unit_from_results(&results, unit);
let markup_expected = Unit::MilliSecond;
assert_eq!(markup_expected, markup_actual);
@ -306,8 +302,7 @@ fn test_markup_table_unit_default_s() {
let results: Vec<BenchmarkResult> = vec![];
let unit = None;
let exporter = TestExporter::default();
let markup_actual = exporter.unit(&results, unit);
let markup_actual = determine_unit_from_results(&results, unit);
let markup_expected = Unit::Second;
assert_eq!(markup_expected, markup_actual);