summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/browser/browser_DynamicScalars.js
blob: 966c7c1be390c6c31a85f1dbad36cb61bffd1bf9 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"use strict";

const { TelemetryController } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryController.sys.mjs"
);
const { TelemetryUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryUtils.sys.mjs"
);

const CONTENT_CREATED = "ipc:content-created";

async function waitForProcessesScalars(
  aProcesses,
  aKeyed,
  aAdditionalCondition = data => true
) {
  await TestUtils.waitForCondition(() => {
    const scalars = aKeyed
      ? Services.telemetry.getSnapshotForKeyedScalars("main", false)
      : Services.telemetry.getSnapshotForScalars("main", false);
    return (
      aProcesses.every(p => Object.keys(scalars).includes(p)) &&
      aAdditionalCondition(scalars)
    );
  });
}

add_task(async function test_setup() {
  // Make sure the newly spawned content processes will have extended Telemetry enabled.
  // Since Telemetry reads the prefs only at process startup, flush all cached
  // and preallocated processes so they pick up the setting.
  await SpecialPowers.pushPrefEnv({
    set: [
      [TelemetryUtils.Preferences.OverridePreRelease, true],
      ["dom.ipc.processPrelaunch.enabled", false],
    ],
  });
  Services.ppmm.releaseCachedProcesses();
  await SpecialPowers.pushPrefEnv({
    set: [["dom.ipc.processPrelaunch.enabled", true]],
  });

  // And take care of the already initialized one as well.
  let canRecordExtended = Services.telemetry.canRecordExtended;
  Services.telemetry.canRecordExtended = true;
  registerCleanupFunction(
    () => (Services.telemetry.canRecordExtended = canRecordExtended)
  );
});

add_task(async function test_recording() {
  let currentPid = gBrowser.selectedBrowser.frameLoader.remoteTab.osPid;

  // Register test scalars before spawning the content process: the scalar
  // definitions will propagate to it.
  Services.telemetry.registerScalars("telemetry.test.dynamic", {
    pre_content_spawn: {
      kind: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
      keyed: false,
      record_on_release: true,
    },
    pre_content_spawn_expiration: {
      kind: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
      keyed: false,
      record_on_release: true,
    },
  });

  Services.telemetry.scalarSet(
    "telemetry.test.dynamic.pre_content_spawn_expiration",
    3
  );

  let processCreated = TestUtils.topicObserved(CONTENT_CREATED);
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank", forceNewProcess: true },
    async function (browser) {
      // Make sure our new browser is in its own process. The processCreated
      // promise should have already resolved by this point.
      await processCreated;
      let newPid = browser.frameLoader.remoteTab.osPid;
      Assert.notEqual(
        currentPid,
        newPid,
        "The new tab must spawn its own process"
      );

      // Register test scalars after spawning the content process: the scalar
      // definitions will propagate to it.
      // Also attempt to register again "pre_content_spawn_expiration" and set
      // it to expired.
      Services.telemetry.registerScalars("telemetry.test.dynamic", {
        post_content_spawn: {
          kind: Ci.nsITelemetry.SCALAR_TYPE_BOOLEAN,
          keyed: false,
          record_on_release: false,
        },
        post_content_spawn_keyed: {
          kind: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
          keyed: true,
          record_on_release: true,
        },
        pre_content_spawn_expiration: {
          kind: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
          keyed: false,
          record_on_release: true,
          expired: true,
        },
      });

      // Accumulate from the content process into both dynamic scalars.
      await SpecialPowers.spawn(browser, [], async function () {
        Services.telemetry.scalarAdd(
          "telemetry.test.dynamic.pre_content_spawn_expiration",
          1
        );
        Services.telemetry.scalarSet(
          "telemetry.test.dynamic.pre_content_spawn",
          3
        );
        Services.telemetry.scalarSet(
          "telemetry.test.dynamic.post_content_spawn",
          true
        );
        Services.telemetry.keyedScalarSet(
          "telemetry.test.dynamic.post_content_spawn_keyed",
          "testKey",
          3
        );
      });
    }
  );

  // Wait for the dynamic scalars to appear non-keyed snapshots.
  await waitForProcessesScalars(["dynamic"], true, scalars => {
    // Wait for the scalars set in the content process to be available.
    return "telemetry.test.dynamic.post_content_spawn_keyed" in scalars.dynamic;
  });

  // Verify the content of the snapshots.
  const scalars = Services.telemetry.getSnapshotForScalars("main", false);
  ok(
    "dynamic" in scalars,
    "The scalars must contain the 'dynamic' process section"
  );
  ok(
    "telemetry.test.dynamic.pre_content_spawn" in scalars.dynamic,
    "Dynamic scalars registered before a process spawns must be present."
  );
  is(
    scalars.dynamic["telemetry.test.dynamic.pre_content_spawn"],
    3,
    "The dynamic scalar must contain the expected value."
  );
  is(
    scalars.dynamic["telemetry.test.dynamic.pre_content_spawn_expiration"],
    3,
    "The dynamic scalar must not be updated after being expired."
  );
  ok(
    "telemetry.test.dynamic.post_content_spawn" in scalars.dynamic,
    "Dynamic scalars registered after a process spawns must be present."
  );
  is(
    scalars.dynamic["telemetry.test.dynamic.post_content_spawn"],
    true,
    "The dynamic scalar must contain the expected value."
  );

  // Wait for the dynamic scalars to appear in the keyed snapshots.
  await waitForProcessesScalars(["dynamic"], true);

  const keyedScalars = Services.telemetry.getSnapshotForKeyedScalars(
    "main",
    false
  );
  ok(
    "dynamic" in keyedScalars,
    "The keyed scalars must contain the 'dynamic' process section"
  );
  ok(
    "telemetry.test.dynamic.post_content_spawn_keyed" in keyedScalars.dynamic,
    "Dynamic keyed scalars registered after a process spawns must be present."
  );
  is(
    keyedScalars.dynamic["telemetry.test.dynamic.post_content_spawn_keyed"]
      .testKey,
    3,
    "The dynamic keyed scalar must contain the expected value."
  );
});

add_task(async function test_aggregation() {
  Services.telemetry.clearScalars();

  // Register test scalars before spawning the content process: the scalar
  // definitions will propagate to it. Also cheat TelemetrySession to put
  // the test scalar in the payload by using "cheattest" instead of "test" in
  // the scalar category name.
  Services.telemetry.registerScalars("telemetry.cheattest.dynamic", {
    test_aggregation: {
      kind: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
      keyed: false,
      record_on_release: true,
    },
  });

  const SCALAR_FULL_NAME = "telemetry.cheattest.dynamic.test_aggregation";
  Services.telemetry.scalarAdd(SCALAR_FULL_NAME, 1);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank", forceNewProcess: true },
    async function (browser) {
      // Accumulate from the content process into both dynamic scalars.
      await SpecialPowers.spawn(
        browser,
        [SCALAR_FULL_NAME],
        async function (aName) {
          Services.telemetry.scalarAdd(aName, 3);
        }
      );
    }
  );

  // Wait for the dynamic scalars to appear. Since we're testing that children
  // and parent data get aggregated, we might need to wait a bit more:
  // TelemetryIPCAccumulator.cpp sends batches to the parent process every 2 seconds.
  await waitForProcessesScalars(["dynamic"], false, scalarData => {
    return (
      "dynamic" in scalarData &&
      SCALAR_FULL_NAME in scalarData.dynamic &&
      scalarData.dynamic[SCALAR_FULL_NAME] == 4
    );
  });

  // Check that the definitions made it to the ping payload.
  const pingData = TelemetryController.getCurrentPingData(true);
  ok(
    "dynamic" in pingData.payload.processes,
    "The ping payload must contain the 'dynamic' process section"
  );
  is(
    pingData.payload.processes.dynamic.scalars[SCALAR_FULL_NAME],
    4,
    "The dynamic scalar must contain the aggregated parent and children data."
  );
});