Files
hyperfine/src/hyperfine/types.rs
T

139 lines
3.3 KiB
Rust
Raw Normal View History

2019-05-07 11:55:38 +02:00
use serde::*;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::fmt;
2021-08-22 17:52:55 +02:00
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
2021-08-22 17:53:08 +02:00
use crate::hyperfine::units::Second;
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]
2019-08-31 12:48:26 +05:30
#[serde(untagged)]
pub enum NumericType {
Int(i32),
Decimal(Decimal),
}
impl fmt::Display for NumericType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
NumericType::Int(i) => fmt::Display::fmt(&i, f),
NumericType::Decimal(i) => fmt::Display::fmt(&i, f),
}
}
}
2021-08-22 16:05:03 +02:00
impl From<i32> for NumericType {
fn from(x: i32) -> NumericType {
NumericType::Int(x)
2019-08-31 12:48:26 +05:30
}
}
2021-08-22 16:05:03 +02:00
impl From<Decimal> for NumericType {
fn from(x: Decimal) -> NumericType {
NumericType::Decimal(x)
2019-08-31 12:48:26 +05:30
}
}
impl TryFrom<NumericType> for usize {
type Error = ();
fn try_from(numeric: NumericType) -> Result<Self, Self::Error> {
match numeric {
NumericType::Int(i) => usize::try_from(i).map_err(|_| ()),
NumericType::Decimal(d) => match d.to_u64() {
Some(u) => usize::try_from(u).map_err(|_| ()),
None => Err(()),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
2019-10-20 13:18:00 +02:00
pub enum ParameterValue {
Text(String),
Numeric(NumericType),
}
2019-10-20 13:18:00 +02:00
impl<'a> ToString for ParameterValue {
fn to_string(&self) -> String {
match self {
2019-10-20 13:18:00 +02:00
ParameterValue::Text(ref value) => value.clone(),
ParameterValue::Numeric(value) => value.to_string(),
}
}
}
/// Set of values that will be exported.
// NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the
// `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate.
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
pub struct BenchmarkResult {
/// The command that was run
pub command: String,
/// The mean run time
pub mean: Second,
/// The standard deviation of all run times
pub stddev: Second,
/// The median run time
pub median: Second,
/// Time spend in user space
pub user: Second,
/// Time spent in system space
pub system: Second,
/// Min time measured
pub min: Second,
/// Max time measured
pub max: Second,
/// All run time measurements
#[serde(skip_serializing_if = "Option::is_none")]
pub times: Option<Vec<Second>>,
2021-02-28 10:22:21 +01:00
/// All run exit codes
pub exit_codes: Vec<Option<i32>>,
2021-02-28 10:22:21 +01:00
/// Any parameter values used
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub parameters: BTreeMap<String, String>,
}
impl BenchmarkResult {
/// Create a new entry with the given values.
2021-08-22 16:05:03 +02:00
#[allow(clippy::too_many_arguments)]
pub fn new(
command: String,
mean: Second,
stddev: Second,
median: Second,
user: Second,
system: Second,
min: Second,
max: Second,
times: Vec<Second>,
exit_codes: Vec<Option<i32>>,
parameters: BTreeMap<String, String>,
) -> Self {
BenchmarkResult {
command,
mean,
stddev,
median,
user,
system,
min,
max,
times: Some(times),
2021-02-28 10:22:21 +01:00
exit_codes,
parameters,
}
}
}