2019-05-07 11:55:38 +02:00
|
|
|
use serde::*;
|
2020-09-14 20:28:02 -07:00
|
|
|
use std::collections::BTreeMap;
|
2021-05-13 05:54:25 +00:00
|
|
|
use std::convert::TryFrom;
|
2018-03-24 15:46:28 +01:00
|
|
|
use std::fmt;
|
2018-03-22 21:53:36 -04:00
|
|
|
|
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;
|
2018-11-01 20:52:25 -07:00
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 05:54:25 +00:00
|
|
|
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(()),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-10-20 13:18:00 +02:00
|
|
|
pub enum ParameterValue {
|
|
|
|
|
Text(String),
|
2019-10-20 09:54:47 +02:00
|
|
|
Numeric(NumericType),
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 13:18:00 +02:00
|
|
|
impl<'a> ToString for ParameterValue {
|
2019-10-20 09:54:47 +02:00
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
match self {
|
2019-10-20 13:18:00 +02:00
|
|
|
ParameterValue::Text(ref value) => value.clone(),
|
2019-10-20 09:54:47 +02:00
|
|
|
ParameterValue::Numeric(value) => value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// Set of values that will be exported.
|
2020-09-14 20:28:02 -07:00
|
|
|
// 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.
|
2019-10-13 15:40:17 +02:00
|
|
|
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
|
2018-03-22 21:53:36 -04:00
|
|
|
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,
|
|
|
|
|
|
2019-06-08 13:04:39 +02:00
|
|
|
/// The median run time
|
|
|
|
|
pub median: Second,
|
|
|
|
|
|
2018-03-22 21:53:36 -04:00
|
|
|
/// 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>>,
|
2019-01-16 12:42:15 +01:00
|
|
|
|
2021-02-28 10:22:21 +01:00
|
|
|
/// All run exit codes
|
2021-05-09 18:20:59 +02:00
|
|
|
pub exit_codes: Vec<Option<i32>>,
|
2021-02-28 10:22:21 +01:00
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
/// Any parameter values used
|
|
|
|
|
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
|
|
|
|
pub parameters: BTreeMap<String, String>,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BenchmarkResult {
|
|
|
|
|
/// Create a new entry with the given values.
|
2021-08-22 16:05:03 +02:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2018-03-22 21:53:36 -04:00
|
|
|
pub fn new(
|
|
|
|
|
command: String,
|
|
|
|
|
mean: Second,
|
|
|
|
|
stddev: Second,
|
2019-06-08 13:04:39 +02:00
|
|
|
median: Second,
|
2018-03-22 21:53:36 -04:00
|
|
|
user: Second,
|
|
|
|
|
system: Second,
|
|
|
|
|
min: Second,
|
|
|
|
|
max: Second,
|
|
|
|
|
times: Vec<Second>,
|
2021-05-09 18:20:59 +02:00
|
|
|
exit_codes: Vec<Option<i32>>,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters: BTreeMap<String, String>,
|
2018-03-22 21:53:36 -04:00
|
|
|
) -> Self {
|
|
|
|
|
BenchmarkResult {
|
|
|
|
|
command,
|
|
|
|
|
mean,
|
|
|
|
|
stddev,
|
2019-06-08 13:04:39 +02:00
|
|
|
median,
|
2018-03-22 21:53:36 -04:00
|
|
|
user,
|
|
|
|
|
system,
|
|
|
|
|
min,
|
|
|
|
|
max,
|
|
|
|
|
times: Some(times),
|
2021-02-28 10:22:21 +01:00
|
|
|
exit_codes,
|
2020-09-14 20:28:02 -07:00
|
|
|
parameters,
|
2018-03-22 21:53:36 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|