summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_TelemetryStopwatch.js
blob: d9e5e08625c2e21eb79405d80a5de57069041c0b (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const HIST_NAME = "TELEMETRY_SEND_SUCCESS";
const HIST_NAME2 = "RANGE_CHECKSUM_ERRORS";
const KEYED_HIST = { id: "TELEMETRY_INVALID_PING_TYPE_SUBMITTED", key: "TEST" };

var refObj = {},
  refObj2 = {};

var originalCount1, originalCount2, originalCount3;

function run_test() {
  let histogram = Telemetry.getHistogramById(HIST_NAME);
  let snapshot = histogram.snapshot();
  originalCount1 = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  histogram = Telemetry.getHistogramById(HIST_NAME2);
  snapshot = histogram.snapshot();
  originalCount2 = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  histogram = Telemetry.getKeyedHistogramById(KEYED_HIST.id);
  snapshot = histogram.snapshot()[KEYED_HIST.key] || { values: [] };
  originalCount3 = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  Assert.ok(TelemetryStopwatch.start("mark1"));
  Assert.ok(TelemetryStopwatch.start("mark2"));

  Assert.ok(TelemetryStopwatch.start("mark1", refObj));
  Assert.ok(TelemetryStopwatch.start("mark2", refObj));

  // Same timer can't be re-started before being stopped
  Assert.ok(!TelemetryStopwatch.start("mark1"));
  Assert.ok(!TelemetryStopwatch.start("mark1", refObj));

  // Can't stop a timer that was accidentaly started twice
  Assert.ok(!TelemetryStopwatch.finish("mark1"));
  Assert.ok(!TelemetryStopwatch.finish("mark1", refObj));

  Assert.ok(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM"));
  Assert.ok(!TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM"));

  Assert.ok(TelemetryStopwatch.start("NON-EXISTENT_HISTOGRAM", refObj));
  Assert.ok(!TelemetryStopwatch.finish("NON-EXISTENT_HISTOGRAM", refObj));

  Assert.ok(!TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2, refObj));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj2));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2, refObj2));

  Assert.ok(TelemetryStopwatch.start(HIST_NAME));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME2));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME2, refObj));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME, refObj2));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME2, refObj2));

  Assert.ok(TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME2));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME2, refObj));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME, refObj2));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME2, refObj2));

  Assert.ok(TelemetryStopwatch.finish(HIST_NAME));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME2));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME2, refObj));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME, refObj2));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME2, refObj2));

  Assert.ok(!TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2, refObj));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj2));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME2, refObj2));

  // Verify that TS.finish deleted the timers
  Assert.ok(!TelemetryStopwatch.finish(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.finish(HIST_NAME, refObj));

  // Verify that they can be used again
  Assert.ok(TelemetryStopwatch.start(HIST_NAME));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME));
  Assert.ok(TelemetryStopwatch.finish(HIST_NAME, refObj));

  Assert.ok(!TelemetryStopwatch.finish("unknown-mark")); // Unknown marker
  Assert.ok(!TelemetryStopwatch.finish("unknown-mark", {})); // Unknown object
  Assert.ok(!TelemetryStopwatch.finish(HIST_NAME, {})); // Known mark on unknown object

  // Test cancel
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME));
  Assert.ok(TelemetryStopwatch.start(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(TelemetryStopwatch.cancel(HIST_NAME));
  Assert.ok(TelemetryStopwatch.cancel(HIST_NAME, refObj));

  // Verify that can not cancel twice
  Assert.ok(!TelemetryStopwatch.cancel(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.cancel(HIST_NAME, refObj));

  // Verify that cancel removes the timers
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.running(HIST_NAME, refObj));
  Assert.ok(!TelemetryStopwatch.finish(HIST_NAME));
  Assert.ok(!TelemetryStopwatch.finish(HIST_NAME, refObj));

  // Verify that keyed histograms can be started.
  Assert.ok(!TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY1"));
  Assert.ok(!TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY2"));
  Assert.ok(!TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY1", refObj));
  Assert.ok(!TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY2", refObj));

  Assert.ok(TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY1"));
  Assert.ok(TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY2"));
  Assert.ok(TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY1", refObj));
  Assert.ok(TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY2", refObj));

  Assert.ok(TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY1"));
  Assert.ok(TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY2"));
  Assert.ok(TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY1", refObj));
  Assert.ok(TelemetryStopwatch.runningKeyed("HISTOGRAM", "KEY2", refObj));

  // Restarting keyed histograms should fail.
  Assert.ok(!TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY1"));
  Assert.ok(!TelemetryStopwatch.startKeyed("HISTOGRAM", "KEY1", refObj));

  // Finishing a stopwatch of a non existing histogram should return false.
  Assert.ok(!TelemetryStopwatch.finishKeyed("HISTOGRAM", "KEY2"));
  Assert.ok(!TelemetryStopwatch.finishKeyed("HISTOGRAM", "KEY2", refObj));

  // Starting & finishing a keyed stopwatch for an existing histogram should work.
  Assert.ok(TelemetryStopwatch.startKeyed(KEYED_HIST.id, KEYED_HIST.key));
  Assert.ok(TelemetryStopwatch.finishKeyed(KEYED_HIST.id, KEYED_HIST.key));
  // Verify that TS.finish deleted the timers
  Assert.ok(!TelemetryStopwatch.runningKeyed(KEYED_HIST.id, KEYED_HIST.key));

  // Verify that they can be used again
  Assert.ok(TelemetryStopwatch.startKeyed(KEYED_HIST.id, KEYED_HIST.key));
  Assert.ok(TelemetryStopwatch.finishKeyed(KEYED_HIST.id, KEYED_HIST.key));

  Assert.ok(!TelemetryStopwatch.finishKeyed("unknown-mark", "unknown-key"));
  Assert.ok(!TelemetryStopwatch.finishKeyed(KEYED_HIST.id, "unknown-key"));

  // Verify that keyed histograms can only be canceled through "keyed" API.
  Assert.ok(TelemetryStopwatch.startKeyed(KEYED_HIST.id, KEYED_HIST.key));
  Assert.throws(
    () => TelemetryStopwatch.cancel(KEYED_HIST.id, KEYED_HIST.key),
    /is not an object/
  );
  Assert.ok(TelemetryStopwatch.cancelKeyed(KEYED_HIST.id, KEYED_HIST.key));
  Assert.ok(!TelemetryStopwatch.cancelKeyed(KEYED_HIST.id, KEYED_HIST.key));

  finishTest();
}

function finishTest() {
  let histogram = Telemetry.getHistogramById(HIST_NAME);
  let snapshot = histogram.snapshot();
  let newCount = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  Assert.equal(
    newCount - originalCount1,
    5,
    "The correct number of histograms were added for histogram 1."
  );

  histogram = Telemetry.getHistogramById(HIST_NAME2);
  snapshot = histogram.snapshot();
  newCount = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  Assert.equal(
    newCount - originalCount2,
    3,
    "The correct number of histograms were added for histogram 2."
  );

  histogram = Telemetry.getKeyedHistogramById(KEYED_HIST.id);
  snapshot = histogram.snapshot()[KEYED_HIST.key];
  newCount = Object.values(snapshot.values).reduce((a, b) => (a += b), 0);

  Assert.equal(
    newCount - originalCount3,
    2,
    "The correct number of histograms were added for histogram 3."
  );
}