| 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 |
#[test] |
| 18 |
fn serializer_should_correctly_serialize_memory_distribution() { |
| 19 |
let (mut tempdir, _) = tempdir(); |
| 20 |
|
| 21 |
let memory_unit = MemoryUnit::Kilobyte; |
| 22 |
let kb = 1024; |
| 23 |
|
| 24 |
{ |
| 25 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 26 |
tempdir = dir; |
| 27 |
|
| 28 |
let metric = MemoryDistributionMetric::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 |
memory_unit, |
| 38 |
); |
| 39 |
|
| 40 |
metric.accumulate_sync(&glean, 100_000); |
| 41 |
|
| 42 |
let snapshot = metric |
| 43 |
.get_value(&glean, "store1") |
| 44 |
.expect("Value should be stored"); |
| 45 |
|
| 46 |
assert_eq!(snapshot.sum, 100_000 * kb); |
| 47 |
} |
| 48 |
|
| 49 |
// Make a new Glean instance here, which should force reloading of the data from disk |
| 50 |
// so we can ensure it persisted, because it has User lifetime |
| 51 |
{ |
| 52 |
let (glean, _t) = new_glean(Some(tempdir)); |
| 53 |
let snapshot = StorageManager |
| 54 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 55 |
.unwrap(); |
| 56 |
|
| 57 |
// We check the exact format to catch changes to the serialization. |
| 58 |
let expected = json!({ |
| 59 |
"sum": 100_000 * kb, |
| 60 |
"values": { |
| 61 |
"99108124": 1, |
| 62 |
} |
| 63 |
}); |
| 64 |
assert_eq!( |
| 65 |
expected, |
| 66 |
snapshot["memory_distribution"]["telemetry.distribution"] |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn set_value_properly_sets_the_value_in_all_stores() { |
| 73 |
let (glean, _t) = new_glean(None); |
| 74 |
let store_names: Vec<String> = vec!["store1".into(), "store2".into()]; |
| 75 |
|
| 76 |
let metric = MemoryDistributionMetric::new( |
| 77 |
CommonMetricData { |
| 78 |
name: "distribution".into(), |
| 79 |
category: "telemetry".into(), |
| 80 |
send_in_pings: store_names.clone(), |
| 81 |
disabled: false, |
| 82 |
lifetime: Lifetime::Ping, |
| 83 |
..Default::default() |
| 84 |
}, |
| 85 |
MemoryUnit::Byte, |
| 86 |
); |
| 87 |
|
| 88 |
metric.accumulate_sync(&glean, 100_000); |
| 89 |
|
| 90 |
// We check the exact format to catch changes to the serialization. |
| 91 |
let expected = json!({ |
| 92 |
"sum": 100_000, |
| 93 |
"values": { |
| 94 |
"96785": 1, |
| 95 |
} |
| 96 |
}); |
| 97 |
for store_name in store_names { |
| 98 |
let snapshot = StorageManager |
| 99 |
.snapshot_as_json(glean.storage(), &store_name, true) |
| 100 |
.unwrap(); |
| 101 |
|
| 102 |
assert_eq!( |
| 103 |
expected, |
| 104 |
snapshot["memory_distribution"]["telemetry.distribution"] |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// SKIPPED from glean-ac: memory distributions must not accumulate negative values |
| 110 |
// This test doesn't apply to Rust, because we're using unsigned integers. |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn the_accumulate_samples_api_correctly_stores_memory_values() { |
| 114 |
let (glean, _t) = new_glean(None); |
| 115 |
|
| 116 |
let metric = MemoryDistributionMetric::new( |
| 117 |
CommonMetricData { |
| 118 |
name: "distribution".into(), |
| 119 |
category: "telemetry".into(), |
| 120 |
send_in_pings: vec!["store1".into()], |
| 121 |
disabled: false, |
| 122 |
lifetime: Lifetime::Ping, |
| 123 |
..Default::default() |
| 124 |
}, |
| 125 |
MemoryUnit::Kilobyte, |
| 126 |
); |
| 127 |
|
| 128 |
// Accumulate the samples. We intentionally do not report |
| 129 |
// negative values to not trigger error reporting. |
| 130 |
metric.accumulate_samples_sync(&glean, [1, 2, 3].to_vec()); |
| 131 |
|
| 132 |
let snapshot = metric |
| 133 |
.get_value(&glean, "store1") |
| 134 |
.expect("Value should be stored"); |
| 135 |
|
| 136 |
let kb = 1024; |
| 137 |
|
| 138 |
// Check that we got the right sum of samples. |
| 139 |
assert_eq!(snapshot.sum, 6 * kb); |
| 140 |
|
| 141 |
// We should get a sample in 3 buckets. |
| 142 |
// These numbers are a bit magic, but they correspond to |
| 143 |
// `hist.sample_to_bucket_minimum(i * kb)` for `i = 1..=3`. |
| 144 |
assert_eq!(1, snapshot.values[&1023]); |
| 145 |
assert_eq!(1, snapshot.values[&2047]); |
| 146 |
assert_eq!(1, snapshot.values[&3024]); |
| 147 |
|
| 148 |
// No errors should be reported. |
| 149 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn the_accumulate_samples_api_correctly_handles_negative_values() { |
| 154 |
let (glean, _t) = new_glean(None); |
| 155 |
|
| 156 |
let metric = MemoryDistributionMetric::new( |
| 157 |
CommonMetricData { |
| 158 |
name: "distribution".into(), |
| 159 |
category: "telemetry".into(), |
| 160 |
send_in_pings: vec!["store1".into()], |
| 161 |
disabled: false, |
| 162 |
lifetime: Lifetime::Ping, |
| 163 |
..Default::default() |
| 164 |
}, |
| 165 |
MemoryUnit::Kilobyte, |
| 166 |
); |
| 167 |
|
| 168 |
// Accumulate the samples. |
| 169 |
metric.accumulate_samples_sync(&glean, [-1, 1, 2, 3].to_vec()); |
| 170 |
|
| 171 |
let snapshot = metric |
| 172 |
.get_value(&glean, "store1") |
| 173 |
.expect("Value should be stored"); |
| 174 |
|
| 175 |
let kb = 1024; |
| 176 |
|
| 177 |
// Check that we got the right sum of samples. |
| 178 |
assert_eq!(snapshot.sum, 6 * kb); |
| 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[&1023]); |
| 184 |
assert_eq!(1, snapshot.values[&2047]); |
| 185 |
assert_eq!(1, snapshot.values[&3024]); |
| 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 |
} |