Bug 1467705 - Add test coverage for histogram packing routines. r=chutten
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Thu, 14 Jun 2018 20:51:10 +0200
changeset 476830 915699f04f0359b1cea589763f429097c5d7adbf
parent 476829 bde911a9320ae5844f01f558ffc3670d5c0fe24d
child 476831 7cd314fe93e3599313c63ebf63029e5f85767fb3
push id9374
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:43:20 +0000
treeherdermozilla-beta@160e085dfb0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1467705
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1467705 - Add test coverage for histogram packing routines. r=chutten MozReview-Commit-ID: LcGGQzYpRGJ
toolkit/components/telemetry/tests/unit/test_TelemetryUtils.js
toolkit/components/telemetry/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryUtils.js
@@ -0,0 +1,173 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm", this);
+ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm", this);
+
+add_task(async function testHistogramPacking() {
+  const HISTOGRAM_SNAPSHOT = {
+    sample_process: {
+      HISTOGRAM_1_DATA: {
+        counts: [
+          1, 0, 0
+        ],
+        ranges: [
+          0, 1, 2
+        ],
+        max: 2,
+        min: 1,
+        sum: 1,
+        histogram_type: 4
+      },
+      TELEMETRY_TEST_HISTOGRAM_2_DATA: {
+        counts: [
+          1, 0, 0
+        ],
+        ranges: [
+          0, 1, 2
+        ],
+        max: 2,
+        min: 1,
+        sum: 1,
+        histogram_type: 4
+      },
+    },
+  };
+
+  const HISTOGRAM_1_DATA = {
+    range: [1, 2],
+    bucket_count: 3,
+    histogram_type: 4,
+    values: {
+      "0": 1,
+      "1": 0
+    },
+    sum: 1
+  };
+
+  const packedHistograms =
+    TelemetryUtils.packHistograms(HISTOGRAM_SNAPSHOT, false /* testingMode */);
+
+  // Check that it worked and that TELEMETRY_TEST_* got filtered.
+  const EXPECTED_DATA =  {
+    sample_process: {
+      HISTOGRAM_1_DATA,
+    }
+  };
+  Assert.ok(!("TELEMETRY_TEST_HISTOGRAM_2_DATA" in packedHistograms.sample_process),
+            "Test histograms must not be reported outside of test mode.");
+  Assert.ok(ObjectUtils.deepEqual(EXPECTED_DATA, packedHistograms),
+            "Packed data must be correct.");
+
+  // Do the same packing in testing mode.
+  const packedHistogramsTest =
+    TelemetryUtils.packHistograms(HISTOGRAM_SNAPSHOT, true /* testingMode */);
+
+  // Check that TELEMETRY_TEST_* is there.
+  const EXPECTED_DATA_TEST =  {
+    sample_process: {
+      HISTOGRAM_1_DATA,
+      TELEMETRY_TEST_HISTOGRAM_2_DATA: HISTOGRAM_1_DATA
+    }
+  };
+  Assert.ok("TELEMETRY_TEST_HISTOGRAM_2_DATA" in packedHistogramsTest.sample_process,
+            "Test histograms must be reported in test mode.");
+  Assert.ok(ObjectUtils.deepEqual(EXPECTED_DATA_TEST, packedHistogramsTest),
+            "Packed data must be correct.");
+});
+
+add_task(async function testKeyedHistogramPacking() {
+  const KEYED_HISTOGRAM_SNAPSHOT = {
+    sample_process: {
+      HISTOGRAM_1_DATA: {
+        someKey: {
+          counts: [
+            1, 0, 0
+          ],
+          ranges: [
+            0, 1, 2
+          ],
+          max: 2,
+          min: 1,
+          sum: 1,
+          histogram_type: 4
+        },
+        otherKey: {
+          counts: [
+            1, 0, 0
+          ],
+          ranges: [
+            0, 1, 2
+          ],
+          max: 2,
+          min: 1,
+          sum: 1,
+          histogram_type: 4
+        }
+      },
+      TELEMETRY_TEST_HISTOGRAM_2_DATA: {
+        someKey: {
+          counts: [
+            1, 0, 0
+          ],
+          ranges: [
+            0, 1, 2
+          ],
+          max: 2,
+          min: 1,
+          sum: 1,
+          histogram_type: 4
+        }
+      },
+    },
+  };
+
+  const someKey = {
+    range: [1, 2],
+    bucket_count: 3,
+    histogram_type: 4,
+    values: {
+      "0": 1,
+      "1": 0
+    },
+    sum: 1
+  };
+
+  const packedKeyedHistograms =
+    TelemetryUtils.packKeyedHistograms(KEYED_HISTOGRAM_SNAPSHOT, false /* testingMode */);
+
+  // Check that it worked and that TELEMETRY_TEST_* got filtered.
+  const EXPECTED_DATA =  {
+    sample_process: {
+      HISTOGRAM_1_DATA: {
+        someKey,
+        otherKey: someKey
+      }
+    }
+  };
+  Assert.ok(!("TELEMETRY_TEST_HISTOGRAM_2_DATA" in packedKeyedHistograms.sample_process),
+            "Test histograms must not be reported outside of test mode.");
+  Assert.ok(ObjectUtils.deepEqual(EXPECTED_DATA, packedKeyedHistograms),
+            "Packed data must be correct.");
+
+  // Do the same packing in testing mode.
+  const packedKeyedHistogramsTest =
+    TelemetryUtils.packKeyedHistograms(KEYED_HISTOGRAM_SNAPSHOT, true /* testingMode */);
+
+  // Check that TELEMETRY_TEST_* is there.
+  const EXPECTED_DATA_TEST =  {
+    sample_process: {
+      HISTOGRAM_1_DATA: {
+        someKey,
+        otherKey: someKey
+      },
+      TELEMETRY_TEST_HISTOGRAM_2_DATA: {
+        someKey,
+      }
+    }
+  };
+  Assert.ok("TELEMETRY_TEST_HISTOGRAM_2_DATA" in packedKeyedHistogramsTest.sample_process,
+            "Test histograms must be reported in test mode.");
+  Assert.ok(ObjectUtils.deepEqual(EXPECTED_DATA_TEST, packedKeyedHistogramsTest),
+            "Packed data must be correct.");
+});
--- a/toolkit/components/telemetry/tests/unit/xpcshell.ini
+++ b/toolkit/components/telemetry/tests/unit/xpcshell.ini
@@ -78,8 +78,9 @@ skip-if = os == "android" # Disabled due
 [test_TelemetryEvents_buildFaster.js]
 [test_ChildEvents.js]
 skip-if = os == "android" # Disabled due to crashes (see bug 1331366)
 [test_TelemetryModules.js]
 [test_PingSender.js]
 skip-if = (os == "android") || (os == "linux" && bits == 32)
 [test_TelemetryGC.js]
 [test_TelemetryAndroidEnvironment.js]
+[test_TelemetryUtils.js]