test/helpers/
metrics.rs

1//! Benchmark metrics.
2
3use std::collections::BTreeMap;
4
5#[derive(Clone, PartialEq, Debug, Copy)]
6pub struct Metric {
7    value: f64,
8    noise: f64,
9}
10
11impl Metric {
12    pub fn new(value: f64, noise: f64) -> Metric {
13        Metric { value, noise }
14    }
15}
16
17#[derive(Clone, PartialEq)]
18pub struct MetricMap(BTreeMap<String, Metric>);
19
20impl MetricMap {
21    pub fn new() -> MetricMap {
22        MetricMap(BTreeMap::new())
23    }
24
25    /// Insert a named `value` (+/- `noise`) metric into the map. The value
26    /// must be non-negative. The `noise` indicates the uncertainty of the
27    /// metric, which doubles as the "noise range" of acceptable
28    /// pairwise-regressions on this named value, when comparing from one
29    /// metric to the next using `compare_to_old`.
30    ///
31    /// If `noise` is positive, then it means this metric is of a value
32    /// you want to see grow smaller, so a change larger than `noise` in the
33    /// positive direction represents a regression.
34    ///
35    /// If `noise` is negative, then it means this metric is of a value
36    /// you want to see grow larger, so a change larger than `noise` in the
37    /// negative direction represents a regression.
38    pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
39        let m = Metric { value, noise };
40        self.0.insert(name.to_owned(), m);
41    }
42
43    pub fn fmt_metrics(&self) -> String {
44        let v = self
45            .0
46            .iter()
47            .map(|(k, v)| format!("{}: {} (+/- {})", *k, v.value, v.noise))
48            .collect::<Vec<_>>();
49        v.join(", ")
50    }
51}