csv.rs
This file
Path
src/export/csv.rs
122 lines · 3.7 KiB · Rust
Last changed by
Add new --sort optionDavid Peterbefore adoptionac617c8
History of this fileTake it
The walk stopped after 200 commits: lines older than 308bf5c point at it.
Extract common functionalityDavid Peterbefore adoption308bf5c
lines 1–6why, one step back1use std::borrow::Cow;23use csv::WriterBuilder;45use super::Exporter;6use crate::benchmark::benchmark_result::BenchmarkResult;Add new --sort optionDavid Peterbefore adoptionac617c8
line 7why, one step back7use crate::options::SortOrder;Extract common functionalityDavid Peterbefore adoption308bf5c
lines 8–15why, one step back8use crate::util::units::Unit;910use anyhow::Result;1112#[derive(Default)]13pub struct CsvExporter {}1415impl Exporter for CsvExporter {Add new --sort optionDavid Peterbefore adoptionac617c8
lines 16–21why, one step back16 fn serialize(17 &self,18 results: &[BenchmarkResult],19 _unit: Option<Unit>,20 _sort_order: SortOrder,21 ) -> Result<Vec<u8>> {Extract common functionalityDavid Peterbefore adoption308bf5c
lines 22–70why, one step back22 let mut writer = WriterBuilder::new().from_writer(vec![]);2324 {25 let mut headers: Vec<Cow<[u8]>> = [26 // The list of times and exit codes cannot be exported to the CSV file - omit them.27 "command", "mean", "stddev", "median", "user", "system", "min", "max",28 ]29 .iter()30 .map(|x| Cow::Borrowed(x.as_bytes()))31 .collect();32 if let Some(res) = results.first() {33 for param_name in res.parameters.keys() {34 headers.push(Cow::Owned(format!("parameter_{}", param_name).into_bytes()));35 }36 }37 writer.write_record(headers)?;38 }3940 for res in results {41 let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];42 for f in &[43 res.mean,44 res.stddev.unwrap_or(0.0),45 res.median,46 res.user,47 res.system,48 res.min,49 res.max,50 ] {51 fields.push(Cow::Owned(f.to_string().into_bytes()))52 }53 for v in res.parameters.values() {54 fields.push(Cow::Borrowed(v.as_bytes()))55 }56 writer.write_record(fields)?;57 }5859 Ok(writer.into_inner()?)60 }61}6263#[test]64fn test_csv() {65 use std::collections::BTreeMap;66 let exporter = CsvExporter::default();6768 let results = vec![69 BenchmarkResult {70 command: String::from("FOO=one BAR=two command | 1"),Show unused parameters in command namesDavid Peterbefore adoptiona875276
line 71why, one step back71 command_with_unused_parameters: String::from("FOO=one BAR=two command | 1"),Extract common functionalityDavid Peterbefore adoption308bf5c
lines 72–89why, one step back72 mean: 1.0,73 stddev: Some(2.0),74 median: 1.0,75 user: 3.0,76 system: 4.0,77 min: 5.0,78 max: 6.0,79 times: Some(vec![7.0, 8.0, 9.0]),80 exit_codes: vec![Some(0), Some(0), Some(0)],81 parameters: {82 let mut params = BTreeMap::new();83 params.insert("foo".into(), "one".into());84 params.insert("bar".into(), "two".into());85 params86 },87 },88 BenchmarkResult {89 command: String::from("FOO=one BAR=seven command | 2"),Show unused parameters in command namesDavid Peterbefore adoptiona875276
line 90why, one step back90 command_with_unused_parameters: String::from("FOO=one BAR=seven command | 2"),Extract common functionalityDavid Peterbefore adoption308bf5c
lines 91–113why, one step back91 mean: 11.0,92 stddev: Some(12.0),93 median: 11.0,94 user: 13.0,95 system: 14.0,96 min: 15.0,97 max: 16.5,98 times: Some(vec![17.0, 18.0, 19.0]),99 exit_codes: vec![Some(0), Some(0), Some(0)],100 parameters: {101 let mut params = BTreeMap::new();102 params.insert("foo".into(), "one".into());103 params.insert("bar".into(), "seven".into());104 params105 },106 },107 ];108 let exps: String = String::from(109 "command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo\n\110 FOO=one BAR=two command | 1,1,2,1,3,4,5,6,two,one\n\111 FOO=one BAR=seven command | 2,11,12,11,13,14,15,16.5,seven,one\n\112 ",113 );Add new --sort optionDavid Peterbefore adoptionac617c8
lines 114–119why, one step back114 let gens = String::from_utf8(115 exporter116 .serialize(&results, Some(Unit::Second), SortOrder::Command)117 .unwrap(),118 )119 .unwrap();Extract common functionalityDavid Peterbefore adoption308bf5c
lines 120–122why, one step back120121 assert_eq!(exps, gens);122}