use anyhow::{Context, Result};
use serde::Serialize;
use std::path::Path;
#[derive(Debug, Serialize)]
pub struct Export {
pub results: Vec<ExportResult>,
}
#[derive(Debug, Serialize)]
pub struct ExportResult {
pub command: String,
pub bench: String,
pub subject: String,
pub mean: f64,
pub stddev: Option<f64>,
pub median: f64,
pub min: f64,
pub max: f64,
pub times: Vec<f64>,
pub exit_codes: Vec<i32>,
}
impl ExportResult {
pub fn new(bench: &str, subject: &str, command: &str, samples_ms: &[f64]) -> Self {
let times: Vec<f64> = samples_ms.iter().map(|ms| ms / 1000.0).collect();
let mut sorted = times.clone();
sorted.sort_by(f64::total_cmp);
let n = sorted.len();
let mean = sorted.iter().sum::<f64>() / n as f64;
let stddev = (n > 1).then(|| {
(sorted.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1) as f64).sqrt()
});
let median = if n.is_multiple_of(2) {
(sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
} else {
sorted[n / 2]
};
ExportResult {
command: command.to_string(),
bench: bench.to_string(),
subject: subject.to_string(),
mean,
stddev,
median,
min: sorted[0],
max: sorted[n - 1],
exit_codes: vec![0; n],
times,
}
}
}
pub fn write(path: &Path, results: Vec<ExportResult>) -> Result<()> {
let json = serde_json::to_string_pretty(&Export { results })?;
std::fs::write(path, json + "\n").with_context(|| format!("could not write {}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn times_are_seconds_and_the_median_is_the_true_median() {
let r = ExportResult::new("b", "s", "s", &[4000.0, 1000.0, 3000.0, 2000.0]);
assert_eq!(r.times, [4.0, 1.0, 3.0, 2.0], "kept in the order taken");
assert_eq!((r.min, r.max, r.mean, r.median), (1.0, 4.0, 2.5, 2.5));
assert_eq!(r.exit_codes, [0; 4]);
assert!(r.stddev.unwrap() > 0.0);
}
#[test]
fn a_single_sample_has_no_stddev() {
let r = ExportResult::new("b", "s", "s", &[5.0]);
assert_eq!(r.stddev, None);
let json = serde_json::to_value(&r).unwrap();
assert!(json["stddev"].is_null());
}
}