performancetable.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2009 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. * @fileoverview A table for showing the results of performance testing.
  16. *
  17. * {@see goog.testing.benchmark} for an easy way to use this functionality.
  18. *
  19. * @author attila@google.com (Attila Bodis)
  20. * @author nicksantos@google.com (Nick Santos)
  21. */
  22. goog.setTestOnly('goog.testing.PerformanceTable');
  23. goog.provide('goog.testing.PerformanceTable');
  24. goog.require('goog.asserts');
  25. goog.require('goog.dom');
  26. goog.require('goog.dom.TagName');
  27. goog.require('goog.testing.PerformanceTimer');
  28. /**
  29. * A UI widget that runs performance tests and displays the results.
  30. * @param {Element} root The element where the table should be attached.
  31. * @param {goog.testing.PerformanceTimer=} opt_timer A timer to use for
  32. * executing functions and profiling them.
  33. * @param {number=} opt_precision Number of digits of precision to include in
  34. * results. Defaults to 0.
  35. * @param {number=} opt_numSamples The number of samples to take. Defaults to 5.
  36. * @constructor
  37. * @final
  38. */
  39. goog.testing.PerformanceTable = function(
  40. root, opt_timer, opt_precision, opt_numSamples) {
  41. /**
  42. * Where the table should be attached.
  43. * @private {Element}
  44. */
  45. this.root_ = root;
  46. /**
  47. * Number of digits of precision to include in results.
  48. * Defaults to 0.
  49. * @private {number}
  50. */
  51. this.precision_ = opt_precision || 0;
  52. var timer = opt_timer;
  53. if (!timer) {
  54. timer = new goog.testing.PerformanceTimer();
  55. timer.setNumSamples(opt_numSamples || 5);
  56. timer.setDiscardOutliers(true);
  57. }
  58. /**
  59. * A timer for running the tests.
  60. * @private {goog.testing.PerformanceTimer}
  61. */
  62. this.timer_ = timer;
  63. this.initRoot_();
  64. };
  65. /**
  66. * @return {goog.testing.PerformanceTimer} The timer being used.
  67. */
  68. goog.testing.PerformanceTable.prototype.getTimer = function() {
  69. return this.timer_;
  70. };
  71. /**
  72. * Render the initial table.
  73. * @private
  74. */
  75. goog.testing.PerformanceTable.prototype.initRoot_ = function() {
  76. this.root_.innerHTML = '<table class="test-results" cellspacing="1">' +
  77. ' <thead>' +
  78. ' <tr>' +
  79. ' <th rowspan="2">Test Description</th>' +
  80. ' <th rowspan="2">Runs</th>' +
  81. ' <th colspan="4">Results (ms)</th>' +
  82. ' </tr>' +
  83. ' <tr>' +
  84. ' <th>Average</th>' +
  85. ' <th>Std Dev</th>' +
  86. ' <th>Minimum</th>' +
  87. ' <th>Maximum</th>' +
  88. ' </tr>' +
  89. ' </thead>' +
  90. ' <tbody>' +
  91. ' </tbody>' +
  92. '</table>';
  93. };
  94. /**
  95. * @return {Element} The body of the table.
  96. * @private
  97. */
  98. goog.testing.PerformanceTable.prototype.getTableBody_ = function() {
  99. return goog.dom.getElementsByTagName(
  100. goog.dom.TagName.TBODY, goog.asserts.assert(this.root_))[0];
  101. };
  102. /**
  103. * Round to the specified precision.
  104. * @param {number} num The number to round.
  105. * @return {string} The rounded number, as a string.
  106. * @private
  107. */
  108. goog.testing.PerformanceTable.prototype.round_ = function(num) {
  109. var factor = Math.pow(10, this.precision_);
  110. return String(Math.round(num * factor) / factor);
  111. };
  112. /**
  113. * Run the given function with the performance timer, and show the results.
  114. * @param {Function} fn The function to run.
  115. * @param {string=} opt_desc A description to associate with this run.
  116. */
  117. goog.testing.PerformanceTable.prototype.run = function(fn, opt_desc) {
  118. this.runTask(
  119. new goog.testing.PerformanceTimer.Task(/** @type {function()} */ (fn)),
  120. opt_desc);
  121. };
  122. /**
  123. * Run the given task with the performance timer, and show the results.
  124. * @param {goog.testing.PerformanceTimer.Task} task The performance timer task
  125. * to run.
  126. * @param {string=} opt_desc A description to associate with this run.
  127. */
  128. goog.testing.PerformanceTable.prototype.runTask = function(task, opt_desc) {
  129. var results = this.timer_.runTask(task);
  130. this.recordResults(results, opt_desc);
  131. };
  132. /**
  133. * Record a performance timer results object to the performance table. See
  134. * {@code goog.testing.PerformanceTimer} for details of the format of this
  135. * object.
  136. * @param {Object} results The performance timer results object.
  137. * @param {string=} opt_desc A description to associate with these results.
  138. */
  139. goog.testing.PerformanceTable.prototype.recordResults = function(
  140. results, opt_desc) {
  141. var average = results['average'];
  142. var standardDeviation = results['standardDeviation'];
  143. var isSuspicious = average < 0 || standardDeviation > average * .5;
  144. var resultsRow = goog.dom.createDom(
  145. goog.dom.TagName.TR, null, goog.dom.createDom(
  146. goog.dom.TagName.TD, 'test-description',
  147. opt_desc || 'No description'),
  148. goog.dom.createDom(
  149. goog.dom.TagName.TD, 'test-count', String(results['count'])),
  150. goog.dom.createDom(
  151. goog.dom.TagName.TD, 'test-average', this.round_(average)),
  152. goog.dom.createDom(
  153. goog.dom.TagName.TD, 'test-standard-deviation',
  154. this.round_(standardDeviation)),
  155. goog.dom.createDom(
  156. goog.dom.TagName.TD, 'test-minimum', String(results['minimum'])),
  157. goog.dom.createDom(
  158. goog.dom.TagName.TD, 'test-maximum', String(results['maximum'])));
  159. if (isSuspicious) {
  160. resultsRow.className = 'test-suspicious';
  161. }
  162. this.getTableBody_().appendChild(resultsRow);
  163. };
  164. /**
  165. * Report an error in the table.
  166. * @param {*} reason The reason for the error.
  167. */
  168. goog.testing.PerformanceTable.prototype.reportError = function(reason) {
  169. this.getTableBody_().appendChild(
  170. goog.dom.createDom(
  171. goog.dom.TagName.TR, null,
  172. goog.dom.createDom(
  173. goog.dom.TagName.TD, {'class': 'test-error', 'colSpan': 5},
  174. String(reason))));
  175. };