summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_TelemetryEvents_buildFaster.js
blob: c7e9e5aaba589be535a1119eb686de8337136801 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/
*/

/**
 * Return the path to the definitions file for the events.
 */
function getDefinitionsPath() {
  // Write the event definition to the spec file in the binary directory.
  let definitionFile = Cc["@mozilla.org/file/local;1"].createInstance(
    Ci.nsIFile
  );
  definitionFile = Services.dirsvc.get("GreD", Ci.nsIFile);
  definitionFile.append("EventArtifactDefinitions.json");
  return definitionFile.path;
}

add_task(async function test_setup() {
  do_get_profile();
});

add_task(
  {
    // The test needs to write a file, and that fails in tests on Android.
    // We don't really need the Android coverage, so skip on Android.
    skip_if: () => AppConstants.platform == "android",
  },
  async function test_invalidJSON() {
    const INVALID_JSON = "{ invalid,JSON { {1}";
    const FILE_PATH = getDefinitionsPath();

    // Write a corrupted JSON file.
    await IOUtils.writeUTF8(FILE_PATH, INVALID_JSON, {
      mode: "overwrite",
    });

    // Simulate Firefox startup. This should not throw!
    await TelemetryController.testSetup();
    await TelemetryController.testPromiseJsProbeRegistration();

    // Cleanup.
    await TelemetryController.testShutdown();
    await IOUtils.remove(FILE_PATH);
  }
);

add_task(
  {
    // The test needs to write a file, and that fails in tests on Android.
    // We don't really need the Android coverage, so skip on Android.
    skip_if: () => AppConstants.platform == "android",
  },
  async function test_dynamicBuiltin() {
    const DYNAMIC_EVENT_SPEC = {
      "telemetry.test.builtin": {
        test: {
          objects: ["object1", "object2"],
          expires: "never",
          methods: ["test1", "test2"],
          extra_keys: ["key2", "key1"],
          record_on_release: false,
        },
      },
      // Test a new, expired event
      "telemetry.test.expired": {
        expired: {
          objects: ["object1"],
          methods: ["method1"],
          expires: AppConstants.MOZ_APP_VERSION,
          record_on_release: false,
        },
      },
      // Test overwriting static expiries
      "telemetry.test": {
        expired_version: {
          objects: ["object1"],
          methods: ["expired_version"],
          expires: "never",
          record_on_release: false,
        },
        not_expired_optout: {
          objects: ["object1"],
          methods: ["not_expired_optout"],
          expires: AppConstants.MOZ_APP_VERSION,
          record_on_release: true,
        },
      },
    };

    Telemetry.clearEvents();

    // Let's write to the definition file to also cover the file
    // loading part.
    const FILE_PATH = getDefinitionsPath();
    await IOUtils.writeJSON(FILE_PATH, DYNAMIC_EVENT_SPEC);

    // Start TelemetryController to trigger loading the specs.
    await TelemetryController.testReset();
    await TelemetryController.testPromiseJsProbeRegistration();

    // Record the events
    const TEST_EVENT_NAME = "telemetry.test.builtin";
    const DYNAMIC_EVENT_CATEGORY = "telemetry.test.expired";
    const STATIC_EVENT_CATEGORY = "telemetry.test";
    Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);
    Telemetry.setEventRecordingEnabled(DYNAMIC_EVENT_CATEGORY, true);
    Telemetry.setEventRecordingEnabled(STATIC_EVENT_CATEGORY, true);
    Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");
    Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object1", null, {
      key1: "foo",
      key2: "bar",
    });
    Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object2", null, {
      key2: "bar",
    });
    Telemetry.recordEvent(DYNAMIC_EVENT_CATEGORY, "method1", "object1");
    Telemetry.recordEvent(STATIC_EVENT_CATEGORY, "expired_version", "object1");
    Telemetry.recordEvent(
      STATIC_EVENT_CATEGORY,
      "not_expired_optout",
      "object1"
    );

    // Check the values we tried to store.
    const snapshot = Telemetry.snapshotEvents(
      Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
      false
    );
    Assert.ok(
      "parent" in snapshot,
      "Should have parent events in the snapshot."
    );

    let expected = [
      [TEST_EVENT_NAME, "test1", "object1"],
      [TEST_EVENT_NAME, "test2", "object1", null, { key1: "foo", key2: "bar" }],
      [TEST_EVENT_NAME, "test2", "object2", null, { key2: "bar" }],
      [STATIC_EVENT_CATEGORY, "expired_version", "object1"],
    ];
    let events = snapshot.parent;
    Assert.equal(
      events.length,
      expected.length,
      "Should have recorded the right amount of events."
    );
    for (let i = 0; i < expected.length; ++i) {
      Assert.deepEqual(
        events[i].slice(1),
        expected[i],
        "Should have recorded the expected event data."
      );
    }

    // Clean up.
    await TelemetryController.testShutdown();
    await IOUtils.remove(FILE_PATH);
  }
);

add_task(async function test_dynamicBuiltinEvents() {
  Telemetry.clearEvents();
  Telemetry.clearScalars();
  Telemetry.canRecordExtended = true;

  const TEST_EVENT_NAME = "telemetry.test.dynamicbuiltin";

  // Register some dynamic builtin test events.
  Telemetry.registerBuiltinEvents(TEST_EVENT_NAME, {
    // Event with only required fields.
    test1: {
      methods: ["test1"],
      objects: ["object1"],
    },
    // Event with extra_keys.
    test2: {
      methods: ["test2", "test2b"],
      objects: ["object1", "object2"],
      extra_keys: ["key1", "key2"],
    },
  });

  // Record some events.
  Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);
  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object1", null, {
    key1: "foo",
    key2: "bar",
  });
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2b", "object2", null, {
    key2: "bar",
  });
  // Now check that the snapshot contains the expected data.
  let snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok("parent" in snapshot, "Should have parent events in the snapshot.");

  // For checking event summaries
  const scalars = Telemetry.getSnapshotForKeyedScalars("main", true);
  Assert.ok(
    "parent" in scalars,
    "Should have parent scalars in the main snapshot."
  );

  let expected = [
    [TEST_EVENT_NAME, "test1", "object1"],
    [TEST_EVENT_NAME, "test2", "object1", null, { key1: "foo", key2: "bar" }],
    [TEST_EVENT_NAME, "test2b", "object2", null, { key2: "bar" }],
  ];
  let events = snapshot.parent;
  Assert.equal(
    events.length,
    expected.length,
    "Should have recorded the right amount of events."
  );
  for (let i = 0; i < expected.length; ++i) {
    Assert.deepEqual(
      events[i].slice(1),
      expected[i],
      "Should have recorded the expected event data."
    );

    const uniqueEventName = `${expected[i][0]}#${expected[i][1]}#${expected[i][2]}`;
    const summaryCount =
      scalars.parent["telemetry.event_counts"][uniqueEventName];
    Assert.equal(1, summaryCount, `${uniqueEventName} had wrong summary count`);
  }
});

add_task(async function test_dynamicBuiltinEventsDisabledByDefault() {
  Telemetry.clearEvents();
  Telemetry.canRecordExtended = true;

  const TEST_EVENT_NAME = "telemetry.test.offbydefault";

  // Register some dynamic builtin test events.
  Telemetry.registerBuiltinEvents(TEST_EVENT_NAME, {
    // Event with only required fields.
    test1: {
      methods: ["test1"],
      objects: ["object1"],
    },
  });

  // Record some events.
  // Explicitely _don't_ enable the category
  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");

  // Now check that the snapshot contains the expected data.
  let snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok(
    !("parent" in snapshot),
    "Should not have parent events in the snapshot."
  );

  // Now enable the category and record again
  Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);
  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");

  snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok("parent" in snapshot, "Should have parent events in the snapshot.");

  let expected = [[TEST_EVENT_NAME, "test1", "object1"]];
  let events = snapshot.parent;
  Assert.equal(
    events.length,
    expected.length,
    "Should have recorded the right amount of events."
  );
  for (let i = 0; i < expected.length; ++i) {
    Assert.deepEqual(
      events[i].slice(1),
      expected[i],
      "Should have recorded the expected event data."
    );
  }
});

add_task(async function test_dynamicBuiltinDontOverwriteStaticData() {
  Telemetry.clearEvents();
  Telemetry.canRecordExtended = true;

  const TEST_STATIC_EVENT_NAME = "telemetry.test";
  const TEST_EVENT_NAME = "telemetry.test.nooverwrite";

  // Register some dynamic builtin test events.
  Telemetry.registerBuiltinEvents(TEST_EVENT_NAME, {
    dynamic: {
      methods: ["dynamic"],
      objects: ["builtin", "anotherone"],
    },
  });

  // First enable the categories we're using
  Telemetry.setEventRecordingEnabled(TEST_STATIC_EVENT_NAME, true);
  Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);

  // Now record some dynamic-builtin and static events
  Telemetry.recordEvent(TEST_EVENT_NAME, "dynamic", "builtin");
  Telemetry.recordEvent(TEST_STATIC_EVENT_NAME, "test1", "object1");
  Telemetry.recordEvent(TEST_EVENT_NAME, "dynamic", "anotherone");

  let snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok("parent" in snapshot, "Should have parent events in the snapshot.");

  // All events should now be recorded in the right order
  let expected = [
    [TEST_EVENT_NAME, "dynamic", "builtin"],
    [TEST_STATIC_EVENT_NAME, "test1", "object1"],
    [TEST_EVENT_NAME, "dynamic", "anotherone"],
  ];
  let events = snapshot.parent;
  Assert.equal(
    events.length,
    expected.length,
    "Should have recorded the right amount of events."
  );
  for (let i = 0; i < expected.length; ++i) {
    Assert.deepEqual(
      events[i].slice(1),
      expected[i],
      "Should have recorded the expected event data."
    );
  }
});

add_task(async function test_dynamicBuiltinEventsOverridingStatic() {
  Telemetry.clearEvents();
  Telemetry.canRecordExtended = true;

  const TEST_EVENT_NAME = "telemetry.test";

  // Register dynamic builtin test events, overwriting existing one.
  Telemetry.registerBuiltinEvents(TEST_EVENT_NAME, {
    // Event with only required fields.
    test1: {
      methods: ["test1"],
      objects: ["object1", "object2"],
    },
    // Event with extra_keys.
    test2: {
      methods: ["test2"],
      objects: ["object1", "object2", "object3"],
      extra_keys: ["key1", "key2", "newdynamickey"],
    },
  });

  // Record some events that should be available in the static event already .
  Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);
  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object1", null, {
    key1: "foo",
    key2: "bar",
  });
  // Record events with newly added objects and keys.
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object2", null, {
    newdynamickey: "foo",
  });
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object3", null, {
    key1: "foo",
  });
  // Now check that the snapshot contains the expected data.
  let snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok("parent" in snapshot, "Should have parent events in the snapshot.");

  let expected = [
    [TEST_EVENT_NAME, "test1", "object1"],
    [TEST_EVENT_NAME, "test2", "object1", null, { key1: "foo", key2: "bar" }],
    [TEST_EVENT_NAME, "test2", "object2", null, { newdynamickey: "foo" }],
    [TEST_EVENT_NAME, "test2", "object3", null, { key1: "foo" }],
  ];
  let events = snapshot.parent;
  Assert.equal(
    events.length,
    expected.length,
    "Should have recorded the right amount of events."
  );
  for (let i = 0; i < expected.length; ++i) {
    Assert.deepEqual(
      events[i].slice(1),
      expected[i],
      "Should have recorded the expected event data."
    );
  }
});

add_task(async function test_realDynamicDontOverwrite() {
  // Real dynamic events follow similar code paths internally.
  // Let's ensure they trigger the right code path and don't overwrite.

  Telemetry.clearEvents();
  Telemetry.canRecordExtended = true;

  const TEST_EVENT_NAME = "telemetry.test";

  // Register dynamic test events, this should not overwrite existing ones.
  Telemetry.registerEvents(TEST_EVENT_NAME, {
    // Event with only required fields.
    test1: {
      methods: ["test1"],
      objects: ["object1", "object2"],
    },
    // Event with extra_keys.
    test2: {
      methods: ["test2"],
      objects: ["object1", "object2", "object3"],
      extra_keys: ["key1", "key2", "realdynamic"],
    },
  });

  // Record some events that should be available in the static event already .
  Telemetry.setEventRecordingEnabled(TEST_EVENT_NAME, true);
  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object1", null, {
    key1: "foo",
    key2: "bar",
  });
  // Record events with newly added objects and keys.
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object2", null, {
    realdynamic: "foo",
  });
  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object3", null, {
    key1: "foo",
  });
  // Now check that the snapshot contains the expected data.
  let snapshot = Telemetry.snapshotEvents(
    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
    false
  );
  Assert.ok("parent" in snapshot, "Should have parent events in the snapshot.");

  let expected = [
    [TEST_EVENT_NAME, "test1", "object1"],
    [TEST_EVENT_NAME, "test2", "object1", null, { key1: "foo", key2: "bar" }],
    [TEST_EVENT_NAME, "test2", "object3", null, { key1: "foo" }],
  ];
  let events = snapshot.parent;
  Assert.equal(
    events.length,
    expected.length,
    "Should have recorded the right amount of events."
  );
  for (let i = 0; i < expected.length; ++i) {
    Assert.deepEqual(
      events[i].slice(1),
      expected[i],
      "Should have recorded the expected event data."
    );
  }
});