Debian Codegraph @

third_party/rust/glean-core/tests/custom_distribution.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 mod common;
6 use crate::common::*;
7
8 use serde_json::json;
9
10 use glean_core::metrics::*;
11 use glean_core::storage::StorageManager;
12 use glean_core::{test_get_num_recorded_errors, ErrorType};
13 use glean_core::{CommonMetricData, Lifetime};
14
15 // Tests ported from glean-ac
16
17 mod linear {
18 use super::*;
19
20 #[test]
21 fn serializer_should_correctly_serialize_custom_distribution() {
22 let (mut tempdir, _) = tempdir();
23
24 {
25 let (glean, dir) = new_glean(Some(tempdir));
26 tempdir = dir;
27
28 let metric = CustomDistributionMetric::new(
29 CommonMetricData {
30 name: "distribution".into(),
31 category: "telemetry".into(),
32 send_in_pings: vec!["store1".into()],
33 disabled: false,
34 lifetime: Lifetime::Ping,
35 ..Default::default()
36 },
37 1,
38 100,
39 100,
40 HistogramType::Linear,
41 );
42
43 metric.accumulate_samples_sync(&glean, &[50]);
44
45 let snapshot = metric
46 .get_value(&glean, "store1")
47 .expect("Value should be stored");
48
49 assert_eq!(snapshot.sum, 50);
50 }
51
52 // Make a new Glean instance here, which should force reloading of the data from disk
53 // so we can ensure it persisted, because it has User lifetime
54 {
55 let (glean, _t) = new_glean(Some(tempdir));
56 let snapshot = StorageManager
57 .snapshot_as_json(glean.storage(), "store1", true)
58 .unwrap();
59
60 assert_eq!(
61 json!(50),
62 snapshot["custom_distribution"]["telemetry.distribution"]["sum"]
63 );
64 }
65 }
66
67 #[test]
68 fn set_value_properly_sets_the_value_in_all_stores() {
69 let (glean, _t) = new_glean(None);
70 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
71
72 let metric = CustomDistributionMetric::new(
73 CommonMetricData {
74 name: "distribution".into(),
75 category: "telemetry".into(),
76 send_in_pings: store_names.clone(),
77 disabled: false,
78 lifetime: Lifetime::Ping,
79 ..Default::default()
80 },
81 1,
82 100,
83 100,
84 HistogramType::Linear,
85 );
86
87 metric.accumulate_samples_sync(&glean, &[50]);
88
89 for store_name in store_names {
90 let snapshot = StorageManager
91 .snapshot_as_json(glean.storage(), &store_name, true)
92 .unwrap();
93
94 assert_eq!(
95 json!(50),
96 snapshot["custom_distribution"]["telemetry.distribution"]["sum"]
97 );
98 assert_eq!(
99 json!(1),
100 snapshot["custom_distribution"]["telemetry.distribution"]["values"]["50"]
101 );
102 }
103 }
104
105 // SKIPPED from glean-ac: memory distributions must not accumulate negative values
106 // This test doesn't apply to Rust, because we're using unsigned integers.
107
108 #[test]
109 fn the_accumulate_samples_api_correctly_stores_memory_values() {
110 let (glean, _t) = new_glean(None);
111
112 let metric = CustomDistributionMetric::new(
113 CommonMetricData {
114 name: "distribution".into(),
115 category: "telemetry".into(),
116 send_in_pings: vec!["store1".into()],
117 disabled: false,
118 lifetime: Lifetime::Ping,
119 ..Default::default()
120 },
121 1,
122 100,
123 100,
124 HistogramType::Linear,
125 );
126
127 // Accumulate the samples. We intentionally do not report
128 // negative values to not trigger error reporting.
129 metric.accumulate_samples_sync(&glean, &[1, 2, 3]);
130
131 let snapshot = metric
132 .get_value(&glean, "store1")
133 .expect("Value should be stored");
134
135 // Check that we got the right sum of samples.
136 assert_eq!(snapshot.sum, 6);
137
138 // We should get a sample in 3 buckets.
139 // These numbers are a bit magic, but they correspond to
140 // `hist.sample_to_bucket_minimum(i * kb)` for `i = 1..=3`.
141 assert_eq!(1, snapshot.values[&1]);
142 assert_eq!(1, snapshot.values[&2]);
143 assert_eq!(1, snapshot.values[&3]);
144
145 // No errors should be reported.
146 assert!(
147 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()
148 );
149 }
150
151 #[test]
152 fn the_accumulate_samples_api_correctly_handles_negative_values() {
153 let (glean, _t) = new_glean(None);
154
155 let metric = CustomDistributionMetric::new(
156 CommonMetricData {
157 name: "distribution".into(),
158 category: "telemetry".into(),
159 send_in_pings: vec!["store1".into()],
160 disabled: false,
161 lifetime: Lifetime::Ping,
162 ..Default::default()
163 },
164 1,
165 100,
166 100,
167 HistogramType::Linear,
168 );
169
170 // Accumulate the samples.
171 metric.accumulate_samples_sync(&glean, &[-1, 1, 2, 3]);
172
173 let snapshot = metric
174 .get_value(&glean, "store1")
175 .expect("Value should be stored");
176
177 // Check that we got the right sum of samples.
178 assert_eq!(snapshot.sum, 6);
179
180 // We should get a sample in 3 buckets.
181 // These numbers are a bit magic, but they correspond to
182 // `hist.sample_to_bucket_minimum(i * kb)` for `i = 1..=3`.
183 assert_eq!(1, snapshot.values[&1]);
184 assert_eq!(1, snapshot.values[&2]);
185 assert_eq!(1, snapshot.values[&3]);
186
187 // 1 error should be reported.
188 assert_eq!(
189 Ok(1),
190 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue)
191 );
192 }
193
194 #[test]
195 fn json_snapshotting_works() {
196 let (glean, _t) = new_glean(None);
197 let metric = CustomDistributionMetric::new(
198 CommonMetricData {
199 name: "distribution".into(),
200 category: "telemetry".into(),
201 send_in_pings: vec!["store1".into()],
202 disabled: false,
203 lifetime: Lifetime::Ping,
204 ..Default::default()
205 },
206 1,
207 100,
208 100,
209 HistogramType::Linear,
210 );
211
212 metric.accumulate_samples_sync(&glean, &[50]);
213
214 let snapshot = metric.get_value(&glean, "store1");
215 assert!(snapshot.is_some());
216 }
217 }
218
219 mod exponential {
220 use super::*;
221
222 #[test]
223 fn serializer_should_correctly_serialize_custom_distribution() {
224 let (mut tempdir, _) = tempdir();
225
226 {
227 let (glean, dir) = new_glean(Some(tempdir));
228 tempdir = dir;
229
230 let metric = CustomDistributionMetric::new(
231 CommonMetricData {
232 name: "distribution".into(),
233 category: "telemetry".into(),
234 send_in_pings: vec!["store1".into()],
235 disabled: false,
236 lifetime: Lifetime::Ping,
237 ..Default::default()
238 },
239 1,
240 100,
241 10,
242 HistogramType::Exponential,
243 );
244
245 metric.accumulate_samples_sync(&glean, &[50]);
246
247 let snapshot = metric
248 .get_value(&glean, "store1")
249 .expect("Value should be stored");
250
251 assert_eq!(snapshot.sum, 50);
252 }
253
254 // Make a new Glean instance here, which should force reloading of the data from disk
255 // so we can ensure it persisted, because it has User lifetime
256 {
257 let (glean, _t) = new_glean(Some(tempdir));
258 let snapshot = StorageManager
259 .snapshot_as_json(glean.storage(), "store1", true)
260 .unwrap();
261
262 assert_eq!(
263 json!(50),
264 snapshot["custom_distribution"]["telemetry.distribution"]["sum"]
265 );
266 }
267 }
268
269 #[test]
270 fn set_value_properly_sets_the_value_in_all_stores() {
271 let (glean, _t) = new_glean(None);
272 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
273
274 let metric = CustomDistributionMetric::new(
275 CommonMetricData {
276 name: "distribution".into(),
277 category: "telemetry".into(),
278 send_in_pings: store_names.clone(),
279 disabled: false,
280 lifetime: Lifetime::Ping,
281 ..Default::default()
282 },
283 1,
284 100,
285 10,
286 HistogramType::Exponential,
287 );
288
289 metric.accumulate_samples_sync(&glean, &[50]);
290
291 for store_name in store_names {
292 let snapshot = StorageManager
293 .snapshot_as_json(glean.storage(), &store_name, true)
294 .unwrap();
295
296 assert_eq!(
297 json!(50),
298 snapshot["custom_distribution"]["telemetry.distribution"]["sum"]
299 );
300 assert_eq!(
301 json!(1),
302 snapshot["custom_distribution"]["telemetry.distribution"]["values"]["29"]
303 );
304 }
305 }
306
307 // SKIPPED from glean-ac: memory distributions must not accumulate negative values
308 // This test doesn't apply to Rust, because we're using unsigned integers.
309
310 #[test]
311 fn the_accumulate_samples_api_correctly_stores_memory_values() {
312 let (glean, _t) = new_glean(None);
313
314 let metric = CustomDistributionMetric::new(
315 CommonMetricData {
316 name: "distribution".into(),
317 category: "telemetry".into(),
318 send_in_pings: vec!["store1".into()],
319 disabled: false,
320 lifetime: Lifetime::Ping,
321 ..Default::default()
322 },
323 1,
324 100,
325 10,
326 HistogramType::Exponential,
327 );
328
329 // Accumulate the samples. We intentionally do not report
330 // negative values to not trigger error reporting.
331 metric.accumulate_samples_sync(&glean, &[1, 2, 3]);
332
333 let snapshot = metric
334 .get_value(&glean, "store1")
335 .expect("Value should be stored");
336
337 // Check that we got the right sum of samples.
338 assert_eq!(snapshot.sum, 6);
339
340 // We should get a sample in 3 buckets.
341 // These numbers are a bit magic, but they correspond to
342 // `hist.sample_to_bucket_minimum(i * kb)` for `i = 1..=3`.
343 assert_eq!(1, snapshot.values[&1]);
344 assert_eq!(1, snapshot.values[&2]);
345 assert_eq!(1, snapshot.values[&3]);
346
347 // No errors should be reported.
348 assert!(
349 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()
350 );
351 }
352
353 #[test]
354 fn the_accumulate_samples_api_correctly_handles_negative_values() {
355 let (glean, _t) = new_glean(None);
356
357 let metric = CustomDistributionMetric::new(
358 CommonMetricData {
359 name: "distribution".into(),
360 category: "telemetry".into(),
361 send_in_pings: vec!["store1".into()],
362 disabled: false,
363 lifetime: Lifetime::Ping,
364 ..Default::default()
365 },
366 1,
367 100,
368 10,
369 HistogramType::Exponential,
370 );
371
372 // Accumulate the samples.
373 metric.accumulate_samples_sync(&glean, &[-1, 1, 2, 3]);
374
375 let snapshot = metric
376 .get_value(&glean, "store1")
377 .expect("Value should be stored");
378
379 // Check that we got the right sum of samples.
380 assert_eq!(snapshot.sum, 6);
381
382 // We should get a sample in 3 buckets.
383 // These numbers are a bit magic, but they correspond to
384 // `hist.sample_to_bucket_minimum(i * kb)` for `i = 1..=3`.
385 assert_eq!(1, snapshot.values[&1]);
386 assert_eq!(1, snapshot.values[&2]);
387 assert_eq!(1, snapshot.values[&3]);
388
389 // 1 error should be reported.
390 assert_eq!(
391 Ok(1),
392 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue)
393 );
394 }
395
396 #[test]
397 fn json_snapshotting_works() {
398 let (glean, _t) = new_glean(None);
399 let metric = CustomDistributionMetric::new(
400 CommonMetricData {
401 name: "distribution".into(),
402 category: "telemetry".into(),
403 send_in_pings: vec!["store1".into()],
404 disabled: false,
405 lifetime: Lifetime::Ping,
406 ..Default::default()
407 },
408 1,
409 100,
410 10,
411 HistogramType::Exponential,
412 );
413
414 metric.accumulate_samples_sync(&glean, &[50]);
415
416 let snapshot = metric.get_value(&glean, "store1");
417 assert!(snapshot.is_some());
418 }
419 }