csv.rs
This file
Path
src/export/csv.rs
185 lines · 6.5 KiB · Rust
Last changed by
Merge pull request #972 from sharkdp/codex/csv-quantity-formattingDavid Peterbefore adoption683885a
History of this fileTake it
1use std::borrow::Cow;23use csv::WriterBuilder;45use super::Exporter;6use crate::benchmark::benchmark_result::BenchmarkResult;7use crate::options::SortOrder;8use crate::quantity::{FormatQuantity, TimeUnit};910use anyhow::Result;1112#[derive(Default)]13pub struct CsvExporter {}1415impl Exporter for CsvExporter {16 fn serialize(17 &self,18 results: &[BenchmarkResult],19 _unit: Option<TimeUnit>,20 _sort_order: SortOrder,21 ) -> Result<Vec<u8>> {22 const CSV_UNIT: TimeUnit = TimeUnit::Second;23 const CSV_PRECISION: usize = 6;2425 let mut writer = WriterBuilder::new().from_writer(vec![]);2627 {28 let mut headers: Vec<Cow<[u8]>> = [29 // The list of times and exit codes cannot be exported to the CSV file - omit them.30 "command", "mean", "stddev", "median", "user", "system", "min", "max",31 ]32 .iter()33 .map(|x| Cow::Borrowed(x.as_bytes()))34 .collect();35 if let Some(res) = results.first() {36 for param_name in res.parameters.keys() {37 headers.push(Cow::Owned(format!("parameter_{param_name}").into_bytes()));38 }39 }40 writer.write_record(headers)?;41 }4243 for res in results {44 let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];45 for f in &[46 res.mean_wall_clock_time(),47 res.measurements.stddev().unwrap_or_default(),48 res.measurements.median(),49 res.measurements.time_user_mean(),50 res.measurements.time_system_mean(),51 res.measurements.min(),52 res.measurements.max(),53 ] {54 fields.push(Cow::Owned(55 f.format_with_precision(CSV_UNIT, CSV_PRECISION)56 .into_bytes(),57 ))58 }59 for v in res.parameters.values() {60 fields.push(Cow::Borrowed(v.value.as_bytes()))61 }62 writer.write_record(fields)?;63 }6465 Ok(writer.into_inner()?)66 }67}6869#[test]70fn test_csv() {71 use crate::benchmark::benchmark_result::Parameter;72 use crate::benchmark::measurement::{Measurement, Measurements};73 use crate::quantity::{byte, second, Information, Time, Zero};7475 use std::collections::BTreeMap;76 use std::process::ExitStatus;7778 let exporter = CsvExporter::default();7980 let results = vec![81 BenchmarkResult {82 command: String::from("command_a"),83 command_with_unused_parameters: String::from("command_a"),84 measurements: Measurements::new(vec![85 Measurement {86 time_wall_clock: Time::new::<second>(7.0),87 time_user: Time::new::<second>(7.0),88 time_system: Time::zero(),89 peak_memory_usage: Information::new::<byte>(1024.),90 exit_status: ExitStatus::default(),91 },92 Measurement {93 time_wall_clock: Time::new::<second>(8.0),94 time_user: Time::new::<second>(8.0),95 time_system: Time::zero(),96 peak_memory_usage: Information::new::<byte>(1024.),97 exit_status: ExitStatus::default(),98 },99 Measurement {100 time_wall_clock: Time::new::<second>(12.0),101 time_user: Time::new::<second>(12.0),102 time_system: Time::zero(),103 peak_memory_usage: Information::new::<byte>(1024.),104 exit_status: ExitStatus::default(),105 },106 ]),107 parameters: {108 let mut params = BTreeMap::new();109 params.insert(110 "foo".into(),111 Parameter {112 value: "one".into(),113 is_unused: false,114 },115 );116 params.insert(117 "bar".into(),118 Parameter {119 value: "two".into(),120 is_unused: false,121 },122 );123 params124 },125 },126 BenchmarkResult {127 command: String::from("command_b"),128 command_with_unused_parameters: String::from("command_b"),129 measurements: Measurements::new(vec![130 Measurement {131 time_wall_clock: Time::new::<second>(17.0),132 time_user: Time::new::<second>(17.0),133 time_system: Time::zero(),134 peak_memory_usage: Information::new::<byte>(1024.),135 exit_status: ExitStatus::default(),136 },137 Measurement {138 time_wall_clock: Time::new::<second>(18.0),139 time_user: Time::new::<second>(18.0),140 time_system: Time::zero(),141 peak_memory_usage: Information::new::<byte>(1024.),142 exit_status: ExitStatus::default(),143 },144 Measurement {145 time_wall_clock: Time::new::<second>(19.0),146 time_user: Time::new::<second>(19.0),147 time_system: Time::zero(),148 peak_memory_usage: Information::new::<byte>(1024.),149 exit_status: ExitStatus::default(),150 },151 ]),152 parameters: {153 let mut params = BTreeMap::new();154 params.insert(155 "foo".into(),156 Parameter {157 value: "one".into(),158 is_unused: false,159 },160 );161 params.insert(162 "bar".into(),163 Parameter {164 value: "seven".into(),165 is_unused: false,166 },167 );168 params169 },170 },171 ];172173 let actual = String::from_utf8(174 exporter175 .serialize(&results, Some(TimeUnit::Second), SortOrder::Command)176 .unwrap(),177 )178 .unwrap();179180 insta::assert_snapshot!(actual, @r#"181 command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo182 command_a,9.000000,2.645751,8.000000,9.000000,0.000000,7.000000,12.000000,two,one183 command_b,18.000000,1.000000,18.000000,18.000000,0.000000,17.000000,19.000000,seven,one184 "#);185}