Skip to content
Myr

Always compute relative speed comprison for export, closes #642

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent 926b8d0 · Browse files · 3 files+62 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+62 added−38 removed

  • other:+62 added−38 removed

3 files+62 added−38 removed

modified:src/benchmark/relative_speed.rs+59 added-30 removed
13 unmodified lines
14pub fn compare_mean_time(l: &BenchmarkResult, r: &BenchmarkResult) -> Ordering {15 l.mean.partial_cmp(&r.mean).unwrap_or(Ordering::Equal)16}1718pub fn compute(results: &[BenchmarkResult]) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {19 let fastest: &BenchmarkResult = results18fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {19 results20 .iter()21 .min_by(|&l, &r| compare_mean_time(l, r))22 .expect("at least one benchmark result");22 .expect("at least one benchmark result")23}2425fn compute_relative_speeds<'a>(26 results: &'a [BenchmarkResult],27 fastest: &'a BenchmarkResult,28) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {29 results30 .iter()31 .map(|result| {32 let is_fastest = result == fastest;3334 if result.mean == 0.0 {35 return BenchmarkResultWithRelativeSpeed {36 result,37 relative_speed: if is_fastest { 1.0 } else { f64::INFINITY },38 relative_speed_stddev: None,39 is_fastest,40 };41 }4243 let ratio = result.mean / fastest.mean;4445 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas46 // Covariance asssumed to be 0, i.e. variables are assumed to be independent47 let ratio_stddev = match (result.stddev, fastest.stddev) {48 (Some(result_stddev), Some(fastest_stddev)) => Some(49 ratio50 * ((result_stddev / result.mean).powi(2)51 + (fastest_stddev / fastest.mean).powi(2))52 .sqrt(),53 ),54 _ => None,55 };5657 BenchmarkResultWithRelativeSpeed {58 result,59 relative_speed: ratio,60 relative_speed_stddev: ratio_stddev,61 is_fastest,62 }63 })64 .collect()65}6667pub fn compute_with_check(68 results: &[BenchmarkResult],69) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {70 let fastest = fastest_of(results);7172 if fastest.mean == 0.0 {73 return None;74 }7528 Some(29 results30 .iter()31 .map(|result| {32 let ratio = result.mean / fastest.mean;76 Some(compute_relative_speeds(results, fastest))77}7834 // https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas35 // Covariance asssumed to be 0, i.e. variables are assumed to be independent36 let ratio_stddev = match (result.stddev, fastest.stddev) {37 (Some(result_stddev), Some(fastest_stddev)) => Some(38 ratio39 * ((result_stddev / result.mean).powi(2)40 + (fastest_stddev / fastest.mean).powi(2))41 .sqrt(),42 ),43 _ => None,44 };79/// Same as compute_with_check, potentially resulting in relative speeds of infinity80pub fn compute(results: &[BenchmarkResult]) -> Vec<BenchmarkResultWithRelativeSpeed> {81 let fastest = fastest_of(results);8246 BenchmarkResultWithRelativeSpeed {47 result,48 relative_speed: ratio,49 relative_speed_stddev: ratio_stddev,50 is_fastest: result == fastest,51 }52 })53 .collect(),54 )83 compute_relative_speeds(results, fastest)84}8586#[cfg(test)]87fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {
23 unmodified lines
111 create_result("cmd2", 2.0),112 create_result("cmd3", 5.0),113 ];11486 let annotated_results = compute(&results).unwrap();115 let annotated_results = compute_with_check(&results).unwrap();116117 assert_relative_eq!(1.5, annotated_results[0].relative_speed);118 assert_relative_eq!(1.0, annotated_results[1].relative_speed);119 assert_relative_eq!(2.5, annotated_results[2].relative_speed);
2 unmodified lines
122#[test]123fn test_compute_relative_speed_for_zero_times() {124 let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];12597 let annotated_results = compute(&results);126 let annotated_results = compute_with_check(&results);127128 assert!(annotated_results.is_none());129}
modified:src/benchmark/scheduler.rs+1 added-1 removed
59 unmodified lines
60 if self.results.len() < 2 {61 return;62 }6364 if let Some(mut annotated_results) = relative_speed::compute(&self.results) {64 if let Some(mut annotated_results) = relative_speed::compute_with_check(&self.results) {65 annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));6667 let fastest = &annotated_results[0];68 let others = &annotated_results[1..];
33 unmodified lines
modified:src/export/markup.rs+2 added-7 removed
2 unmodified lines
3use crate::output::format::format_duration_value;4use crate::util::units::Unit;56use super::Exporter;7use anyhow::{anyhow, Result};7use anyhow::Result;89pub enum Alignment {10 Left,11 Right,
93 unmodified lines
105impl<T: MarkupExporter> Exporter for T {106 fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {107 let unit = unit.unwrap_or_else(|| determine_unit_from_results(results));108 let entries = relative_speed::compute(results);109 if entries.is_none() {110 return Err(anyhow!(111 "Relative speed comparison is not available for markup exporter."112 ));113 }109115 let table = self.table_results(&entries.unwrap(), unit);110 let table = self.table_results(&entries, unit);111 Ok(table.as_bytes().to_vec())112 }113}114
171 unmodified lines