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