Some renamings and documentation updates

This commit is contained in:
sharkdp authored and David Peter committed 2018-03-20 21:40:06 +01:00
1 parent c59110d652
commit aabe1331a2
4 files changed
+32 -28

No files matched your search

+3 -3
View File
@@ -1,12 +1,12 @@
use super::{ExportEntry, ResultExporter}; use super::{ExportEntry, Exporter};
use csv::WriterBuilder; use csv::WriterBuilder;
use std::io::{Error, ErrorKind, Result}; use std::io::{Error, ErrorKind, Result};
pub struct CsvExporter {} pub struct CsvExporter {}
impl ResultExporter for CsvExporter { impl Exporter for CsvExporter {
fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> { fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {
let mut writer = WriterBuilder::new().from_writer(vec![]); let mut writer = WriterBuilder::new().from_writer(vec![]);
for res in results { for res in results {
writer.serialize(res)?; writer.serialize(res)?;
+3 -3
View File
@@ -1,4 +1,4 @@
use super::{ExportEntry, ResultExporter}; use super::{ExportEntry, Exporter};
use std::io::{Error, ErrorKind, Result}; use std::io::{Error, ErrorKind, Result};
@@ -11,8 +11,8 @@ struct HyperfineSummary<'a> {
pub struct JsonExporter {} pub struct JsonExporter {}
impl ResultExporter for JsonExporter { impl Exporter for JsonExporter {
fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> { fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {
let serialized = to_vec_pretty(&HyperfineSummary { results }); let serialized = to_vec_pretty(&HyperfineSummary { results });
match serialized { match serialized {
+23 -19
View File
@@ -9,28 +9,33 @@ use std::fs::File;
use hyperfine::internal::Second; use hyperfine::internal::Second;
/// The ExportEntry is the main set of values that will /// Set of values that will be exported.
/// be exported to files when requested.
#[derive(Debug, Default, Clone, Serialize)] #[derive(Debug, Default, Clone, Serialize)]
pub struct ExportEntry { pub struct ExportEntry {
/// The command that was run /// The command that was run
command: String, command: String,
/// The mean run time /// The mean run time
mean: Second, mean: Second,
/// The standard deviation of all run times /// The standard deviation of all run times
stddev: Second, stddev: Second,
/// Time spend in user space /// Time spend in user space
user: Second, user: Second,
/// Time spent in system space /// Time spent in system space
system: Second, system: Second,
/// Min time measured /// Min time measured
min: Second, min: Second,
/// Max time measured /// Max time measured
max: Second, max: Second,
} }
impl ExportEntry { impl ExportEntry {
/// Create a new ExportEntry with the given values /// Create a new entry with the given values.
pub fn new( pub fn new(
command: String, command: String,
mean: Second, mean: Second,
@@ -52,21 +57,20 @@ impl ExportEntry {
} }
} }
/// The ResultExportType enum is used to denote the desired form /// The desired form of exporter to use for a given file.
/// of exporter to use for a given file.
#[derive(Clone)] #[derive(Clone)]
pub enum ResultExportType { pub enum ExportType {
/// Export to a csv file with the provided name /// CSV (comma separated values) format
Csv(String), Csv(String),
/// Export to a JSON file
/// JSON format
Json(String), Json(String),
} }
/// A ResultExporter is responsible for writing all results to the /// Interface for different exporters.
/// appropriate file trait Exporter {
trait ResultExporter { /// Export the given entries in the serialized form.
/// Write all entries to the target destination fn serialize(&self, values: &Vec<ExportEntry>) -> Result<Vec<u8>>;
fn write(&self, values: &Vec<ExportEntry>) -> Result<Vec<u8>>;
} }
/// Create a new ExportManager /// Create a new ExportManager
@@ -79,24 +83,24 @@ pub fn create_export_manager() -> ExportManager {
/// The ExportManager handles the management of multiple file /// The ExportManager handles the management of multiple file
/// exporters. /// exporters.
pub struct ExportManager { pub struct ExportManager {
exporters: Vec<ResultExportType>, exporters: Vec<ExportType>,
} }
impl ExportManager { impl ExportManager {
/// Add an additional exporter to the ExportManager /// Add an additional exporter to the ExportManager
pub fn add_exporter(&mut self, for_type: ResultExportType) { pub fn add_exporter(&mut self, for_type: ExportType) {
self.exporters.push(for_type); self.exporters.push(for_type);
} }
/// Write the given results to all Exporters contained within this manager /// Write the given results to all Exporters contained within this manager
pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> { pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> {
for exp in &self.exporters { for exp in &self.exporters {
let (exporter, filename): (Box<ResultExporter>, &str) = match exp { let (exporter, filename): (Box<Exporter>, &str) = match exp {
&ResultExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file), &ExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file),
&ResultExportType::Json(ref file) => (Box::from(JsonExporter::new()), file), &ExportType::Json(ref file) => (Box::from(JsonExporter::new()), file),
}; };
let file_content = exporter.write(&to_write)?; let file_content = exporter.serialize(&to_write)?;
write_to_file(filename, file_content)?; write_to_file(filename, file_content)?;
} }
Ok(()) Ok(())
+3 -3
View File
@@ -39,7 +39,7 @@ mod hyperfine;
use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption}; use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark}; use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
use hyperfine::export::{create_export_manager, ExportEntry, ExportManager, ResultExportType}; use hyperfine::export::{create_export_manager, ExportEntry, ExportManager, ExportType};
/// Print error message to stderr and terminate /// Print error message to stderr and terminate
pub fn error(message: &str) -> ! { pub fn error(message: &str) -> ! {
@@ -226,11 +226,11 @@ fn create_exporter(targets: ExportTargetList) -> Option<ExportManager> {
let mut export_manager = create_export_manager(); let mut export_manager = create_export_manager();
if let Some(filename) = targets.json_file { if let Some(filename) = targets.json_file {
export_manager.add_exporter(ResultExportType::Json(filename.to_string())); export_manager.add_exporter(ExportType::Json(filename.to_string()));
} }
if let Some(filename) = targets.csv_file { if let Some(filename) = targets.csv_file {
export_manager.add_exporter(ResultExportType::Csv(filename.to_string())); export_manager.add_exporter(ExportType::Csv(filename.to_string()));
} }
Some(export_manager) Some(export_manager)
} }