summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_background_telemetry.js
blob: 4af16405a2c983ce57ac5478e6d2007a1fa895cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const HISTOGRAM = "WEBEXT_BACKGROUND_PAGE_LOAD_MS";
const HISTOGRAM_KEYED = "WEBEXT_BACKGROUND_PAGE_LOAD_MS_BY_ADDONID";

add_task(async function test_telemetry() {
  const { GleanTimingDistribution } = globalThis;

  let extension1 = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.sendMessage("loaded");
    },
  });

  let extension2 = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.sendMessage("loaded");
    },
  });

  resetTelemetryData();

  assertHistogramEmpty(HISTOGRAM);
  assertKeyedHistogramEmpty(HISTOGRAM_KEYED);
  assertGleanMetricsNoSamples({
    metricId: "backgroundPageLoad",
    gleanMetric: Glean.extensionsTiming.backgroundPageLoad,
    gleanMetricConstructor: GleanTimingDistribution,
  });

  await extension1.startup();
  await extension1.awaitMessage("loaded");

  const processSnapshot = snapshot => {
    return snapshot.sum > 0;
  };

  const processKeyedSnapshot = snapshot => {
    let res = {};
    for (let key of Object.keys(snapshot)) {
      res[key] = snapshot[key].sum > 0;
    }
    return res;
  };

  assertHistogramSnapshot(
    HISTOGRAM,
    { processSnapshot, expectedValue: true },
    `Data recorded for first extension for histogram: ${HISTOGRAM}.`
  );

  assertHistogramSnapshot(
    HISTOGRAM_KEYED,
    {
      keyed: true,
      processSnapshot: processKeyedSnapshot,
      expectedValue: {
        [extension1.extension.id]: true,
      },
    },
    `Data recorded for first extension for histogram ${HISTOGRAM_KEYED}`
  );

  assertGleanMetricsSamplesCount({
    metricId: "backgroundPageLoad",
    gleanMetric: Glean.extensionsTiming.backgroundPageLoad,
    gleanMetricConstructor: GleanTimingDistribution,
    expectedSamplesCount: 1,
  });

  let histogram = Services.telemetry.getHistogramById(HISTOGRAM);
  let histogramKeyed =
    Services.telemetry.getKeyedHistogramById(HISTOGRAM_KEYED);
  let histogramSum = histogram.snapshot().sum;
  let histogramSumExt1 = histogramKeyed.snapshot()[extension1.extension.id].sum;

  await extension2.startup();
  await extension2.awaitMessage("loaded");

  assertHistogramSnapshot(
    HISTOGRAM,
    {
      processSnapshot: snapshot => snapshot.sum > histogramSum,
      expectedValue: true,
    },
    `Data recorded for second extension for histogram: ${HISTOGRAM}.`
  );

  assertHistogramSnapshot(
    HISTOGRAM_KEYED,
    {
      keyed: true,
      processSnapshot: processKeyedSnapshot,
      expectedValue: {
        [extension1.extension.id]: true,
        [extension2.extension.id]: true,
      },
    },
    `Data recorded for second extension for histogram ${HISTOGRAM_KEYED}`
  );

  equal(
    histogramKeyed.snapshot()[extension1.extension.id].sum,
    histogramSumExt1,
    `Data recorder for first extension is unchanged on the keyed histogram ${HISTOGRAM_KEYED}`
  );

  assertGleanMetricsSamplesCount({
    metricId: "backgroundPageLoad",
    gleanMetric: Glean.extensionsTiming.backgroundPageLoad,
    gleanMetricConstructor: GleanTimingDistribution,
    expectedSamplesCount: 2,
  });

  await extension1.unload();
  await extension2.unload();
});