performancetimer_test.js 5.6 KB

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