michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: const Telemetry = Cc["@mozilla.org/base/telemetry;1"] michael@0: .getService(Ci.nsITelemetry); michael@0: michael@0: let tmpScope = {}; michael@0: Cu.import("resource://gre/modules/TelemetryStopwatch.jsm", tmpScope); michael@0: let TelemetryStopwatch = tmpScope.TelemetryStopwatch; michael@0: michael@0: // We can't create a histogram here since the ones created with michael@0: // newHistogram are not seen by getHistogramById that the module uses. michael@0: const HIST_NAME = "TELEMETRY_PING"; michael@0: const HIST_NAME2 = "RANGE_CHECKSUM_ERRORS"; michael@0: michael@0: let refObj = {}, refObj2 = {}; michael@0: michael@0: let originalCount1, originalCount2; michael@0: michael@0: function run_test() { michael@0: let histogram = Telemetry.getHistogramById(HIST_NAME); michael@0: let snapshot = histogram.snapshot(); michael@0: originalCount1 = snapshot.counts.reduce(function (a,b) a += b); michael@0: michael@0: histogram = Telemetry.getHistogramById(HIST_NAME2); michael@0: snapshot = histogram.snapshot(); michael@0: originalCount2 = snapshot.counts.reduce(function (a,b) a += b); michael@0: michael@0: do_check_false(TelemetryStopwatch.start(3)); michael@0: do_check_false(TelemetryStopwatch.start({})); michael@0: do_check_false(TelemetryStopwatch.start("", 3)); michael@0: do_check_false(TelemetryStopwatch.start("", "")); michael@0: do_check_false(TelemetryStopwatch.start({}, {})); michael@0: michael@0: do_check_true(TelemetryStopwatch.start("mark1")); michael@0: do_check_true(TelemetryStopwatch.start("mark2")); michael@0: michael@0: do_check_true(TelemetryStopwatch.start("mark1", refObj)); michael@0: do_check_true(TelemetryStopwatch.start("mark2", refObj)); michael@0: michael@0: // Same timer can't be re-started before being stopped michael@0: do_check_false(TelemetryStopwatch.start("mark1")); michael@0: do_check_false(TelemetryStopwatch.start("mark1", refObj)); michael@0: michael@0: // Can't stop a timer that was accidentaly started twice michael@0: do_check_false(TelemetryStopwatch.finish("mark1")); michael@0: do_check_false(TelemetryStopwatch.finish("mark1", refObj)); michael@0: michael@0: do_check_true(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM")); michael@0: try { michael@0: TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM"); michael@0: do_throw("Non-existent histogram name should throw an error."); michael@0: } catch (e) {} michael@0: michael@0: do_check_true(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM", refObj)); michael@0: try { michael@0: TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM", refObj); michael@0: do_throw("Non-existent histogram name should throw an error."); michael@0: } catch (e) {} michael@0: michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME2)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME2, refObj)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj2)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME2, refObj2)); michael@0: michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME2)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME2, refObj)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj2)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME2, refObj2)); michael@0: michael@0: // Verify that TS.finish deleted the timers michael@0: do_check_false(TelemetryStopwatch.finish(HIST_NAME)); michael@0: do_check_false(TelemetryStopwatch.finish(HIST_NAME, refObj)); michael@0: michael@0: // Verify that they can be used again michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj)); michael@0: michael@0: do_check_false(TelemetryStopwatch.finish("unknown-mark")); // Unknown marker michael@0: do_check_false(TelemetryStopwatch.finish("unknown-mark", {})); // Unknown object michael@0: do_check_false(TelemetryStopwatch.finish(HIST_NAME, {})); // Known mark on unknown object michael@0: michael@0: // Test cancel michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj)); michael@0: do_check_true(TelemetryStopwatch.cancel(HIST_NAME)); michael@0: do_check_true(TelemetryStopwatch.cancel(HIST_NAME, refObj)); michael@0: michael@0: // Verify that can not cancel twice michael@0: do_check_false(TelemetryStopwatch.cancel(HIST_NAME)); michael@0: do_check_false(TelemetryStopwatch.cancel(HIST_NAME, refObj)); michael@0: michael@0: // Verify that cancel removes the timers michael@0: do_check_false(TelemetryStopwatch.finish(HIST_NAME)); michael@0: do_check_false(TelemetryStopwatch.finish(HIST_NAME, refObj)); michael@0: michael@0: finishTest(); michael@0: } michael@0: michael@0: function finishTest() { michael@0: let histogram = Telemetry.getHistogramById(HIST_NAME); michael@0: let snapshot = histogram.snapshot(); michael@0: let newCount = snapshot.counts.reduce(function (a,b) a += b); michael@0: michael@0: do_check_eq(newCount - originalCount1, 5, "The correct number of histograms were added for histogram 1."); michael@0: michael@0: histogram = Telemetry.getHistogramById(HIST_NAME2); michael@0: snapshot = histogram.snapshot(); michael@0: newCount = snapshot.counts.reduce(function (a,b) a += b); michael@0: michael@0: do_check_eq(newCount - originalCount2, 3, "The correct number of histograms were added for histogram 2."); michael@0: }