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