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

+10 -3
View File
@@ -76,6 +76,9 @@ fn cfg_test_table_header(unit_short_name: &str) -> String {
)
}
#[cfg(test)]
use crate::options::SortOrder;
#[cfg(test)]
use crate::util::units::Unit;
@@ -132,8 +135,12 @@ fn test_asciidoc_format_s() {
},
];
let actual =
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}
| `FOO=1 BAR=2 command \\| 1`
@@ -205,7 +212,7 @@ fn test_asciidoc_format_ms() {
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::MilliSecond))
.serialize(&results, Some(Unit::MilliSecond), SortOrder::Command)
.unwrap(),
)
.unwrap();
+13 -3
View File
@@ -4,6 +4,7 @@ use csv::WriterBuilder;
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::options::SortOrder;
use crate::util::units::Unit;
use anyhow::Result;
@@ -12,7 +13,12 @@ use anyhow::Result;
pub struct CsvExporter {}
impl Exporter for CsvExporter {
fn serialize(&self, results: &[BenchmarkResult], _unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(
&self,
results: &[BenchmarkResult],
_unit: Option<Unit>,
_sort_order: SortOrder,
) -> Result<Vec<u8>> {
let mut writer = WriterBuilder::new().from_writer(vec![]);
{
@@ -105,8 +111,12 @@ fn test_csv() {
FOO=one BAR=seven command | 2,11,12,11,13,14,15,16.5,seven,one\n\
",
);
let gens =
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
let gens = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
assert_eq!(exps, gens);
}
+7 -1
View File
@@ -3,6 +3,7 @@ use serde_json::to_vec_pretty;
use super::Exporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::options::SortOrder;
use crate::util::units::Unit;
use anyhow::Result;
@@ -16,7 +17,12 @@ struct HyperfineSummary<'a> {
pub struct JsonExporter {}
impl Exporter for JsonExporter {
fn serialize(&self, results: &[BenchmarkResult], _unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(
&self,
results: &[BenchmarkResult],
_unit: Option<Unit>,
_sort_order: SortOrder,
) -> Result<Vec<u8>> {
let mut output = to_vec_pretty(&HyperfineSummary { results });
if let Ok(ref mut content) = output {
content.push(b'\n');
+61 -28
View File
@@ -28,6 +28,9 @@ impl MarkupExporter for MarkdownExporter {
}
}
#[cfg(test)]
use crate::options::SortOrder;
/// Check Markdown-based data row formatting
#[test]
fn test_markdown_formatter_table_data() {
@@ -99,7 +102,12 @@ fn test_markdown_format_ms() {
},
];
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let actual = String::from_utf8(
exporter
.serialize(&timing_results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
@@ -151,7 +159,12 @@ fn test_markdown_format_s() {
},
];
let actual = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
let actual = String::from_utf8(
exporter
.serialize(&timing_results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
@@ -173,20 +186,6 @@ fn test_markdown_format_time_unit_s() {
let exporter = MarkdownExporter::default();
let timing_results = vec![
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 2"),
command_with_unused_parameters: String::from("sleep 2"),
@@ -201,23 +200,57 @@ fn test_markdown_format_time_unit_s() {
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
BenchmarkResult {
command: String::from("sleep 0.1"),
command_with_unused_parameters: String::from("sleep 0.1"),
mean: 0.1057,
stddev: Some(0.0016),
median: 0.1057,
user: 0.0009,
system: 0.0011,
min: 0.1023,
max: 0.1080,
times: Some(vec![0.1, 0.1, 0.1]),
exit_codes: vec![Some(0), Some(0), Some(0)],
parameters: BTreeMap::new(),
},
];
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second))
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
{
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
",
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
}
{
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second), SortOrder::MeanTime)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
| `sleep 2` | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
",
cfg_test_table_header("s".to_string())
);
cfg_test_table_header("s".to_string())
);
assert_eq!(expect, actual);
assert_eq!(expect, actual);
}
}
/// This (again) demonstrates that the given time unit (ms) is used to set
@@ -263,7 +296,7 @@ fn test_markdown_format_time_unit_ms() {
let actual = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond))
.serialize(&timing_results, Some(Unit::MilliSecond), SortOrder::Command)
.unwrap(),
)
.unwrap();
+8 -2
View File
@@ -1,5 +1,6 @@
use crate::benchmark::relative_speed::BenchmarkResultWithRelativeSpeed;
use crate::benchmark::{benchmark_result::BenchmarkResult, relative_speed};
use crate::options::SortOrder;
use crate::output::format::format_duration_value;
use crate::util::units::Unit;
@@ -105,9 +106,14 @@ fn determine_unit_from_results(results: &[BenchmarkResult]) -> Unit {
}
impl<T: MarkupExporter> Exporter for T {
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>> {
fn serialize(
&self,
results: &[BenchmarkResult],
unit: Option<Unit>,
sort_order: SortOrder,
) -> Result<Vec<u8>> {
let unit = unit.unwrap_or_else(|| determine_unit_from_results(results));
let entries = relative_speed::compute(results);
let entries = relative_speed::compute(results, sort_order);
let table = self.table_results(&entries, unit);
Ok(table.as_bytes().to_vec())
+14 -3
View File
@@ -15,6 +15,7 @@ use self::markdown::MarkdownExporter;
use self::orgmode::OrgmodeExporter;
use crate::benchmark::benchmark_result::BenchmarkResult;
use crate::options::SortOrder;
use crate::util::units::Unit;
use anyhow::{Context, Result};
@@ -42,7 +43,12 @@ pub enum ExportType {
/// Interface for different exporters.
trait Exporter {
/// Export the given entries in the serialized form.
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;
fn serialize(
&self,
results: &[BenchmarkResult],
unit: Option<Unit>,
sort_order: SortOrder,
) -> Result<Vec<u8>>;
}
pub enum ExportTarget {
@@ -116,9 +122,14 @@ impl ExportManager {
/// results are written to all file targets (to always have them up to date, even
/// if a benchmark fails). In the latter case, we only print to stdout targets (in
/// order not to clutter the output of hyperfine with intermediate results).
pub fn write_results(&self, results: &[BenchmarkResult], intermediate: bool) -> Result<()> {
pub fn write_results(
&self,
results: &[BenchmarkResult],
sort_order: SortOrder,
intermediate: bool,
) -> Result<()> {
for e in &self.exporters {
let content = || e.exporter.serialize(results, self.time_unit);
let content = || e.exporter.serialize(results, self.time_unit, sort_order);
match e.target {
ExportTarget::File(ref filename) => {
+15 -3
View File
@@ -22,6 +22,9 @@ impl MarkupExporter for OrgmodeExporter {
}
}
#[cfg(test)]
use crate::options::SortOrder;
/// Check Emacs org-mode data row formatting
#[test]
fn test_orgmode_formatter_table_data() {
@@ -105,7 +108,12 @@ fn test_orgmode_format_ms() {
},
];
let actual = String::from_utf8(exporter.serialize(&results, None).unwrap()).unwrap();
let actual = String::from_utf8(
exporter
.serialize(&results, None, SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
@@ -159,8 +167,12 @@ fn test_orgmode_format_s() {
},
];
let actual =
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
let actual = String::from_utf8(
exporter
.serialize(&results, Some(Unit::Second), SortOrder::Command)
.unwrap(),
)
.unwrap();
let expect = format!(
"{}\
| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |