2021-08-24 21:33:48 +02:00
|
|
|
use std::fs::{File, OpenOptions};
|
2022-02-06 17:44:28 +01:00
|
|
|
use std::io::Write;
|
2021-08-24 21:33:48 +02:00
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
mod asciidoc;
|
2018-03-17 18:59:28 -04:00
|
|
|
mod csv;
|
2018-03-19 17:37:56 -04:00
|
|
|
mod json;
|
2018-03-20 17:00:24 -04:00
|
|
|
mod markdown;
|
2022-03-21 19:57:57 +01:00
|
|
|
mod markup;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2019-01-29 18:02:19 -05:00
|
|
|
use self::asciidoc::AsciidocExporter;
|
2018-03-17 18:59:28 -04:00
|
|
|
use self::csv::CsvExporter;
|
2018-03-19 17:37:56 -04:00
|
|
|
use self::json::JsonExporter;
|
2018-03-20 17:00:24 -04:00
|
|
|
use self::markdown::MarkdownExporter;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2022-02-20 13:44:34 +01:00
|
|
|
use crate::benchmark::benchmark_result::BenchmarkResult;
|
2022-02-06 20:01:41 +01:00
|
|
|
use crate::util::units::Unit;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2022-02-06 17:44:28 +01:00
|
|
|
use anyhow::{Context, Result};
|
2022-02-06 18:22:55 +01:00
|
|
|
use clap::ArgMatches;
|
2022-02-06 17:44:28 +01:00
|
|
|
|
2018-03-20 20:26:28 +01:00
|
|
|
/// The desired form of exporter to use for a given file.
|
2018-03-19 17:37:56 -04:00
|
|
|
#[derive(Clone)]
|
2018-03-20 20:26:28 +01:00
|
|
|
pub enum ExportType {
|
2019-01-29 18:02:19 -05:00
|
|
|
/// Asciidoc Table
|
|
|
|
|
Asciidoc,
|
|
|
|
|
|
2018-03-20 20:26:28 +01:00
|
|
|
/// CSV (comma separated values) format
|
2018-03-20 21:09:56 +01:00
|
|
|
Csv,
|
2018-03-20 20:26:28 +01:00
|
|
|
|
|
|
|
|
/// JSON format
|
2018-03-20 21:09:56 +01:00
|
|
|
Json,
|
2018-03-20 17:00:24 -04:00
|
|
|
|
|
|
|
|
/// Markdown table
|
|
|
|
|
Markdown,
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2018-03-20 20:26:28 +01:00
|
|
|
/// Interface for different exporters.
|
|
|
|
|
trait Exporter {
|
|
|
|
|
/// Export the given entries in the serialized form.
|
2019-07-13 10:29:05 +02:00
|
|
|
fn serialize(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<Vec<u8>>;
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2018-03-20 21:09:56 +01:00
|
|
|
struct ExporterWithFilename {
|
2019-05-07 11:55:38 +02:00
|
|
|
exporter: Box<dyn Exporter>,
|
2018-03-20 21:09:56 +01:00
|
|
|
filename: String,
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 20:28:57 +01:00
|
|
|
/// Handles the management of multiple file exporters.
|
2021-10-17 17:10:46 +02:00
|
|
|
#[derive(Default)]
|
2018-03-18 15:13:24 -04:00
|
|
|
pub struct ExportManager {
|
2018-03-20 21:09:56 +01:00
|
|
|
exporters: Vec<ExporterWithFilename>,
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 23:07:38 +09:00
|
|
|
impl ExportManager {
|
2022-02-06 18:22:55 +01:00
|
|
|
/// Build the ExportManager that will export the results specified
|
|
|
|
|
/// in the given ArgMatches
|
|
|
|
|
pub fn from_cli_arguments(matches: &ArgMatches) -> Result<Self> {
|
|
|
|
|
let mut export_manager = Self::default();
|
|
|
|
|
{
|
|
|
|
|
let mut add_exporter = |flag, exporttype| -> Result<()> {
|
|
|
|
|
if let Some(filename) = matches.value_of(flag) {
|
|
|
|
|
export_manager.add_exporter(exporttype, filename)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
};
|
|
|
|
|
add_exporter("export-asciidoc", ExportType::Asciidoc)?;
|
|
|
|
|
add_exporter("export-json", ExportType::Json)?;
|
|
|
|
|
add_exporter("export-csv", ExportType::Csv)?;
|
|
|
|
|
add_exporter("export-markdown", ExportType::Markdown)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(export_manager)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-19 17:37:56 -04:00
|
|
|
/// Add an additional exporter to the ExportManager
|
2020-10-18 09:15:44 +02:00
|
|
|
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> {
|
2022-02-06 17:44:28 +01:00
|
|
|
let _ = File::create(filename)
|
2022-02-06 18:01:43 +01:00
|
|
|
.with_context(|| format!("Could not create export file '{}'", filename))?;
|
2020-10-18 09:15:44 +02:00
|
|
|
|
2019-05-07 11:55:38 +02:00
|
|
|
let exporter: Box<dyn Exporter> = match export_type {
|
2019-01-29 18:02:19 -05:00
|
|
|
ExportType::Asciidoc => Box::new(AsciidocExporter::default()),
|
2018-03-20 17:00:24 -04:00
|
|
|
ExportType::Csv => Box::new(CsvExporter::default()),
|
|
|
|
|
ExportType::Json => Box::new(JsonExporter::default()),
|
|
|
|
|
ExportType::Markdown => Box::new(MarkdownExporter::default()),
|
2018-03-20 21:09:56 +01:00
|
|
|
};
|
|
|
|
|
self.exporters.push(ExporterWithFilename {
|
|
|
|
|
exporter,
|
|
|
|
|
filename: filename.to_string(),
|
|
|
|
|
});
|
2020-10-18 09:15:44 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
2018-03-19 17:37:56 -04:00
|
|
|
/// Write the given results to all Exporters contained within this manager
|
2020-10-17 15:23:45 +02:00
|
|
|
pub fn write_results(&self, results: &[BenchmarkResult], unit: Option<Unit>) -> Result<()> {
|
2018-03-20 21:09:56 +01:00
|
|
|
for e in &self.exporters {
|
2021-08-22 15:59:56 +02:00
|
|
|
let file_content = e.exporter.serialize(results, unit)?;
|
2018-03-20 17:00:24 -04:00
|
|
|
write_to_file(&e.filename, &file_content)?;
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-19 17:37:56 -04:00
|
|
|
|
|
|
|
|
/// Write the given content to a file with the specified name
|
2019-07-13 10:29:05 +02:00
|
|
|
fn write_to_file(filename: &str, content: &[u8]) -> Result<()> {
|
2020-10-18 09:15:44 +02:00
|
|
|
let mut file = OpenOptions::new().write(true).open(filename)?;
|
2018-03-20 17:00:24 -04:00
|
|
|
file.write_all(content)
|
2022-02-06 17:44:28 +01:00
|
|
|
.with_context(|| format!("Failed to export results to '{}'", filename))
|
2018-03-19 17:37:56 -04:00
|
|
|
}
|