Debian Codegraph @

third_party/rust/glean-core/src/histogram/functional.rs

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5 use std::collections::HashMap;
6
7 use malloc_size_of_derive::MallocSizeOf;
8 use serde::{Deserialize, Serialize};
9
10 use super::{Bucketing, Histogram};
11
12 use crate::util::floating_point_context::FloatingPointContext;
13
14 /// A functional bucketing algorithm.
15 ///
16 /// Bucketing is performed by a function, rather than pre-computed buckets.
17 /// The bucket index of a given sample is determined with the following function:
18 ///
19 /// i = ⌊n log<sub>base</sub>(𝑥)⌋
20 ///
21 /// In other words, there are n buckets for each power of `base` magnitude.
22 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, MallocSizeOf)]
23 pub struct Functional {
24 exponent: f64,
25 }
26
27 impl Functional {
28 /// Instantiate a new functional bucketing.
29 fn new(log_base: f64, buckets_per_magnitude: f64) -> Functional {
30 // Set the FPU control flag to the required state within this function
31 let _fpc = FloatingPointContext::new();
32
33 let exponent = log_base.powf(1.0 / buckets_per_magnitude);
34
35 Functional { exponent }
36 }
37
38 /// Maps a sample to a "bucket index" that it belongs in.
39 /// A "bucket index" is the consecutive integer index of each bucket, useful as a
40 /// mathematical concept, even though the internal representation is stored and
41 /// sent using the minimum value in each bucket.
42 fn sample_to_bucket_index(&self, sample: u64) -> u64 {
43 // Set the FPU control flag to the required state within this function
44 let _fpc = FloatingPointContext::new();
45
46 ((sample.saturating_add(1)) as f64).log(self.exponent) as u64
47 }
48
49 /// Determines the minimum value of a bucket, given a bucket index.
50 fn bucket_index_to_bucket_minimum(&self, index: u64) -> u64 {
51 // Set the FPU control flag to the required state within this function
52 let _fpc = FloatingPointContext::new();
53
54 self.exponent.powf(index as f64) as u64
55 }
56 }
57
58 impl Bucketing for Functional {
59 fn sample_to_bucket_minimum(&self, sample: u64) -> u64 {
60 if sample == 0 {
61 return 0;
62 }
63
64 let index = self.sample_to_bucket_index(sample);
65 self.bucket_index_to_bucket_minimum(index)
66 }
67
68 fn ranges(&self) -> &[u64] {
69 unimplemented!("Bucket ranges for functional bucketing are not precomputed")
70 }
71 }
72
73 impl Histogram<Functional> {
74 /// Creates a histogram with functional buckets.
75 pub fn functional(log_base: f64, buckets_per_magnitude: f64) -> Histogram<Functional> {
76 Histogram {
77 values: HashMap::new(),
78 count: 0,
79 sum: 0,
80 bucketing: Functional::new(log_base, buckets_per_magnitude),
81 }
82 }
83
84 /// Gets a snapshot of all contiguous values.
85 ///
86 /// **Caution** This is a more specific implementation of `snapshot_values` on functional
87 /// histograms. `snapshot_values` cannot be used with those, due to buckets not being
88 /// precomputed.
89 pub fn snapshot(&self) -> &HashMap<u64, u64> {
90 &self.values
91 }
92 }
93
94 #[cfg(test)]
95 mod test {
96 use super::*;
97
98 #[test]
99 fn can_count() {
100 let mut hist = Histogram::functional(2.0, 8.0);
101 assert!(hist.is_empty());
102
103 for i in 1..=10 {
104 hist.accumulate(i);
105 }
106
107 assert_eq!(10, hist.count());
108 assert_eq!(55, hist.sum());
109 }
110
111 #[test]
112 fn sample_to_bucket_minimum_correctly_rounds_down() {
113 let hist = Histogram::functional(2.0, 8.0);
114
115 // Check each of the first 100 integers, where numerical accuracy of the round-tripping
116 // is most potentially problematic
117 for value in 0..100 {
118 let bucket_minimum = hist.bucketing.sample_to_bucket_minimum(value);
119 assert!(bucket_minimum <= value);
120
121 assert_eq!(
122 bucket_minimum,
123 hist.bucketing.sample_to_bucket_minimum(bucket_minimum)
124 );
125 }
126
127 // Do an exponential sampling of higher numbers
128 for i in 11..500 {
129 let value = 1.5f64.powi(i);
130 let value = value as u64;
131 let bucket_minimum = hist.bucketing.sample_to_bucket_minimum(value);
132 assert!(bucket_minimum <= value);
133 assert_eq!(
134 bucket_minimum,
135 hist.bucketing.sample_to_bucket_minimum(bucket_minimum)
136 );
137 }
138 }
139
140 #[test]
141 fn histogram_merge() {
142 let mut hist = Histogram::functional(2.0, 8.0);
143
144 // Check each of the first 100 integers, where numerical accuracy of the round-tripping
145 // is most potentially problematic
146 for sample in 0..100 {
147 hist.accumulate(sample);
148 }
149
150 let mut filled_hist = hist.clone();
151
152 let mut hist2 = Histogram::functional(2.0, 8.0);
153 for sample in 100..200 {
154 hist.accumulate(sample);
155 hist2.accumulate(sample);
156 }
157
158 filled_hist.merge(&hist2);
159
160 assert_eq!(filled_hist, hist);
161 }
162
163 #[test]
164 #[should_panic]
165 fn histogram_merge_different_buckets() {
166 let mut hist1 = Histogram::functional(2.0, 8.0);
167 let hist2 = Histogram::functional(1.0, 4.0);
168
169 hist1.merge(&hist2);
170 }
171
172 #[test]
173 fn histogram_clears() {
174 let mut hist = Histogram::functional(2.0, 8.0);
175 let empty_hist = hist.clone();
176
177 assert_eq!(empty_hist, hist);
178
179 hist.accumulate(13);
180 assert_ne!(empty_hist, hist);
181
182 hist.clear();
183 assert_eq!(empty_hist, hist);
184 }
185 }