Add new --sort option

This adds a new `--sort` option to choose the way in which the results
in the speed comparison and the markup exports are ordered.

closes #614
closes #601
This commit is contained in:
David Peter authored and David Peter committed 2023-06-03 21:34:36 +02:00
1 parent 2393de0c36
commit ac617c8714
12 files changed
+285 -89

No files matched your search

+22 -8
View File
@@ -1,7 +1,7 @@
use std::cmp::Ordering;
use super::benchmark_result::BenchmarkResult;
use crate::util::units::Scalar;
use crate::{options::SortOrder, util::units::Scalar};
#[derive(Debug)]
pub struct BenchmarkResultWithRelativeSpeed<'a> {
@@ -25,8 +25,9 @@ fn fastest_of(results: &[BenchmarkResult]) -> &BenchmarkResult {
fn compute_relative_speeds<'a>(
results: &'a [BenchmarkResult],
fastest: &'a BenchmarkResult,
sort_order: SortOrder,
) -> Vec<BenchmarkResultWithRelativeSpeed<'a>> {
results
let mut results: Vec<_> = results
.iter()
.map(|result| {
let is_fastest = result == fastest;
@@ -61,11 +62,21 @@ fn compute_relative_speeds<'a>(
is_fastest,
}
})
.collect()
.collect();
match sort_order {
SortOrder::Command => {}
SortOrder::MeanTime => {
results.sort_unstable_by(|r1, r2| compare_mean_time(r1.result, r2.result));
}
}
results
}
pub fn compute_with_check(
results: &[BenchmarkResult],
sort_order: SortOrder,
) -> Option<Vec<BenchmarkResultWithRelativeSpeed>> {
let fastest = fastest_of(results);
@@ -73,14 +84,17 @@ pub fn compute_with_check(
return None;
}
Some(compute_relative_speeds(results, fastest))
Some(compute_relative_speeds(results, fastest, sort_order))
}
/// Same as compute_with_check, potentially resulting in relative speeds of infinity
pub fn compute(results: &[BenchmarkResult]) -> Vec<BenchmarkResultWithRelativeSpeed> {
pub fn compute(
results: &[BenchmarkResult],
sort_order: SortOrder,
) -> Vec<BenchmarkResultWithRelativeSpeed> {
let fastest = fastest_of(results);
compute_relative_speeds(results, fastest)
compute_relative_speeds(results, fastest, sort_order)
}
#[cfg(test)]
@@ -113,7 +127,7 @@ fn test_compute_relative_speed() {
create_result("cmd3", 5.0),
];
let annotated_results = compute_with_check(&results).unwrap();
let annotated_results = compute_with_check(&results, SortOrder::Command).unwrap();
assert_relative_eq!(1.5, annotated_results[0].relative_speed);
assert_relative_eq!(1.0, annotated_results[1].relative_speed);
@@ -124,7 +138,7 @@ fn test_compute_relative_speed() {
fn test_compute_relative_speed_for_zero_times() {
let results = vec![create_result("cmd1", 1.0), create_result("cmd2", 0.0)];
let annotated_results = compute_with_check(&results);
let annotated_results = compute_with_check(&results, SortOrder::Command);
assert!(annotated_results.is_none());
}
+54 -23
View File
@@ -6,7 +6,7 @@ use super::{relative_speed, Benchmark};
use crate::command::Commands;
use crate::export::ExportManager;
use crate::options::{ExecutorKind, Options, OutputStyleOption};
use crate::options::{ExecutorKind, Options, OutputStyleOption, SortOrder};
use anyhow::Result;
@@ -46,7 +46,11 @@ impl<'a> Scheduler<'a> {
// We export results after each individual benchmark, because
// we would risk losing them if a later benchmark fails.
self.export_manager.write_results(&self.results, true)?;
self.export_manager.write_results(
&self.results,
self.options.sort_order_exports,
true,
)?;
}
Ok(())
@@ -61,29 +65,55 @@ impl<'a> Scheduler<'a> {
return;
}
if let Some(mut annotated_results) = relative_speed::compute_with_check(&self.results) {
annotated_results.sort_by(|l, r| relative_speed::compare_mean_time(l.result, r.result));
if let Some(annotated_results) = relative_speed::compute_with_check(
&self.results,
self.options.sort_order_speed_comparison,
) {
match self.options.sort_order_speed_comparison {
SortOrder::MeanTime => {
println!("{}", "Summary".bold());
let fastest = &annotated_results[0];
let others = &annotated_results[1..];
let fastest = annotated_results.iter().find(|r| r.is_fastest).unwrap();
let others = annotated_results.iter().filter(|r| !r.is_fastest);
println!("{}", "Summary".bold());
println!(
" {} ran",
fastest.result.command_with_unused_parameters.cyan()
);
println!(
" {} ran",
fastest.result.command_with_unused_parameters.cyan()
);
for item in others {
println!(
"{}{} times faster than {}",
format!("{:8.2}", item.relative_speed).bold().green(),
if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:.2}", stddev).green())
} else {
"".into()
},
&item.result.command_with_unused_parameters.magenta()
);
for item in others {
println!(
"{}{} times faster than {}",
format!("{:8.2}", item.relative_speed).bold().green(),
if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:.2}", stddev).green())
} else {
"".into()
},
&item.result.command_with_unused_parameters.magenta()
);
}
}
SortOrder::Command => {
println!("{}", "Relative speed comparison".bold());
for item in annotated_results {
println!(
" {}{} {}",
format!("{:10.2}", item.relative_speed).bold().green(),
if item.is_fastest {
" ".into()
} else {
if let Some(stddev) = item.relative_speed_stddev {
format!(" ± {}", format!("{:5.2}", stddev).green())
} else {
" ".into()
}
},
&item.result.command_with_unused_parameters,
);
}
}
}
} else {
eprintln!(
@@ -99,6 +129,7 @@ impl<'a> Scheduler<'a> {
}
pub fn final_export(&self) -> Result<()> {
self.export_manager.write_results(&self.results, false)
self.export_manager
.write_results(&self.results, self.options.sort_order_exports, false)
}
}