Files
hyperfine/src/hyperfine/export/mod.rs
T

120 lines
2.8 KiB
Rust
Raw Normal View History

mod csv;
mod json;
use self::csv::CsvExporter;
use self::json::JsonExporter;
use std::io::{Result, Write};
use std::fs::File;
use hyperfine::internal::Second;
2018-03-20 20:26:28 +01:00
/// Set of values that will be exported.
#[derive(Debug, Default, Clone, Serialize)]
pub struct ExportEntry {
/// The command that was run
command: String,
2018-03-20 20:26:28 +01:00
/// The mean run time
mean: Second,
2018-03-20 20:26:28 +01:00
/// The standard deviation of all run times
stddev: Second,
2018-03-20 20:26:28 +01:00
/// Time spend in user space
user: Second,
2018-03-20 20:26:28 +01:00
/// Time spent in system space
system: Second,
2018-03-20 20:26:28 +01:00
2018-03-18 16:53:34 -04:00
/// Min time measured
min: Second,
2018-03-20 20:26:28 +01:00
2018-03-18 16:53:34 -04:00
/// Max time measured
max: Second,
}
impl ExportEntry {
2018-03-20 20:26:28 +01:00
/// Create a new entry with the given values.
2018-03-18 16:53:34 -04:00
pub fn new(
command: String,
mean: Second,
stddev: Second,
user: Second,
system: Second,
min: Second,
max: Second,
) -> Self {
ExportEntry {
command,
mean,
stddev,
user,
system,
2018-03-18 16:53:34 -04:00
min,
max,
}
}
}
2018-03-20 20:26:28 +01:00
/// The desired form of exporter to use for a given file.
#[derive(Clone)]
2018-03-20 20:26:28 +01:00
pub enum ExportType {
/// 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 20:26:28 +01:00
/// Interface for different exporters.
trait Exporter {
/// Export the given entries in the serialized form.
fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;
}
2018-03-20 21:09:56 +01:00
struct ExporterWithFilename {
exporter: Box<Exporter>,
filename: String,
}
2018-03-20 20:28:57 +01:00
/// Handles the management of multiple file exporters.
pub struct ExportManager {
2018-03-20 21:09:56 +01:00
exporters: Vec<ExporterWithFilename>,
}
impl ExportManager {
2018-03-20 20:28:57 +01:00
/// Create a new ExportManager
pub fn new() -> ExportManager {
ExportManager {
exporters: Vec::new(),
}
}
/// Add an additional exporter to the ExportManager
2018-03-20 21:09:56 +01:00
pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) {
let exporter: Box<Exporter> = match export_type {
ExportType::Csv => Box::new(CsvExporter::new()),
ExportType::Json => Box::new(JsonExporter::new()),
};
self.exporters.push(ExporterWithFilename {
exporter,
filename: filename.to_string(),
});
}
/// Write the given results to all Exporters contained within this manager
2018-03-20 21:09:56 +01:00
pub fn write_results(&self, results: Vec<ExportEntry>) -> Result<()> {
for e in &self.exporters {
let file_content = e.exporter.serialize(&results)?;
write_to_file(&e.filename, file_content)?;
}
Ok(())
}
}
/// Write the given content to a file with the specified name
2018-03-20 21:09:56 +01:00
fn write_to_file(filename: &String, content: String) -> Result<()> {
let mut file = File::create(filename)?;
2018-03-20 21:09:56 +01:00
file.write_all(content.as_bytes())
}