Files
hyperfine/src/export/csv.rs
T

185 lines
6.5 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
2018-03-20 22:24:57 +01:00
use csv::WriterBuilder;
2021-08-24 21:33:48 +02:00
use super::Exporter;
2022-02-20 13:44:34 +01:00
use crate::benchmark::benchmark_result::BenchmarkResult;
2023-06-03 21:06:50 +02:00
use crate::options::SortOrder;
use crate::quantity::{FormatQuantity, TimeUnit};
2021-08-24 21:33:48 +02:00
2022-02-06 17:44:28 +01:00
use anyhow::Result;
2018-03-20 17:00:24 -04:00
#[derive(Default)]
pub struct CsvExporter {}
2018-03-20 20:26:28 +01:00
impl Exporter for CsvExporter {
2023-06-03 21:06:50 +02:00
fn serialize(
&self,
results: &[BenchmarkResult],
_unit: Option<TimeUnit>,
2023-06-03 21:06:50 +02:00
_sort_order: SortOrder,
) -> Result<Vec<u8>> {
const CSV_UNIT: TimeUnit = TimeUnit::Second;
2026-10-05 11:16:20 +02:00
const CSV_PRECISION: usize = 6;
let mut writer = WriterBuilder::new().from_writer(vec![]);
2018-03-20 22:59:43 +01:00
{
2020-09-17 11:31:27 -07:00
let mut headers: Vec<Cow<[u8]>> = [
2021-02-28 10:22:21 +01:00
// The list of times and exit codes cannot be exported to the CSV file - omit them.
"command", "mean", "stddev", "median", "user", "system", "min", "max",
]
.iter()
2020-09-17 11:31:27 -07:00
.map(|x| Cow::Borrowed(x.as_bytes()))
.collect();
if let Some(res) = results.first() {
for param_name in res.parameters.keys() {
2024-05-31 13:26:35 -07:00
headers.push(Cow::Owned(format!("parameter_{param_name}").into_bytes()));
}
}
writer.write_record(headers)?;
}
for res in results {
2021-08-22 16:05:03 +02:00
let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];
for f in &[
res.mean_wall_clock_time(),
res.measurements.stddev().unwrap_or_default(),
res.measurements.median(),
res.measurements.time_user_mean(),
res.measurements.time_system_mean(),
res.measurements.min(),
res.measurements.max(),
] {
fields.push(Cow::Owned(
f.format_with_precision(CSV_UNIT, CSV_PRECISION)
.into_bytes(),
))
}
for v in res.parameters.values() {
fields.push(Cow::Borrowed(v.value.as_bytes()))
}
writer.write_record(fields)?;
}
2022-02-06 17:44:28 +01:00
Ok(writer.into_inner()?)
}
}
#[test]
fn test_csv() {
use crate::benchmark::benchmark_result::Parameter;
2026-10-05 12:46:10 +02:00
use crate::benchmark::measurement::{Measurement, Measurements};
use crate::quantity::{byte, second, Information, Time, Zero};
use std::collections::BTreeMap;
2026-10-05 12:46:10 +02:00
use std::process::ExitStatus;
let exporter = CsvExporter::default();
let results = vec![
2022-02-06 13:36:09 +01:00
BenchmarkResult {
2024-12-29 13:38:54 +01:00
command: String::from("command_a"),
command_with_unused_parameters: String::from("command_a"),
2026-10-05 12:46:10 +02:00
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(7.0),
time_user: Time::new::<second>(7.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(8.0),
time_user: Time::new::<second>(8.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(12.0),
time_user: Time::new::<second>(12.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
]),
2022-02-06 13:36:09 +01:00
parameters: {
let mut params = BTreeMap::new();
params.insert(
"foo".into(),
Parameter {
value: "one".into(),
is_unused: false,
},
);
params.insert(
"bar".into(),
Parameter {
value: "two".into(),
is_unused: false,
},
);
params
},
2022-02-06 13:36:09 +01:00
},
BenchmarkResult {
2024-12-29 13:38:54 +01:00
command: String::from("command_b"),
command_with_unused_parameters: String::from("command_b"),
2026-10-05 12:46:10 +02:00
measurements: Measurements::new(vec![
Measurement {
time_wall_clock: Time::new::<second>(17.0),
time_user: Time::new::<second>(17.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(18.0),
time_user: Time::new::<second>(18.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
Measurement {
time_wall_clock: Time::new::<second>(19.0),
time_user: Time::new::<second>(19.0),
time_system: Time::zero(),
peak_memory_usage: Information::new::<byte>(1024.),
exit_status: ExitStatus::default(),
},
]),
2022-02-06 13:36:09 +01:00
parameters: {
let mut params = BTreeMap::new();
params.insert(
"foo".into(),
Parameter {
value: "one".into(),
is_unused: false,
},
);
params.insert(
"bar".into(),
Parameter {
value: "seven".into(),
is_unused: false,
},
);
params
},
2022-02-06 13:36:09 +01:00
},
];
2024-12-29 13:10:50 +01:00
let actual = String::from_utf8(
2023-06-03 21:06:50 +02:00
exporter
.serialize(&results, Some(TimeUnit::Second), SortOrder::Command)
2023-06-03 21:06:50 +02:00
.unwrap(),
)
.unwrap();
2024-12-29 13:10:50 +01:00
insta::assert_snapshot!(actual, @r#"
command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo
2026-10-05 12:46:10 +02:00
command_a,9.000000,2.645751,8.000000,9.000000,0.000000,7.000000,12.000000,two,one
command_b,18.000000,1.000000,18.000000,18.000000,0.000000,17.000000,19.000000,seven,one
2024-12-29 13:10:50 +01:00
"#);
}