2018-03-17 18:59:28 -04:00
|
|
|
mod csv;
|
2018-03-19 17:37:56 -04:00
|
|
|
mod json;
|
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-17 18:59:28 -04:00
|
|
|
|
2018-03-19 17:37:56 -04:00
|
|
|
use std::io::{Result, Write};
|
|
|
|
|
use std::fs::File;
|
2018-03-17 18:59:28 -04:00
|
|
|
|
2018-03-18 15:13:24 -04:00
|
|
|
use hyperfine::internal::Second;
|
|
|
|
|
|
2018-03-20 20:26:28 +01:00
|
|
|
/// Set of values that will be exported.
|
2018-03-17 18:59:28 -04:00
|
|
|
#[derive(Debug, Default, Clone, Serialize)]
|
|
|
|
|
pub struct ExportEntry {
|
|
|
|
|
/// The command that was run
|
|
|
|
|
command: String,
|
2018-03-20 20:26:28 +01:00
|
|
|
|
2018-03-17 18:59:28 -04:00
|
|
|
/// The mean run time
|
2018-03-18 15:13:24 -04:00
|
|
|
mean: Second,
|
2018-03-20 20:26:28 +01:00
|
|
|
|
2018-03-17 18:59:28 -04:00
|
|
|
/// The standard deviation of all run times
|
2018-03-18 15:13:24 -04:00
|
|
|
stddev: Second,
|
2018-03-20 20:26:28 +01:00
|
|
|
|
2018-03-17 18:59:28 -04:00
|
|
|
/// Time spend in user space
|
2018-03-18 15:13:24 -04:00
|
|
|
user: Second,
|
2018-03-20 20:26:28 +01:00
|
|
|
|
2018-03-17 18:59:28 -04:00
|
|
|
/// Time spent in system space
|
2018-03-18 15:13:24 -04:00
|
|
|
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,
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2018-03-17 18:59:28 -04:00
|
|
|
ExportEntry {
|
|
|
|
|
command,
|
|
|
|
|
mean,
|
|
|
|
|
stddev,
|
|
|
|
|
user,
|
|
|
|
|
system,
|
2018-03-18 16:53:34 -04:00
|
|
|
min,
|
|
|
|
|
max,
|
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 {
|
|
|
|
|
/// 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-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.
|
2018-03-20 20:47:19 +01:00
|
|
|
fn serialize(&self, results: &Vec<ExportEntry>) -> Result<String>;
|
2018-03-17 18:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
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.
|
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
|
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(),
|
|
|
|
|
});
|
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
|
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)?;
|
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
|
2018-03-20 21:09:56 +01:00
|
|
|
fn write_to_file(filename: &String, content: String) -> Result<()> {
|
2018-03-19 17:37:56 -04:00
|
|
|
let mut file = File::create(filename)?;
|
2018-03-20 21:09:56 +01:00
|
|
|
file.write_all(content.as_bytes())
|
2018-03-19 17:37:56 -04:00
|
|
|
}
|