Debian Codegraph @

closure-library/closure/goog/debug/tracer_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.debug.TraceTest');
16 goog.setTestOnly('goog.debug.TraceTest');
17
18 goog.require('goog.array');
19 goog.require('goog.debug.Trace');
20 goog.require('goog.testing.jsunit');
21 goog.require('goog.testing.recordFunction');
22
23 /** @type {!Function} */
24 const recorder = goog.testing.recordFunction();
25
26 function setUp() {
27 goog.debug.Trace.initCurrentTrace();
28 goog.debug.Trace.removeAllListeners();
29 recorder.reset();
30 }
31
32 function testProperEventReleaseViaResetForComment() {
33 goog.debug.Trace.startTracer('foo');
34 // The Start event and its id are released due to calling the reset method.
35 goog.debug.Trace.reset(0);
36
37 // Recycling the last Start event.
38 goog.debug.Trace.addComment('abc');
39 goog.debug.Trace.startTracer('foo');
40 goog.debug.Trace.reset(0);
41
42 var t1 = goog.debug.Trace.startTracer('f1');
43 var t2 = goog.debug.Trace.startTracer('f2');
44 assertNotEquals('The trace ids cannot repeat.', t1, t2);
45 }
46
47 function testProperEventReleaseViaThresholdForComment() {
48 var t3 = goog.debug.Trace.startTracer('foo');
49 // The Start event and its id are released due to 1000ms threshold.
50 goog.debug.Trace.stopTracer(t3, 1000);
51
52 // Recycling the last Start event.
53 goog.debug.Trace.addComment('abc');
54 goog.debug.Trace.startTracer('foo');
55 goog.debug.Trace.reset(0);
56
57 var t1 = goog.debug.Trace.startTracer('f1');
58 var t2 = goog.debug.Trace.startTracer('f2');
59 assertNotEquals('The trace ids cannot repeat.', t1, t2);
60 }
61
62 function testProperEventReleaseViaResetForStop() {
63 goog.debug.Trace.startTracer('foo');
64 goog.debug.Trace.startTracer('foo');
65 // The Start events and their ids are released because of reseting.
66 goog.debug.Trace.reset(0);
67
68 // Recycling the last two Start events.
69 var t0 = goog.debug.Trace.startTracer('foo');
70 goog.debug.Trace.stopTracer(t0);
71 goog.debug.Trace.reset(0);
72
73 var t1 = goog.debug.Trace.startTracer('fa');
74 goog.debug.Trace.startTracer('fb');
75 var t2 = goog.debug.Trace.startTracer('fc');
76 // No id is repeated.
77 assertNotEquals('The trace ids cannot repeat.', t1, t2);
78 }
79
80 function testProperEventReleaseViaThresholdForStop() {
81 var t1 = goog.debug.Trace.startTracer('f1');
82 var t2 = goog.debug.Trace.startTracer('f2');
83 // The Start events and their ids are released due to 1000ms threshold.
84 goog.debug.Trace.stopTracer(t2, 1000);
85 goog.debug.Trace.stopTracer(t1, 1000);
86
87 // Recycling the last two Start events.
88 var t0 = goog.debug.Trace.startTracer('foo');
89 goog.debug.Trace.stopTracer(t0);
90 goog.debug.Trace.reset(0);
91
92 var t1 = goog.debug.Trace.startTracer('fa');
93 goog.debug.Trace.startTracer('fb');
94 var t2 = goog.debug.Trace.startTracer('fc');
95 // No id is repeated.
96 assertNotEquals('The trace ids cannot repeat.', t1, t2);
97 }
98
99 function testTracer() {
100 var t = goog.debug.Trace.startTracer('foo');
101 var sum = 0;
102 for (var i = 0; i < 100000; i++) {
103 sum += i;
104 }
105 goog.debug.Trace.stopTracer(t);
106 var trace = goog.debug.Trace.getFormattedTrace();
107 var lines = trace.split('\n');
108 assertEquals(8, lines.length);
109 assertNotNull(lines[0].match(/^\s*\d+\.\d+\s+Start\s+foo$/));
110 assertNotNull(lines[1].match(/^\s*\d+\s+\d+\.\d+\s+Done\s+\d+ ms\s+foo$/));
111 }
112
113 function testPerf() {
114 var count = 1000;
115 var start = goog.now();
116 for (var i = 0; i < count; i++) {
117 var t = goog.debug.Trace.startTracer('foo');
118 var t2 = goog.debug.Trace.startTracer('foo.bar');
119 var t3 = goog.debug.Trace.startTracer('foo.bar.baz');
120 goog.debug.Trace.stopTracer(t3);
121 var t4 = goog.debug.Trace.startTracer('foo.bar.bim');
122 goog.debug.Trace.stopTracer(t4);
123 goog.debug.Trace.stopTracer(t2);
124 goog.debug.Trace.stopTracer(t);
125 }
126 count *= 4;
127 var end = goog.now();
128 }
129
130 /**
131 * Checks if the actual log of a fake listener matches the expectations.
132 * @param {!Array<!Array<*>>} expected The expected log from the fake listener.
133 * @param {!Function} recorder The output of {@code goog.testing.recordFunction}
134 * for logging all calls to the fake listener.
135 * @return {boolean} True if the recorder's log is expected.
136 */
137 function validateRecordedListener(expected, recorder) {
138 assertObjectEquals(
139 expected, goog.array.map(recorder.getCalls(), function(call) {
140 return call.getArguments();
141 }));
142 }
143
144 function testListenerStopTracerSilence() {
145 goog.debug.Trace.addTraceCallbacks({
146 start: goog.partial(recorder, 'start'),
147 stop: goog.partial(recorder, 'stop'),
148 comment: goog.partial(recorder, 'comment'),
149 });
150 const t = goog.debug.Trace.startTracer('first');
151 // 1000ms should be enough for silencing the tracer.
152 goog.debug.Trace.stopTracer(t, 1000);
153 validateRecordedListener([['start', t, 'first'], ['stop', t]], recorder);
154 }
155
156 function testListenerStartTracerType() {
157 goog.debug.Trace.addTraceCallbacks({
158 start: goog.partial(recorder, 'start'),
159 stop: goog.partial(recorder, 'stop'),
160 comment: goog.partial(recorder, 'comment'),
161 });
162 const t = goog.debug.Trace.startTracer('first', 'New Type');
163 goog.debug.Trace.stopTracer(t);
164 validateRecordedListener(
165 [['start', t, '[New Type] first'], ['stop', t]], recorder);
166 }
167
168 function testListenerCommentTracerType() {
169 goog.debug.Trace.addTraceCallbacks({
170 start: goog.partial(recorder, 'start'),
171 stop: goog.partial(recorder, 'stop'),
172 comment: goog.partial(recorder, 'comment'),
173 });
174 goog.debug.Trace.addComment('foo', 'bar');
175 validateRecordedListener([['comment', '[bar] foo']], recorder);
176 }
177
178 function testListenerCommentTracerTime() {
179 goog.debug.Trace.addTraceCallbacks({
180 start: goog.partial(recorder, 'start'),
181 stop: goog.partial(recorder, 'stop'),
182 comment: goog.partial(recorder, 'comment'),
183 });
184 const timestamp = goog.now() - 10;
185 goog.debug.Trace.addComment('first', null, timestamp);
186 validateRecordedListener([['comment', 'first', timestamp]], recorder);
187 }
188
189 function testListenerCommentTracerAlone() {
190 goog.debug.Trace.addTraceCallbacks({
191 comment: goog.partial(recorder, 'comment'),
192 });
193 goog.debug.Trace.addComment('foo');
194 validateRecordedListener([['comment', 'foo']], recorder);
195 }
196
197 function testListenerCommentTracerNoLog() {
198 goog.debug.Trace.addTraceCallbacks({
199 start: goog.partial(recorder, 'start'),
200 stop: goog.partial(recorder, 'stop'),
201 });
202 goog.debug.Trace.addComment('foo');
203 validateRecordedListener([], recorder);
204 }
205
206 function testListenerStartStopTracerOnly() {
207 goog.debug.Trace.addTraceCallbacks({
208 start: goog.partial(recorder, 'start'),
209 stop: goog.partial(recorder, 'stop'),
210 });
211 const t = goog.debug.Trace.startTracer('bar');
212 goog.debug.Trace.stopTracer(t);
213 validateRecordedListener([['start', t, 'bar'], ['stop', t]], recorder);
214 }
215
216 function testTwoListeners() {
217 const r1 = goog.testing.recordFunction();
218 goog.debug.Trace.addTraceCallbacks({
219 start: goog.partial(r1, 'start'),
220 stop: goog.partial(r1, 'stop'),
221 });
222 const t0 = goog.debug.Trace.startTracer('first');
223 goog.debug.Trace.stopTracer(t0);
224 const expected1 = [['start', t0, 'first'], ['stop', t0]];
225 validateRecordedListener(expected1, r1);
226
227 const r2 = goog.testing.recordFunction();
228 goog.debug.Trace.addTraceCallbacks({
229 start: goog.partial(r2, 'start'),
230 stop: goog.partial(r2, 'stop'),
231 comment: goog.partial(r2, 'comment'),
232 });
233 const t1 = goog.debug.Trace.startTracer('second', 'XType');
234 const t2 = goog.debug.Trace.startTracer('third');
235 goog.debug.Trace.addComment('NoTime');
236 const currentTime = goog.now();
237 goog.debug.Trace.addComment('WithTime', null, currentTime);
238 goog.debug.Trace.addComment('NoTime', 'YType');
239 goog.debug.Trace.stopTracer(t2);
240 goog.debug.Trace.stopTracer(t1);
241
242 const expected2 = [
243 ['start', t1, '[XType] second'],
244 ['start', t2, 'third'],
245 ['comment', 'NoTime'],
246 ['comment', 'WithTime', currentTime],
247 ['comment', '[YType] NoTime'],
248 ['stop', t2],
249 ['stop', t1],
250 ];
251 validateRecordedListener(expected2, r2);
252
253 const expectedNoComment = [
254 ['start', t0, 'first'],
255 ['stop', t0],
256 ['start', t1, '[XType] second'],
257 ['start', t2, 'third'],
258 ['stop', t2],
259 ['stop', t1],
260 ];
261 validateRecordedListener(expectedNoComment, r1);
262 }