Compare commits

..
Author SHA1 Message Date
David Peter 16f53eb8d6 Format CSV measurements through FormatQuantity 2026-10-05 13:41:43 +02:00
2 changed files with 41 additions and 32 deletions

No files matched your search

+28 -20
View File
@@ -1,10 +1,8 @@
use std::cmp::Ordering;
use super::benchmark_result::BenchmarkResult;
use crate::{
options::SortOrder,
quantity::{self, Ratio, Time, Zero},
};
use crate::options::SortOrder;
use crate::quantity::second;
#[derive(Debug)]
pub struct BenchmarkResultWithRelativeSpeed<'a> {
@@ -18,7 +16,8 @@ pub struct BenchmarkResultWithRelativeSpeed<'a> {
pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
l.mean_wall_clock_time()
.partial_cmp(&r.mean_wall_clock_time())
.get::<second>()
.partial_cmp(&r.mean_wall_clock_time().get::<second>())
.unwrap_or(Ordering::Equal)
}
@@ -40,7 +39,7 @@ fn compute_relative_speeds<'a>(
let is_reference = result == reference;
let relative_ordering = compare_mean_time(result, reference);
if result.mean_wall_clock_time() == Time::zero() {
if result.mean_wall_clock_time().get::<second>() == 0.0 {
return BenchmarkResultWithRelativeSpeed {
result,
relative_speed: if is_reference { 1.0 } else { f64::INFINITY },
@@ -51,25 +50,34 @@ fn compute_relative_speeds<'a>(
}
let ratio = match relative_ordering {
Ordering::Less => reference.mean_wall_clock_time() / result.mean_wall_clock_time(),
Ordering::Equal => Ratio::new::<quantity::ratio>(1.0),
Ordering::Less => {
reference.mean_wall_clock_time().get::<second>()
/ result.mean_wall_clock_time().get::<second>()
}
Ordering::Equal => 1.0,
Ordering::Greater => {
result.mean_wall_clock_time() / reference.mean_wall_clock_time()
result.mean_wall_clock_time().get::<second>()
/ reference.mean_wall_clock_time().get::<second>()
}
};
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas
// Covariance assumed to be 0, i.e. variables are assumed to be independent
let ratio_stddev = match (
result.measurements.stddev(),
reference.measurements.stddev(),
result
.measurements
.stddev()
.map(|time| time.get::<second>()),
reference
.measurements
.stddev()
.map(|time| time.get::<second>()),
) {
(Some(result_stddev), Some(fastest_stddev)) => Some(
ratio
* ((result_stddev / result.mean_wall_clock_time())
.powi(uom::typenum::P2::new())
+ (fastest_stddev / reference.mean_wall_clock_time())
.powi(uom::typenum::P2::new()))
* ((result_stddev / result.mean_wall_clock_time().get::<second>()).powi(2)
+ (fastest_stddev / reference.mean_wall_clock_time().get::<second>())
.powi(2))
.sqrt(),
),
_ => None,
@@ -77,8 +85,8 @@ fn compute_relative_speeds<'a>(
BenchmarkResultWithRelativeSpeed {
result,
relative_speed: ratio.get::<quantity::ratio>(),
relative_speed_stddev: ratio_stddev.map(|r| r.get::<quantity::ratio>()),
relative_speed: ratio,
relative_speed_stddev: ratio_stddev,
is_reference,
relative_ordering,
}
@@ -100,8 +108,8 @@ pub fn compute_with_check_from_reference<'a>(
reference: &'a BenchmarkResult,
sort_order: SortOrder,
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'a>>> {
if fastest_of(results).mean_wall_clock_time() == Time::zero()
|| reference.mean_wall_clock_time() == Time::zero()
if fastest_of(results).mean_wall_clock_time().get::<second>() == 0.0
|| reference.mean_wall_clock_time().get::<second>() == 0.0
{
return None;
}
@@ -115,7 +123,7 @@ pub fn compute_with_check(
) -> Option<Vec<BenchmarkResultWithRelativeSpeed<'_>>> {
let fastest = fastest_of(results);
if fastest.mean_wall_clock_time() == Time::zero() {
if fastest.mean_wall_clock_time().get::<second>() == 0.0 {
return None;
}
+13 -12
View File
@@ -5,7 +5,7 @@ use csv::WriterBuilder;
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::options::SortOrder;
use crate::quantity::{second, TimeUnit};
use crate::quantity::{FormatQuantity, TimeUnit};
use anyhow::Result;
@@ -19,6 +19,7 @@ impl Exporter for CsvExporter {
_unit: Option<TimeUnit>,
_sort_order: SortOrder,
) -> Result<Vec<u8>> {
const CSV_UNIT: TimeUnit = TimeUnit::Second;
const CSV_PRECISION: usize = 6;
let mut writer = WriterBuilder::new().from_writer(vec![]);
@@ -42,18 +43,18 @@ impl Exporter for CsvExporter {
for res in results {
let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];
for f in &[
res.mean_wall_clock_time().get::<second>(),
res.measurements
.stddev()
.unwrap_or_default()
.get::<second>(),
res.measurements.median().get::<second>(),
res.measurements.time_user_mean().get::<second>(),
res.measurements.time_system_mean().get::<second>(),
res.measurements.min().get::<second>(),
res.measurements.max().get::<second>(),
res.mean_wall_clock_time(),
res.measurements.stddev().unwrap_or_default(),
res.measurements.median(),
res.measurements.time_user_mean(),
res.measurements.time_system_mean(),
res.measurements.min(),
res.measurements.max(),
] {
fields.push(Cow::Owned(format!("{f:.CSV_PRECISION$}").into_bytes()))
fields.push(Cow::Owned(
f.format_with_precision(CSV_UNIT, CSV_PRECISION)
.into_bytes(),
))
}
for v in res.parameters.values() {
fields.push(Cow::Borrowed(v.value.as_bytes()))