| 1 |
use crate::error::Result; |
| 2 |
use crate::measurement::ValueFormatter; |
| 3 |
use crate::report::{BenchmarkId, MeasurementData, Report, ReportContext}; |
| 4 |
use crate::Throughput; |
| 5 |
use csv::Writer; |
| 6 |
use serde::Serialize; |
| 7 |
use std::io::Write; |
| 8 |
use std::path::Path; |
| 9 |
|
| 10 |
#[derive(Serialize)] |
| 11 |
struct CsvRow<'a> { |
| 12 |
group: &'a str, |
| 13 |
function: Option<&'a str>, |
| 14 |
value: Option<&'a str>, |
| 15 |
throughput_num: Option<&'a str>, |
| 16 |
throughput_type: Option<&'a str>, |
| 17 |
sample_measured_value: f64, |
| 18 |
unit: &'static str, |
| 19 |
iteration_count: u64, |
| 20 |
} |
| 21 |
|
| 22 |
struct CsvReportWriter<W: Write> { |
| 23 |
writer: Writer<W>, |
| 24 |
} |
| 25 |
impl<W: Write> CsvReportWriter<W> { |
| 26 |
fn write_data( |
| 27 |
&mut self, |
| 28 |
id: &BenchmarkId, |
| 29 |
data: &MeasurementData<'_>, |
| 30 |
formatter: &dyn ValueFormatter, |
| 31 |
) -> Result<()> { |
| 32 |
let mut data_scaled: Vec<f64> = data.sample_times().as_ref().into(); |
| 33 |
let unit = formatter.scale_for_machines(&mut data_scaled); |
| 34 |
let group = id.group_id.as_str(); |
| 35 |
let function = id.function_id.as_deref(); |
| 36 |
let value = id.value_str.as_deref(); |
| 37 |
let (throughput_num, throughput_type) = match id.throughput { |
| 38 |
Some(Throughput::Bytes(bytes)) => (Some(format!("{}", bytes)), Some("bytes")), |
| 39 |
Some(Throughput::BytesDecimal(bytes)) => (Some(format!("{}", bytes)), Some("bytes")), |
| 40 |
Some(Throughput::Elements(elems)) => (Some(format!("{}", elems)), Some("elements")), |
| 41 |
Some(Throughput::Bits(bits)) => (Some(format!("{}", bits)), Some("bits")), |
| 42 |
None => (None, None), |
| 43 |
}; |
| 44 |
let throughput_num = throughput_num.as_deref(); |
| 45 |
|
| 46 |
for (count, measured_value) in data.iter_counts().iter().zip(data_scaled) { |
| 47 |
let row = CsvRow { |
| 48 |
group, |
| 49 |
function, |
| 50 |
value, |
| 51 |
throughput_num, |
| 52 |
throughput_type, |
| 53 |
sample_measured_value: measured_value, |
| 54 |
unit, |
| 55 |
iteration_count: (*count) as u64, |
| 56 |
}; |
| 57 |
self.writer.serialize(row)?; |
| 58 |
} |
| 59 |
Ok(()) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub struct FileCsvReport; |
| 64 |
impl FileCsvReport { |
| 65 |
fn write_file( |
| 66 |
&self, |
| 67 |
path: &Path, |
| 68 |
id: &BenchmarkId, |
| 69 |
measurements: &MeasurementData<'_>, |
| 70 |
formatter: &dyn ValueFormatter, |
| 71 |
) -> Result<()> { |
| 72 |
let writer = Writer::from_path(path)?; |
| 73 |
let mut writer = CsvReportWriter { writer }; |
| 74 |
writer.write_data(id, measurements, formatter)?; |
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl Report for FileCsvReport { |
| 80 |
fn measurement_complete( |
| 81 |
&self, |
| 82 |
id: &BenchmarkId, |
| 83 |
context: &ReportContext, |
| 84 |
measurements: &MeasurementData<'_>, |
| 85 |
formatter: &dyn ValueFormatter, |
| 86 |
) { |
| 87 |
let mut path = context.output_directory.clone(); |
| 88 |
path.push(id.as_directory_name()); |
| 89 |
path.push("new"); |
| 90 |
path.push("raw.csv"); |
| 91 |
log_if_err!(self.write_file(&path, id, measurements, formatter)); |
| 92 |
} |
| 93 |
} |