csv.rs
This file
Path
src/export/csv.rs
218 lines · 7.9 KiB · Rust
Last changed by
Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
History of this fileTake it
The walk stopped after 200 commits: lines older than ab5ae5f point at it.
Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
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 clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 6why, one step back6Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 7why, one step back7use anyhow::{Context, Result};Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 8–12why, one step back89#[derive(Default)]10pub struct CsvExporter {}1112impl Exporter for CsvExporter {Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 13–15why, one step back13 fn serialize(&self, results: &[BenchmarkResult], primary: MetricSelection) -> Result<Vec<u8>> {14 // CSV never chooses units automatically: an omitted suffix means base units.15 let unit = primary.csv_unit();Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 16why, one step back16 let mut writer = WriterBuilder::new().from_writer(vec![]);Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 17–30why, one step back17 let mut headers: Vec<String> = [18 "command", "metric", "unit", "mean", "stddev", "median", "min", "max",19 ]20 .iter()21 .copied()22 .map(str::to_owned)23 .collect();24 if let Some(result) = results.first() {25 headers.extend(26 result27 .parameters28 .keys()29 .map(|name| format!("parameter_{name}")),30 );Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 31why, one step back31 }Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 32why, one step back32 writer.write_record(headers)?;Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 33why, one step back33Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 34–70why, one step back34 for result in results {35 let stats = primary36 .metric37 .summarize(&result.measurements)38 .with_context(|| {39 format!(40 "Metric '{}' is unavailable for '{}'",41 primary.metric.name(),42 result.get_name()43 )44 })?;45 let mut fields = vec![46 result.get_name().to_owned(),47 primary.metric.name().to_owned(),48 unit.symbol.to_owned(),49 ];50 // Preserve numeric precision rather than using rounded display values.51 fields.extend(52 [53 Some(stats.mean),54 stats.stddev,55 Some(stats.median),56 Some(stats.min),57 Some(stats.max),58 ]59 .map(|value| {60 value61 .map(|value| (value / unit.scale).to_string())62 .unwrap_or_default()63 }),64 );65 fields.extend(66 result67 .parameters68 .values()69 .map(|parameter| parameter.value.clone()),70 );Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 71–79why, one step back71 writer.write_record(fields)?;72 }7374 Ok(writer.into_inner()?)75 }76}7778#[test]79fn test_csv() {Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
line 80why, one step back80 use crate::benchmark::benchmark_result::Parameter;Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 81–83why, one step back81 use crate::benchmark::measurement::{Measurement, Measurements};82 use crate::quantity::{byte, second, Information, Time, Zero};83Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 84why, one step back84 use std::collections::BTreeMap;Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 85–86why, one step back85 use std::process::ExitStatus;86Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 87–90why, one step back87 let exporter = CsvExporter::default();8889 let results = vec![90 BenchmarkResult {Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 91–93why, one step back91 command: String::from("echo command_a"),92 name: Some(String::from("command_a")),93 display_name: String::from("command_a"),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 94–96why, one step back94 measurements: Measurements::new(vec![95 Measurement {96 time_wall_clock: Time::new::<second>(7.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 97why, one step back97 time_cpu: Time::new::<second>(7.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 98–99why, one step back98 time_user: Time::new::<second>(7.0),99 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 100why, one step back100 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 101why, one step back101 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 102–105why, one step back102 exit_status: ExitStatus::default(),103 },104 Measurement {105 time_wall_clock: Time::new::<second>(8.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 106why, one step back106 time_cpu: Time::new::<second>(8.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 107–108why, one step back107 time_user: Time::new::<second>(8.0),108 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 109why, one step back109 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 110why, one step back110 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 111–114why, one step back111 exit_status: ExitStatus::default(),112 },113 Measurement {114 time_wall_clock: Time::new::<second>(12.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 115why, one step back115 time_cpu: Time::new::<second>(12.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 116–117why, one step back116 time_user: Time::new::<second>(12.0),117 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 118why, one step back118 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 119why, one step back119 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 120–122why, one step back120 exit_status: ExitStatus::default(),121 },122 ]),Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 123–124why, one step back123 parameters: {124 let mut params = BTreeMap::new();Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
lines 125–136why, one step back125 params.insert(126 "foo".into(),127 Parameter {128 value: "one".into(),129 },130 );131 params.insert(132 "bar".into(),133 Parameter {134 value: "two".into(),135 },136 );Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 137–140why, one step back137 params138 },139 },140 BenchmarkResult {Simplify CSV testsDavid Peterbefore adoption0d2e3a4
line 141why, one step back141 command: String::from("command_b"),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
lines 142–143why, one step back142 name: None,143 display_name: String::from("command_b"),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 144–146why, one step back144 measurements: Measurements::new(vec![145 Measurement {146 time_wall_clock: Time::new::<second>(17.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 147why, one step back147 time_cpu: Time::new::<second>(17.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 148–149why, one step back148 time_user: Time::new::<second>(17.0),149 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 150why, one step back150 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 151why, one step back151 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 152–155why, one step back152 exit_status: ExitStatus::default(),153 },154 Measurement {155 time_wall_clock: Time::new::<second>(18.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 156why, one step back156 time_cpu: Time::new::<second>(18.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 157–158why, one step back157 time_user: Time::new::<second>(18.0),158 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 159why, one step back159 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 160why, one step back160 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 161–164why, one step back161 exit_status: ExitStatus::default(),162 },163 Measurement {164 time_wall_clock: Time::new::<second>(19.0),Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 165why, one step back165 time_cpu: Time::new::<second>(19.0),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 166–167why, one step back166 time_user: Time::new::<second>(19.0),167 time_system: Time::zero(),Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015
line 168why, one step back168 memory_peak_resident: Some(Information::new::<byte>(1024.)),Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0
line 169why, one step back169 hardware_counters: Default::default(),Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520
lines 170–172why, one step back170 exit_status: ExitStatus::default(),171 },172 ]),Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 173–174why, one step back173 parameters: {174 let mut params = BTreeMap::new();Merge pull request #968 from sharkdp/codex/parameter-metadataDavid Peterbefore adoption05de077
lines 175–186why, one step back175 params.insert(176 "foo".into(),177 Parameter {178 value: "one".into(),179 },180 );181 params.insert(182 "bar".into(),183 Parameter {184 value: "seven".into(),185 },186 );Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
lines 187–190why, one step back187 params188 },189 },190 ];Move more tests to instaDavid Peterbefore adoptionda399ab
lines 191–192why, one step back191192 let actual = String::from_utf8(Add new --sort optionDavid Peterbefore adoptionac617c8
line 193why, one step back193 exporterAlternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
line 194why, one step back194 .serialize(&results, MetricSelection::default())Add new --sort optionDavid Peterbefore adoptionac617c8
lines 195–197why, one step back195 .unwrap(),196 )197 .unwrap();Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 198why, one step back198Move more tests to instaDavid Peterbefore adoptionda399ab
line 199why, one step back199 insta::assert_snapshot!(actual, @r#"Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 200–202why, one step back200 command,metric,unit,mean,stddev,median,min,max,parameter_bar,parameter_foo201 command_a,time_wall_clock,s,9,2.6457513110645907,8,7,12,two,one202 command_b,time_wall_clock,s,18,1,18,17,19,seven,oneMove more tests to instaDavid Peterbefore adoptionda399ab
line 203why, one step back203 "#);Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9
lines 204–217why, one step back204205 for (selection, unit, mean) in [206 ("memory_peak_resident", "B", "1024"),207 ("memory_peak_resident:KiB", "KiB", "1"),208 ] {209 let output = exporter210 .serialize(&results, MetricSelection::parse_list(selection).unwrap()[0])211 .unwrap();212 let mut reader = csv::Reader::from_reader(output.as_slice());213 let row = reader.records().next().unwrap().unwrap();214 assert_eq!(&row[1], "memory_peak_resident");215 assert_eq!(&row[2], unit);216 assert_eq!(&row[3], mean);217 }Bump clap_complete from 4.2.0 to 4.2.1dependabot[bot]before adoptionab5ae5f
line 218why, one step back218}