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;
|
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
|
|
|
|
2020-10-18 09:15:44 +02:00
|
|
|
use std::fs::{File, OpenOptions};
|
2018-05-22 22:57:51 +02:00
|
|
|
use std::io::{Result, Write};
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2021-08-24 21:24:34 +02:00
|
|
|
use crate::benchmark_result::BenchmarkResult;
|
|
|
|
|
use crate::units::Unit;
|
2018-03-17 18:59:28 -04: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.
|
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
|
|
|
}
|
|
|
|
|
|
2018-03-18 15:13:24 -04:00
|
|
|
impl ExportManager {
|
2018-03-20 20:28:57 +01:00
|
|
|
/// Create a new ExportManager
|
|
|
|
|
pub fn new() -> ExportManager {
|
|
|
|
|
ExportManager {
|
|
|
|
|
exporters: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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<()> {
|
|
|
|
|
let _ = File::create(filename)?;
|
|
|
|
|
|
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)
|
2018-03-19 17:37:56 -04:00
|
|
|
}
|