toolkit/components/telemetry/tests/unit/test_TelemetryStopwatch.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 const Cc = Components.classes;
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cu = Components.utils;
michael@0 7
michael@0 8 const Telemetry = Cc["@mozilla.org/base/telemetry;1"]
michael@0 9 .getService(Ci.nsITelemetry);
michael@0 10
michael@0 11 let tmpScope = {};
michael@0 12 Cu.import("resource://gre/modules/TelemetryStopwatch.jsm", tmpScope);
michael@0 13 let TelemetryStopwatch = tmpScope.TelemetryStopwatch;
michael@0 14
michael@0 15 // We can't create a histogram here since the ones created with
michael@0 16 // newHistogram are not seen by getHistogramById that the module uses.
michael@0 17 const HIST_NAME = "TELEMETRY_PING";
michael@0 18 const HIST_NAME2 = "RANGE_CHECKSUM_ERRORS";
michael@0 19
michael@0 20 let refObj = {}, refObj2 = {};
michael@0 21
michael@0 22 let originalCount1, originalCount2;
michael@0 23
michael@0 24 function run_test() {
michael@0 25 let histogram = Telemetry.getHistogramById(HIST_NAME);
michael@0 26 let snapshot = histogram.snapshot();
michael@0 27 originalCount1 = snapshot.counts.reduce(function (a,b) a += b);
michael@0 28
michael@0 29 histogram = Telemetry.getHistogramById(HIST_NAME2);
michael@0 30 snapshot = histogram.snapshot();
michael@0 31 originalCount2 = snapshot.counts.reduce(function (a,b) a += b);
michael@0 32
michael@0 33 do_check_false(TelemetryStopwatch.start(3));
michael@0 34 do_check_false(TelemetryStopwatch.start({}));
michael@0 35 do_check_false(TelemetryStopwatch.start("", 3));
michael@0 36 do_check_false(TelemetryStopwatch.start("", ""));
michael@0 37 do_check_false(TelemetryStopwatch.start({}, {}));
michael@0 38
michael@0 39 do_check_true(TelemetryStopwatch.start("mark1"));
michael@0 40 do_check_true(TelemetryStopwatch.start("mark2"));
michael@0 41
michael@0 42 do_check_true(TelemetryStopwatch.start("mark1", refObj));
michael@0 43 do_check_true(TelemetryStopwatch.start("mark2", refObj));
michael@0 44
michael@0 45 // Same timer can't be re-started before being stopped
michael@0 46 do_check_false(TelemetryStopwatch.start("mark1"));
michael@0 47 do_check_false(TelemetryStopwatch.start("mark1", refObj));
michael@0 48
michael@0 49 // Can't stop a timer that was accidentaly started twice
michael@0 50 do_check_false(TelemetryStopwatch.finish("mark1"));
michael@0 51 do_check_false(TelemetryStopwatch.finish("mark1", refObj));
michael@0 52
michael@0 53 do_check_true(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM"));
michael@0 54 try {
michael@0 55 TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM");
michael@0 56 do_throw("Non-existent histogram name should throw an error.");
michael@0 57 } catch (e) {}
michael@0 58
michael@0 59 do_check_true(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM", refObj));
michael@0 60 try {
michael@0 61 TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM", refObj);
michael@0 62 do_throw("Non-existent histogram name should throw an error.");
michael@0 63 } catch (e) {}
michael@0 64
michael@0 65 do_check_true(TelemetryStopwatch.start(HIST_NAME));
michael@0 66 do_check_true(TelemetryStopwatch.start(HIST_NAME2));
michael@0 67 do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj));
michael@0 68 do_check_true(TelemetryStopwatch.start(HIST_NAME2, refObj));
michael@0 69 do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj2));
michael@0 70 do_check_true(TelemetryStopwatch.start(HIST_NAME2, refObj2));
michael@0 71
michael@0 72 do_check_true(TelemetryStopwatch.finish(HIST_NAME));
michael@0 73 do_check_true(TelemetryStopwatch.finish(HIST_NAME2));
michael@0 74 do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj));
michael@0 75 do_check_true(TelemetryStopwatch.finish(HIST_NAME2, refObj));
michael@0 76 do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj2));
michael@0 77 do_check_true(TelemetryStopwatch.finish(HIST_NAME2, refObj2));
michael@0 78
michael@0 79 // Verify that TS.finish deleted the timers
michael@0 80 do_check_false(TelemetryStopwatch.finish(HIST_NAME));
michael@0 81 do_check_false(TelemetryStopwatch.finish(HIST_NAME, refObj));
michael@0 82
michael@0 83 // Verify that they can be used again
michael@0 84 do_check_true(TelemetryStopwatch.start(HIST_NAME));
michael@0 85 do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj));
michael@0 86 do_check_true(TelemetryStopwatch.finish(HIST_NAME));
michael@0 87 do_check_true(TelemetryStopwatch.finish(HIST_NAME, refObj));
michael@0 88
michael@0 89 do_check_false(TelemetryStopwatch.finish("unknown-mark")); // Unknown marker
michael@0 90 do_check_false(TelemetryStopwatch.finish("unknown-mark", {})); // Unknown object
michael@0 91 do_check_false(TelemetryStopwatch.finish(HIST_NAME, {})); // Known mark on unknown object
michael@0 92
michael@0 93 // Test cancel
michael@0 94 do_check_true(TelemetryStopwatch.start(HIST_NAME));
michael@0 95 do_check_true(TelemetryStopwatch.start(HIST_NAME, refObj));
michael@0 96 do_check_true(TelemetryStopwatch.cancel(HIST_NAME));
michael@0 97 do_check_true(TelemetryStopwatch.cancel(HIST_NAME, refObj));
michael@0 98
michael@0 99 // Verify that can not cancel twice
michael@0 100 do_check_false(TelemetryStopwatch.cancel(HIST_NAME));
michael@0 101 do_check_false(TelemetryStopwatch.cancel(HIST_NAME, refObj));
michael@0 102
michael@0 103 // Verify that cancel removes the timers
michael@0 104 do_check_false(TelemetryStopwatch.finish(HIST_NAME));
michael@0 105 do_check_false(TelemetryStopwatch.finish(HIST_NAME, refObj));
michael@0 106
michael@0 107 finishTest();
michael@0 108 }
michael@0 109
michael@0 110 function finishTest() {
michael@0 111 let histogram = Telemetry.getHistogramById(HIST_NAME);
michael@0 112 let snapshot = histogram.snapshot();
michael@0 113 let newCount = snapshot.counts.reduce(function (a,b) a += b);
michael@0 114
michael@0 115 do_check_eq(newCount - originalCount1, 5, "The correct number of histograms were added for histogram 1.");
michael@0 116
michael@0 117 histogram = Telemetry.getHistogramById(HIST_NAME2);
michael@0 118 snapshot = histogram.snapshot();
michael@0 119 newCount = snapshot.counts.reduce(function (a,b) a += b);
michael@0 120
michael@0 121 do_check_eq(newCount - originalCount2, 3, "The correct number of histograms were added for histogram 2.");
michael@0 122 }

mercurial