Skip to content
Myr

Some renamings and documentation updates

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by David Peter · parent c59110d · Browse files · 4 files+32 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+32 added−28 removed

  • other:+32 added−28 removed

4 files+32 added−28 removed

modified:src/hyperfine/export/csv.rs+3 added-3 removed
1use super::{ExportEntry, ResultExporter};1use super::{ExportEntry, Exporter};23use csv::WriterBuilder;4use std::io::{Error, ErrorKind, Result};56pub struct CsvExporter {}78impl ResultExporter for CsvExporter {9 fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {8impl Exporter for CsvExporter {9 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {10 let mut writer = WriterBuilder::new().from_writer(vec![]);11 for res in results {12 writer.serialize(res)?;13 }
13 unmodified lines
modified:src/hyperfine/export/json.rs+3 added-3 removed
1use super::{ExportEntry, ResultExporter};1use super::{ExportEntry, Exporter};23use std::io::{Error, ErrorKind, Result};45use serde_json::to_vec_pretty;
4 unmodified lines
10}1112pub struct JsonExporter {}1314impl ResultExporter for JsonExporter {15 fn write(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {14impl Exporter for JsonExporter {15 fn serialize(&self, results: &Vec<ExportEntry>) -> Result<Vec<u8>> {16 let serialized = to_vec_pretty(&HyperfineSummary { results });1718 match serialized {19 Ok(file_content) => Ok(file_content),
10 unmodified lines
modified:src/hyperfine/export/mod.rs+23 added-19 removed
7 unmodified lines
8use std::fs::File;910use hyperfine::internal::Second;1112/// The ExportEntry is the main set of values that will13/// be exported to files when requested.12/// Set of values that will be exported.13#[derive(Debug, Default, Clone, Serialize)]14pub struct ExportEntry {15 /// The command that was run16 command: String,1718 /// The mean run time19 mean: Second,2021 /// The standard deviation of all run times22 stddev: Second,2324 /// Time spend in user space25 user: Second,2627 /// Time spent in system space28 system: Second,2930 /// Min time measured31 min: Second,3233 /// Max time measured34 max: Second,35}3637impl ExportEntry {33 /// Create a new ExportEntry with the given values38 /// Create a new entry with the given values.39 pub fn new(40 command: String,41 mean: Second,42 stddev: Second,
13 unmodified lines
56 }57 }58}5955/// The ResultExportType enum is used to denote the desired form56/// of exporter to use for a given file.60/// The desired form of exporter to use for a given file.61#[derive(Clone)]58pub enum ResultExportType {59 /// Export to a csv file with the provided name62pub enum ExportType {63 /// CSV (comma separated values) format64 Csv(String),61 /// Export to a JSON file6566 /// JSON format67 Json(String),68}6965/// A ResultExporter is responsible for writing all results to the66/// appropriate file67trait ResultExporter {68 /// Write all entries to the target destination69 fn write(&self, values: &Vec<ExportEntry>) -> Result<Vec<u8>>;70/// Interface for different exporters.71trait Exporter {72 /// Export the given entries in the serialized form.73 fn serialize(&self, values: &Vec<ExportEntry>) -> Result<Vec<u8>>;74}7576/// Create a new ExportManager77pub fn create_export_manager() -> ExportManager {
4 unmodified lines
8283/// The ExportManager handles the management of multiple file84/// exporters.85pub struct ExportManager {82 exporters: Vec<ResultExportType>,86 exporters: Vec<ExportType>,87}8889impl ExportManager {90 /// Add an additional exporter to the ExportManager87 pub fn add_exporter(&mut self, for_type: ResultExportType) {91 pub fn add_exporter(&mut self, for_type: ExportType) {92 self.exporters.push(for_type);93 }9495 /// Write the given results to all Exporters contained within this manager96 pub fn write_results(&self, to_write: Vec<ExportEntry>) -> Result<()> {97 for exp in &self.exporters {94 let (exporter, filename): (Box<ResultExporter>, &str) = match exp {95 &ResultExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file),96 &ResultExportType::Json(ref file) => (Box::from(JsonExporter::new()), file),98 let (exporter, filename): (Box<Exporter>, &str) = match exp {99 &ExportType::Csv(ref file) => (Box::from(CsvExporter::new()), file),100 &ExportType::Json(ref file) => (Box::from(JsonExporter::new()), file),101 };10299 let file_content = exporter.write(&to_write)?;103 let file_content = exporter.serialize(&to_write)?;104 write_to_file(filename, file_content)?;105 }106 Ok(())107 }
8 unmodified lines
modified:src/main.rs+3 added-3 removed
37 unmodified lines
38mod hyperfine;3940use hyperfine::internal::{CmdFailureAction, HyperfineOptions, OutputStyleOption};41use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};42use hyperfine::export::{create_export_manager, ExportEntry, ExportManager, ResultExportType};42use hyperfine::export::{create_export_manager, ExportEntry, ExportManager, ExportType};4344/// Print error message to stderr and terminate45pub fn error(message: &str) -> ! {46 eprintln!("{} {}", "Error:".red(), message);
178 unmodified lines
225226 let mut export_manager = create_export_manager();227228 if let Some(filename) = targets.json_file {229 export_manager.add_exporter(ResultExportType::Json(filename.to_string()));229 export_manager.add_exporter(ExportType::Json(filename.to_string()));230 }231232 if let Some(filename) = targets.csv_file {233 export_manager.add_exporter(ResultExportType::Csv(filename.to_string()));233 export_manager.add_exporter(ExportType::Csv(filename.to_string()));234 }235 Some(export_manager)236}