2021-08-24 22:12:42 +02:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
2022-02-20 13:44:34 +01:00
|
|
|
use super::benchmark_result::BenchmarkResult;
|
2023-06-03 21:06:50 +02:00
|
|
|
use crate::{options::SortOrder, util::units::Scalar};
|
2021-08-24 22:12:42 +02:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct BenchmarkResultWithRelativeSpeed<'a> {
|
|
|
|
|
pub result: &'a BenchmarkResult,
|
|
|
|
|
pub relative_speed: Scalar,
|
2021-11-14 10:18:28 +01:00
|
|
|
pub relative_speed_stddev: Option<Scalar>,
|
2021-08-24 22:12:42 +02:00
|
|
|
pub is_fastest: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {
|
|
|
|
|
l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 12:43:08 +02:00
|
|
|
fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {
|
|
|
|
|
results
|
2021-08-24 22:12:42 +02:00
|
|
|
.iter()
|
|
|
|
|
.min_by(|&l, &r| compare_mean_time(l, r))
|
2023-04-20 12:43:08 +02:00
|
|
|
.expect("at least one benchmark result")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compute_relative_speeds<'a>(
|
|
|
|
|
results: &'a [BenchmarkResult],
|
|
|
|
|
fastest: &'a BenchmarkResult,
|
2023-06-03 21:06:50 +02:00
|
|
|
sort_order: SortOrder,
|
2023-04-20 12:43:08 +02:00
|
|
|
) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {
|
2023-06-03 21:06:50 +02:00
|
|
|
let mut results: Vec<_> = results
|
2023-04-20 12:43:08 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|result| {
|
|
|
|
|
let is_fastest = result == fastest;
|
|
|
|
|
|
|
|
|
|
if result.mean == 0.0 {
|
|
|
|
|
return BenchmarkResultWithRelativeSpeed {
|
|
|
|
|
result,
|
|
|
|
|
relative_speed: if is_fastest { 1.0 } else { f64::INFINITY },
|
|
|
|
|
relative_speed_stddev: None,
|
|
|
|
|
is_fastest,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ratio = result.mean / fastest.mean;
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas
|
|
|
|
|
// Covariance asssumed to be 0, i.e. variables are assumed to be independent
|
|
|
|
|
let ratio_stddev = match (result.stddev, fastest.stddev) {
|
|
|
|
|
(Some(result_stddev), Some(fastest_stddev)) => Some(
|
|
|
|
|
ratio
|
|
|
|
|
* ((result_stddev / result.mean).powi(2)
|
|
|
|
|
+ (fastest_stddev / fastest.mean).powi(2))
|
|
|
|
|
.sqrt(),
|
|
|
|
|
),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BenchmarkResultWithRelativeSpeed {
|
|
|
|
|
result,
|
|
|
|
|
relative_speed: ratio,
|
|
|
|
|
relative_speed_stddev: ratio_stddev,
|
|
|
|
|
is_fastest,
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-06-03 21:06:50 +02:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
match sort_order {
|
|
|
|
|
SortOrder::Command => {}
|
|
|
|
|
SortOrder::MeanTime => {
|
|
|
|
|
results.sort_unstable_by(|r1, r2| compare_mean_time(r1.result, r2.result));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results
|
2023-04-20 12:43:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn compute_with_check(
|
|
|
|
|
results: &[BenchmarkResult],
|
2023-06-03 21:06:50 +02:00
|
|
|
sort_order: SortOrder,
|
2023-04-20 12:43:08 +02:00
|
|
|
) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {
|
|
|
|
|
let fastest = fastest_of(results);
|
2021-08-24 22:12:42 +02:00
|
|
|
|
|
|
|
|
if fastest.mean == 0.0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 21:06:50 +02:00
|
|
|
Some(compute_relative_speeds(results, fastest, sort_order))
|
2023-04-20 12:43:08 +02:00
|
|
|
}
|
2021-08-24 22:12:42 +02:00
|
|
|
|
2023-04-20 12:43:08 +02:00
|
|
|
/// Same as compute_with_check, potentially resulting in relative speeds of infinity
|
2023-06-03 21:06:50 +02:00
|
|
|
pub fn compute(
|
|
|
|
|
results: &[BenchmarkResult],
|
|
|
|
|
sort_order: SortOrder,
|
|
|
|
|
) -> Vec<BenchmarkResultWithRelativeSpeed> {
|
2023-04-20 12:43:08 +02:00
|
|
|
let fastest = fastest_of(results);
|
2021-08-24 22:12:42 +02:00
|
|
|
|
2023-06-03 21:06:50 +02:00
|
|
|
compute_relative_speeds(results, fastest, sort_order)
|
2021-08-24 22:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command: name.into(),
|
2023-04-20 21:32:06 +02:00
|
|
|
command_with_unused_parameters: name.into(),
|
2021-08-24 22:12:42 +02:00
|
|
|
mean,
|
2021-11-14 10:18:28 +01:00
|
|
|
stddev: Some(1.0),
|
2021-08-24 22:12:42 +02:00
|
|
|
median: mean,
|
|
|
|
|
user: mean,
|
|
|
|
|
system: 0.0,
|
|
|
|
|
min: mean,
|
|
|
|
|
max: mean,
|
|
|
|
|
times: None,
|
|
|
|
|
exit_codes: Vec::new(),
|
|
|
|
|
parameters: BTreeMap::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_compute_relative_speed() {
|
|
|
|
|
use approx::assert_relative_eq;
|
|
|
|
|
|
|
|
|
|
let results = vec![
|
|
|
|
|
create_result("cmd1", 3.0),
|
|
|
|
|
create_result("cmd2", 2.0),
|
|
|
|
|
create_result("cmd3", 5.0),
|
|
|
|
|
];
|
|
|
|
|
|
2023-06-03 21:06:50 +02:00
|
|
|
let annotated_results = compute_with_check(&results, SortOrder::Command).unwrap();
|
2021-08-24 22:12:42 +02:00
|
|
|
|
|
|
|
|
assert_relative_eq!(1.5, annotated_results[0].relative_speed);
|
|
|
|
|
assert_relative_eq!(1.0, annotated_results[1].relative_speed);
|
|
|
|
|
assert_relative_eq!(2.5, annotated_results[2].relative_speed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_compute_relative_speed_for_zero_times() {
|
|
|
|
|
let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];
|
|
|
|
|
|
2023-06-03 21:06:50 +02:00
|
|
|
let annotated_results = compute_with_check(&results, SortOrder::Command);
|
2021-08-24 22:12:42 +02:00
|
|
|
|
|
|
|
|
assert!(annotated_results.is_none());
|
|
|
|
|
}
|