summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_telemetry.js
blob: dc5104443d8d8bdd645e24ab9791b987fddce573 (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
"use strict";

var {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
var {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {});

// Ensure that we have a profile but that the OS.File worker is not launched
add_task(function* init() {
  do_get_profile();
  yield File.resetWorker();
});

function getCount(histogram) {
  if (histogram == null) {
    return 0;
  }

  let total = 0;
  for (let i of histogram.counts) {
    total += i;
  }
  return total;
}

// Ensure that launching the OS.File worker adds data to the relevant
// histograms
add_task(function* test_startup() {
  let LAUNCH = "OSFILE_WORKER_LAUNCH_MS";
  let READY = "OSFILE_WORKER_READY_MS";

  let before = Services.telemetry.histogramSnapshots;

  // Launch the OS.File worker
  yield File.getCurrentDirectory();

  let after = Services.telemetry.histogramSnapshots;


  do_print("Ensuring that we have recorded measures for histograms");
  do_check_eq(getCount(after[LAUNCH]), getCount(before[LAUNCH]) + 1);
  do_check_eq(getCount(after[READY]), getCount(before[READY]) + 1);

  do_print("Ensuring that launh <= ready");
  do_check_true(after[LAUNCH].sum <= after[READY].sum);
});

// Ensure that calling writeAtomic adds data to the relevant histograms
add_task(function* test_writeAtomic() {
  let LABEL = "OSFILE_WRITEATOMIC_JANK_MS";

  let before = Services.telemetry.histogramSnapshots;

  // Perform a write.
  let path = Path.join(Constants.Path.profileDir, "test_osfile_telemetry.tmp");
  yield File.writeAtomic(path, LABEL, { tmpPath: path + ".tmp" } );

  let after = Services.telemetry.histogramSnapshots;

  do_check_eq(getCount(after[LABEL]), getCount(before[LABEL]) + 1);
});

function run_test() {
  run_next_test();
}