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