csv.rs
This file
Path
src/export/csv.rs
112 lines · 3.6 KiB · Rust
Last changed by
Show unused parameters in command namesDavid Peterbefore adoptiona875276
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ffc6d7 point at it.
Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 1–64why, one step back1use std::borrow::Cow;23use csv::WriterBuilder;45use super::Exporter;6use crate::benchmark::benchmark_result::BenchmarkResult;7use crate::util::units::Unit;89use anyhow::Result;1011#[derive(Default)]12pub struct CsvExporter {}1314impl Exporter for CsvExporter {15 fn serialize(&self, results: &[BenchmarkResult], _unit: Option<Unit>) -> Result<Vec<u8>> {16 let mut writer = WriterBuilder::new().from_writer(vec![]);1718 {19 let mut headers: Vec<Cow<[u8]>> = [20 // The list of times and exit codes cannot be exported to the CSV file - omit them.21 "command", "mean", "stddev", "median", "user", "system", "min", "max",22 ]23 .iter()24 .map(|x| Cow::Borrowed(x.as_bytes()))25 .collect();26 if let Some(res) = results.first() {27 for param_name in res.parameters.keys() {28 headers.push(Cow::Owned(format!("parameter_{}", param_name).into_bytes()));29 }30 }31 writer.write_record(headers)?;32 }3334 for res in results {35 let mut fields = vec![Cow::Borrowed(res.command.as_bytes())];36 for f in &[37 res.mean,38 res.stddev.unwrap_or(0.0),39 res.median,40 res.user,41 res.system,42 res.min,43 res.max,44 ] {45 fields.push(Cow::Owned(f.to_string().into_bytes()))46 }47 for v in res.parameters.values() {48 fields.push(Cow::Borrowed(v.as_bytes()))49 }50 writer.write_record(fields)?;51 }5253 Ok(writer.into_inner()?)54 }55}5657#[test]58fn test_csv() {59 use std::collections::BTreeMap;60 let exporter = CsvExporter::default();6162 let results = vec![63 BenchmarkResult {64 command: String::from("FOO=one BAR=two command | 1"),Show unused parameters in command namesDavid Peterbefore adoptiona875276
line 65why, one step back65 command_with_unused_parameters: String::from("FOO=one BAR=two command | 1"),Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 66–83why, one step back66 mean: 1.0,67 stddev: Some(2.0),68 median: 1.0,69 user: 3.0,70 system: 4.0,71 min: 5.0,72 max: 6.0,73 times: Some(vec![7.0, 8.0, 9.0]),74 exit_codes: vec![Some(0), Some(0), Some(0)],75 parameters: {76 let mut params = BTreeMap::new();77 params.insert("foo".into(), "one".into());78 params.insert("bar".into(), "two".into());79 params80 },81 },82 BenchmarkResult {83 command: String::from("FOO=one BAR=seven command | 2"),Show unused parameters in command namesDavid Peterbefore adoptiona875276
line 84why, one step back84 command_with_unused_parameters: String::from("FOO=one BAR=seven command | 2"),Move to timer moduleDavid Peterbefore adoption0ffc6d7
lines 85–112why, one step back85 mean: 11.0,86 stddev: Some(12.0),87 median: 11.0,88 user: 13.0,89 system: 14.0,90 min: 15.0,91 max: 16.5,92 times: Some(vec![17.0, 18.0, 19.0]),93 exit_codes: vec![Some(0), Some(0), Some(0)],94 parameters: {95 let mut params = BTreeMap::new();96 params.insert("foo".into(), "one".into());97 params.insert("bar".into(), "seven".into());98 params99 },100 },101 ];102 let exps: String = String::from(103 "command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo\n\104 FOO=one BAR=two command | 1,1,2,1,3,4,5,6,two,one\n\105 FOO=one BAR=seven command | 2,11,12,11,13,14,15,16.5,seven,one\n\106 ",107 );108 let gens =109 String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();110111 assert_eq!(exps, gens);112}