Debian Codegraph @

closure-library/closure/goog/testing/performancetimer_test.js

raw
1 // Copyright 2008 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.testing.PerformanceTimerTest');
16 goog.setTestOnly('goog.testing.PerformanceTimerTest');
17
18 goog.require('goog.async.Deferred');
19 goog.require('goog.dom');
20 goog.require('goog.math');
21 goog.require('goog.testing.MockClock');
22 goog.require('goog.testing.PerformanceTimer');
23 goog.require('goog.testing.jsunit');
24
25 var mockClock;
26 var sandbox;
27 var timer;
28 function setUpPage() {
29 sandbox = document.getElementById('sandbox');
30 }
31
32 function setUp() {
33 mockClock = new goog.testing.MockClock(true);
34 timer = new goog.testing.PerformanceTimer();
35 }
36
37 function tearDown() {
38 mockClock.dispose();
39 timer = null;
40 goog.dom.removeChildren(sandbox);
41 }
42
43 function testConstructor() {
44 assertTrue(
45 'Timer must be an instance of goog.testing.PerformanceTimer',
46 timer instanceof goog.testing.PerformanceTimer);
47 assertEquals(
48 'Timer must collect the default number of samples', 10,
49 timer.getNumSamples());
50 assertEquals(
51 'Timer must have the default timeout interval', 5000,
52 timer.getTimeoutInterval());
53 }
54
55 function testRun_noSetUpOrTearDown() {
56 runAndAssert(false, false, false);
57 }
58
59 function testRun_withSetup() {
60 runAndAssert(true, false, false);
61 }
62
63 function testRun_withTearDown() {
64 runAndAssert(false, true, false);
65 }
66
67 function testRun_withSetUpAndTearDown() {
68 runAndAssert(true, true, false);
69 }
70
71 function testRunAsync_noSetUpOrTearDown() {
72 runAndAssert(false, false, true);
73 }
74
75 function testRunAsync_withSetup() {
76 runAndAssert(true, false, true);
77 }
78
79 function testRunAsync_withTearDown() {
80 runAndAssert(false, true, true);
81 }
82
83 function testRunAsync_withSetUpAndTearDown() {
84 runAndAssert(true, true, true);
85 }
86
87 /**
88 * @param {boolean} useSetUp
89 * @param {boolean} useTearDown
90 * @param {boolean} runAsync
91 */
92 function runAndAssert(useSetUp, useTearDown, runAsync) {
93 var fakeExecutionTime = [100, 95, 98, 104, 130, 101, 96, 98, 90, 103];
94 var count = 0;
95 var testFunction = function() {
96 mockClock.tick(fakeExecutionTime[count++]);
97 if (runAsync) {
98 var deferred = new goog.async.Deferred();
99 deferred.callback();
100 return deferred;
101 }
102 };
103
104 var setUpCount = 0;
105 var setUpFunction = function() {
106 // Should have no effect on total time.
107 mockClock.tick(7);
108 setUpCount++;
109 if (runAsync) {
110 var deferred = new goog.async.Deferred();
111 deferred.callback();
112 return deferred;
113 }
114 };
115
116 var tearDownCount = 0;
117 var tearDownFunction = function() {
118 // Should have no effect on total time.
119 mockClock.tick(11);
120 tearDownCount++;
121 if (runAsync) {
122 var deferred = new goog.async.Deferred();
123 deferred.callback();
124 return deferred;
125 }
126 };
127
128 // Fast test function should complete successfully in under 5 seconds...
129 var task = new goog.testing.PerformanceTimer.Task(testFunction);
130 if (useSetUp) {
131 task.withSetUp(setUpFunction);
132 }
133 if (useTearDown) {
134 task.withTearDown(tearDownFunction);
135 }
136 if (runAsync) {
137 var assertsRan = false;
138 var deferred = timer.runAsyncTask(task);
139 deferred.addCallback(function(results) {
140 assertsRan = assertResults(
141 results, useSetUp, useTearDown, setUpCount, tearDownCount,
142 fakeExecutionTime);
143 });
144 assertTrue(assertsRan);
145 } else {
146 var results = timer.runTask(task);
147 assertResults(
148 results, useSetUp, useTearDown, setUpCount, tearDownCount,
149 fakeExecutionTime);
150 }
151 }
152
153 /**
154 * @param {Array} results
155 * @param {boolean} useSetUp
156 * @param {boolean} useTearDown
157 * @param {boolean} setUpCount
158 * @param {boolean} tearDownCount
159 * @param {boolean} fakeExecutionTime
160 * @return {boolean} true
161 */
162 function assertResults(
163 results, useSetUp, useTearDown, setUpCount, tearDownCount,
164 fakeExecutionTime) {
165 assertNotNull('Results must be available.', results);
166
167 assertEquals(
168 'Average is wrong.', goog.math.average.apply(null, fakeExecutionTime),
169 results['average']);
170 assertEquals(
171 'Standard deviation is wrong.',
172 goog.math.standardDeviation.apply(null, fakeExecutionTime),
173 results['standardDeviation']);
174
175 assertEquals('Count must be as expected.', 10, results['count']);
176 assertEquals('Maximum is wrong.', 130, results['maximum']);
177 assertEquals('Mimimum is wrong.', 90, results['minimum']);
178 assertEquals(
179 'Total must be a nonnegative number.',
180 goog.math.sum.apply(null, fakeExecutionTime), results['total']);
181
182 assertEquals(
183 'Set up count must be as expected.', useSetUp ? 10 : 0, setUpCount);
184 assertEquals(
185 'Tear down count must be as expected.', useTearDown ? 10 : 0,
186 tearDownCount);
187
188 return true;
189 }
190
191 function testTimeout() {
192 var count = 0;
193 var testFunction = function() {
194 mockClock.tick(100);
195 ++count;
196 };
197
198 timer.setNumSamples(200);
199 timer.setTimeoutInterval(2500);
200 var results = timer.run(testFunction);
201
202 assertNotNull('Results must be available', results);
203 assertEquals('Count is wrong', count, results['count']);
204 assertTrue(
205 'Count must less than expected',
206 results['count'] < timer.getNumSamples());
207 }
208
209 function testCreateResults() {
210 var samples = [53, 0, 103];
211 var expectedResults = {
212 'average': 52,
213 'count': 3,
214 'median': 53,
215 'maximum': 103,
216 'minimum': 0,
217 'standardDeviation': goog.math.standardDeviation.apply(null, samples),
218 'total': 156
219 };
220 assertObjectEquals(
221 expectedResults, goog.testing.PerformanceTimer.createResults(samples));
222 }