toolkit/components/osfile/tests/xpcshell/test_telemetry.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_telemetry.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +"use strict";
     1.5 +
     1.6 +let {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
     1.7 +let {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {});
     1.8 +
     1.9 +// Ensure that we have a profile but that the OS.File worker is not launched
    1.10 +add_task(function* init() {
    1.11 +  do_get_profile();
    1.12 +  yield File.resetWorker();
    1.13 +});
    1.14 +
    1.15 +function getCount(histogram) {
    1.16 +  if (histogram == null) {
    1.17 +    return 0;
    1.18 +  }
    1.19 +
    1.20 +  let total = 0;
    1.21 +  for (let i of histogram.counts) {
    1.22 +    total += i;
    1.23 +  }
    1.24 +  return total;
    1.25 +}
    1.26 +
    1.27 +// Ensure that launching the OS.File worker adds data to the relevant
    1.28 +// histograms
    1.29 +add_task(function* test_startup() {
    1.30 +  let LAUNCH = "OSFILE_WORKER_LAUNCH_MS";
    1.31 +  let READY = "OSFILE_WORKER_READY_MS";
    1.32 +
    1.33 +  let before = Services.telemetry.histogramSnapshots;
    1.34 +
    1.35 +  // Launch the OS.File worker
    1.36 +  yield File.getCurrentDirectory();
    1.37 +
    1.38 +  let after = Services.telemetry.histogramSnapshots;
    1.39 +
    1.40 +
    1.41 +  do_print("Ensuring that we have recorded measures for histograms");
    1.42 +  do_check_eq(getCount(after[LAUNCH]), getCount(before[LAUNCH]) + 1);
    1.43 +  do_check_eq(getCount(after[READY]), getCount(before[READY]) + 1);
    1.44 +
    1.45 +  do_print("Ensuring that launh <= ready");
    1.46 +  do_check_true(after[LAUNCH].sum <= after[READY].sum);
    1.47 +});
    1.48 +
    1.49 +// Ensure that calling writeAtomic adds data to the relevant histograms
    1.50 +add_task(function* test_writeAtomic() {
    1.51 +  let LABEL = "OSFILE_WRITEATOMIC_JANK_MS";
    1.52 +
    1.53 +  let before = Services.telemetry.histogramSnapshots;
    1.54 +
    1.55 +  // Perform a write.
    1.56 +  let path = Path.join(Constants.Path.profileDir, "test_osfile_telemetry.tmp");
    1.57 +  yield File.writeAtomic(path, LABEL, { tmpPath: path + ".tmp" } );
    1.58 +
    1.59 +  let after = Services.telemetry.histogramSnapshots;
    1.60 +
    1.61 +  do_check_eq(getCount(after[LABEL]), getCount(before[LABEL]) + 1);
    1.62 +});
    1.63 +
    1.64 +function run_test() {
    1.65 +  run_next_test();
    1.66 +}

mercurial