csv.rs
This file
Path
src/export/csv.rs
225 lines · 8.5 KiB · Rust
Last changed by
Add --env for benchmark environment overridesDavid Peterbefore adoption6dddb02
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f91d0c point at it.
Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 1–4why, one step back1use csv::WriterBuilder;23use super::Exporter;4use crate::benchmark::benchmark_result::BenchmarkResult;Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 5why, one step back5use crate::metric::MetricSelection;Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 6why, one step back6Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
line 7why, one step back7use anyhow::Result;Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 8–12why, one step back89#[derive(Default)]10pub struct CsvExporter {}1112impl Exporter for CsvExporter {Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 13–26why, one step back13 fn serialize(14 &self,15 results: &[BenchmarkResult],16 metrics: &[MetricSelection],17 ) -> Result<Vec<u8>> {18 // Omit metrics unavailable for all commands (possible with `--metrics all`).19 let metrics: Vec<_> = metrics20 .iter()21 .filter(|selection| {22 results23 .iter()24 .any(|result| selection.metric.summarize(&result.measurements).is_some())25 })26 .collect();Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 27why, one step back27 let mut writer = WriterBuilder::new().from_writer(vec![]);Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 28–34why, one step back28 let mut headers = vec!["command".to_owned()];29 for selection in &metrics {30 headers.extend(31 ["unit", "mean", "stddev", "median", "min", "max"]32 .map(|field| format!("{}_{field}", selection.metric.name())),33 );34 }Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 35–41why, one step back35 if let Some(result) = results.first() {36 headers.extend(37 result38 .parameters39 .keys()40 .map(|name| format!("parameter_{name}")),41 );Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 42why, one step back42 }Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 43why, one step back43 writer.write_record(headers)?;Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 44why, one step back44Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 45why, one step back45 for result in results {Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 46–67why, one step back46 let mut fields = vec![result.get_name().to_owned()];47 for selection in &metrics {48 // CSV never chooses units automatically: an omitted suffix means base units.49 let unit = selection.csv_unit();50 let stats = selection.metric.summarize(&result.measurements);51 fields.push(unit.symbol.to_owned());52 // Preserve numeric precision rather than using rounded display values.53 fields.extend(54 [55 stats.map(|stats| stats.mean),56 stats.and_then(|stats| stats.stddev),57 stats.map(|stats| stats.median),58 stats.map(|stats| stats.min),59 stats.map(|stats| stats.max),60 ]61 .map(|value| {62 value63 .map(|value| (value / unit.scale).to_string())64 .unwrap_or_default()65 }),66 );67 }Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 68–73why, one step back68 fields.extend(69 result70 .parameters71 .values()72 .map(|parameter| parameter.value.clone()),73 );Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 74–82why, one step back74 writer.write_record(fields)?;75 }7677 Ok(writer.into_inner()?)78 }79}8081#[test]82fn test_csv() {Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
line 83why, one step back83 use crate::benchmark::benchmark_result::Parameter;Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 84–86why, one step back84 use crate::benchmark::measurement::{Measurement, Measurements};85 use crate::quantity::{byte, second, Information, Time, Zero};86Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 87why, one step back87 use std::collections::BTreeMap;Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 88–89why, one step back88 use std::process::ExitStatus;89Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 90–93why, one step back90 let exporter = CsvExporter::default();9192 let results = vec![93 BenchmarkResult {Add --env for benchmark environment overridesDavid Peterbefore adoption6dddb02
line 94why, one step back94 environment: BTreeMap::new(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 95–97why, one step back95 command: String::from("echo command_a"),96 name: Some(String::from("command_a")),97 display_name: String::from("command_a"),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 98–100why, one step back98 measurements: Measurements::new(vec![99 Measurement {100 time_wall_clock: Time::new::<second>(7.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 101why, one step back101 time_cpu: Time::new::<second>(7.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 102–103why, one step back102 time_user: Time::new::<second>(7.0),103 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 104why, one step back104 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 105why, one step back105 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 106–109why, one step back106 exit_status: ExitStatus::default(),107 },108 Measurement {109 time_wall_clock: Time::new::<second>(8.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 110why, one step back110 time_cpu: Time::new::<second>(8.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 111–112why, one step back111 time_user: Time::new::<second>(8.0),112 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 113why, one step back113 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 114why, one step back114 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 115–118why, one step back115 exit_status: ExitStatus::default(),116 },117 Measurement {118 time_wall_clock: Time::new::<second>(12.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 119why, one step back119 time_cpu: Time::new::<second>(12.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 120–121why, one step back120 time_user: Time::new::<second>(12.0),121 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 122why, one step back122 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 123why, one step back123 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 124–126why, one step back124 exit_status: ExitStatus::default(),125 },126 ]),Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 127–128why, one step back127 parameters: {128 let mut params = BTreeMap::new();Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
lines 129–140why, one step back129 params.insert(130 "foo".into(),131 Parameter {132 value: "one".into(),133 },134 );135 params.insert(136 "bar".into(),137 Parameter {138 value: "two".into(),139 },140 );Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 141–144why, one step back141 params142 },143 },144 BenchmarkResult {Add --env for benchmark environment overridesDavid Peterbefore adoption6dddb02
line 145why, one step back145 environment: BTreeMap::new(),Simplify CSV testsDavid Peterbefore adoption0d2e3a4
line 146why, one step back146 command: String::from("command_b"),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 147–148why, one step back147 name: None,148 display_name: String::from("command_b"),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 149–151why, one step back149 measurements: Measurements::new(vec![150 Measurement {151 time_wall_clock: Time::new::<second>(17.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 152why, one step back152 time_cpu: Time::new::<second>(17.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 153–154why, one step back153 time_user: Time::new::<second>(17.0),154 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 155why, one step back155 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 156why, one step back156 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 157–160why, one step back157 exit_status: ExitStatus::default(),158 },159 Measurement {160 time_wall_clock: Time::new::<second>(18.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 161why, one step back161 time_cpu: Time::new::<second>(18.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 162–163why, one step back162 time_user: Time::new::<second>(18.0),163 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 164why, one step back164 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 165why, one step back165 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 166–169why, one step back166 exit_status: ExitStatus::default(),167 },168 Measurement {169 time_wall_clock: Time::new::<second>(19.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 170why, one step back170 time_cpu: Time::new::<second>(19.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 171–172why, one step back171 time_user: Time::new::<second>(19.0),172 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 173why, one step back173 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 174why, one step back174 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 175–177why, one step back175 exit_status: ExitStatus::default(),176 },177 ]),Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 178–179why, one step back178 parameters: {179 let mut params = BTreeMap::new();Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
lines 180–191why, one step back180 params.insert(181 "foo".into(),182 Parameter {183 value: "one".into(),184 },185 );186 params.insert(187 "bar".into(),188 Parameter {189 value: "seven".into(),190 },191 );Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 192–195why, one step back192 params193 },194 },195 ];Move more tests to instaDavid Peterbefore adoptionda399ab
lines 196–197why, one step back196197 let actual = String::from_utf8(Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 198why, one step back198 exporterExport selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 199–202why, one step back199 .serialize(200 &results,201 &MetricSelection::parse_list("time_wall_clock,memory_peak_resident").unwrap(),202 )Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
lines 203–206why, one step back203 .unwrap(),204 )205 .unwrap();206Move more tests to instaDavid Peterbefore adoptionda399ab
line 207why, one step back207 insta::assert_snapshot!(actual, @r#"Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 208–210why, one step back208 command,time_wall_clock_unit,time_wall_clock_mean,time_wall_clock_stddev,time_wall_clock_median,time_wall_clock_min,time_wall_clock_max,memory_peak_resident_unit,memory_peak_resident_mean,memory_peak_resident_stddev,memory_peak_resident_median,memory_peak_resident_min,memory_peak_resident_max,parameter_bar,parameter_foo209 command_a,s,9,2.6457513110645907,8,7,12,B,1024,0,1024,1024,1024,two,one210 command_b,s,18,1,18,17,19,B,1024,0,1024,1024,1024,seven,oneMove more tests to instaDavid Peterbefore adoptionda399ab
line 211why, one step back211 "#);Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 212–217why, one step back212213 for (selection, unit, mean) in [214 ("memory_peak_resident", "B", "1024"),215 ("memory_peak_resident:KiB", "KiB", "1"),216 ] {217 let output = exporterExport selected metrics side by side in CSVDavid Peterbefore adoption067ee70
line 218why, one step back218 .serialize(&results, &MetricSelection::parse_list(selection).unwrap())Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 219–221why, one step back219 .unwrap();220 let mut reader = csv::Reader::from_reader(output.as_slice());221 let row = reader.records().next().unwrap().unwrap();Export selected metrics side by side in CSVDavid Peterbefore adoption067ee70
lines 222–223why, one step back222 assert_eq!(&row[1], unit);223 assert_eq!(&row[2], mean);Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 224why, one step back224 }Bump serde from 1.0.174 to 1.0.179dependabot[bot]before adoption0f91d0c
line 225why, one step back225}