Store typed Measurements in BenchmarkResult

This commit is contained in:
David Peter committed 2026-10-05 12:46:10 +02:00
1 parent 05de077d8e
commit b23372c5e2
7 files changed
+432 -257

No files matched your search

+75 -40
View File
@@ -1,6 +1,11 @@
use std::collections::BTreeMap;
use serde::Serialize;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use crate::benchmark::measurement::Measurements;
use crate::quantity::{byte, second, Time};
use crate::util::exit_code::extract_exit_code;
/// Parameter value and whether it was used in the command line template
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
@@ -11,52 +16,82 @@ pub struct Parameter {
pub is_unused: bool,
}
/// Set of values that will be exported.
// NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the
// `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate.
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
/// Meta data and performance metrics for a single benchmark
#[derive(Debug, Default, Clone, PartialEq)]
pub struct BenchmarkResult {
/// The full command line of the program that is being benchmarked
pub command: String,
/// The full command line of the program that is being benchmarked, possibly including a list of
/// parameters that were not used in the command line template.
#[serde(skip_serializing)]
/// The full command line, including parameters not used in the command template.
pub command_with_unused_parameters: String,
/// The average run time
pub mean: f64,
/// The standard deviation of all run times. Not available if only one run has been performed
pub stddev: Option<f64>,
/// The median run time
pub median: f64,
/// Time spent in user mode
pub user: f64,
/// Time spent in kernel mode
pub system: f64,
/// Minimum of all measured times
pub min: f64,
/// Maximum of all measured times
pub max: f64,
/// All run time measurements
#[serde(skip_serializing_if = "Option::is_none")]
pub times: Option<Vec<f64>>,
/// Maximum memory usage of the process, in bytes
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_usage_byte: Option<Vec<u64>>,
/// Exit codes of all command invocations
pub exit_codes: Vec<Option<i32>>,
/// Performance measurements and exit statuses for each run
pub measurements: Measurements,
/// Parameter values for this benchmark
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub parameters: BTreeMap<String, Parameter>,
}
impl BenchmarkResult {
/// The average wall clock time
pub fn mean_wall_clock_time(&self) -> Time {
self.measurements.time_wall_clock_mean()
}
}
// Preserve the existing export format while storing typed measurements internally.
impl Serialize for BenchmarkResult {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct(
"BenchmarkResult",
if self.parameters.is_empty() { 11 } else { 12 },
)?;
state.serialize_field("command", &self.command)?;
state.serialize_field("mean", &self.mean_wall_clock_time().get::<second>())?;
state.serialize_field(
"stddev",
&self.measurements.stddev().map(|time| time.get::<second>()),
)?;
state.serialize_field("median", &self.measurements.median().get::<second>())?;
state.serialize_field("user", &self.measurements.time_user_mean().get::<second>())?;
state.serialize_field(
"system",
&self.measurements.time_system_mean().get::<second>(),
)?;
state.serialize_field("min", &self.measurements.min().get::<second>())?;
state.serialize_field("max", &self.measurements.max().get::<second>())?;
state.serialize_field(
"times",
&self
.measurements
.wall_clock_times()
.map(|time| time.get::<second>())
.collect::<Vec<_>>(),
)?;
state.serialize_field(
"memory_usage_byte",
&self
.measurements
.measurements
.iter()
.map(|measurement| measurement.peak_memory_usage.get::<byte>() as u64)
.collect::<Vec<_>>(),
)?;
state.serialize_field(
"exit_codes",
&self
.measurements
.measurements
.iter()
.map(|measurement| extract_exit_code(measurement.exit_status))
.collect::<Vec<_>>(),
)?;
if !self.parameters.is_empty() {
state.serialize_field("parameters", &self.parameters)?;
}
state.end()
}
}