| 1 |
// Copyright 2010 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.benchmark'); |
| 16 |
goog.setTestOnly('goog.testing.benchmark'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.testing.PerformanceTable'); |
| 21 |
goog.require('goog.testing.PerformanceTimer'); |
| 22 |
goog.require('goog.testing.TestCase'); |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Run the benchmarks. |
| 27 |
* @private |
| 28 |
*/ |
| 29 |
goog.testing.benchmark.run_ = function() { |
| 30 |
// Parse the 'times' query parameter if it's set. |
| 31 |
var times = 200; |
| 32 |
var search = window.location.search; |
| 33 |
var timesMatch = search.match(/(?:\?|&)times=([^?&]+)/i); |
| 34 |
if (timesMatch) { |
| 35 |
times = Number(timesMatch[1]); |
| 36 |
} |
| 37 |
|
| 38 |
var prefix = 'benchmark'; |
| 39 |
|
| 40 |
// First, get the functions. |
| 41 |
var testSources = goog.testing.TestCase.getGlobals(); |
| 42 |
|
| 43 |
var benchmarks = {}; |
| 44 |
var names = []; |
| 45 |
|
| 46 |
for (var i = 0; i < testSources.length; i++) { |
| 47 |
var testSource = testSources[i]; |
| 48 |
for (var name in testSource) { |
| 49 |
if ((new RegExp('^' + prefix)).test(name)) { |
| 50 |
var ref; |
| 51 |
try { |
| 52 |
ref = testSource[name]; |
| 53 |
} catch (ex) { |
| 54 |
// NOTE(brenneman): When running tests from a file:// URL on Firefox |
| 55 |
// 3.5 for Windows, any reference to window.sessionStorage raises |
| 56 |
// an "Operation is not supported" exception. Ignore any exceptions |
| 57 |
// raised by simply accessing global properties. |
| 58 |
ref = undefined; |
| 59 |
} |
| 60 |
|
| 61 |
if (goog.isFunction(ref)) { |
| 62 |
benchmarks[name] = ref; |
| 63 |
names.push(name); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
document.body.appendChild( |
| 70 |
goog.dom.createTextNode( |
| 71 |
'Running ' + names.length + ' benchmarks ' + times + ' times each.')); |
| 72 |
document.body.appendChild(goog.dom.createElement(goog.dom.TagName.BR)); |
| 73 |
|
| 74 |
names.sort(); |
| 75 |
|
| 76 |
// Build a table and timer. |
| 77 |
var performanceTimer = new goog.testing.PerformanceTimer(times); |
| 78 |
performanceTimer.setDiscardOutliers(true); |
| 79 |
|
| 80 |
var performanceTable = |
| 81 |
new goog.testing.PerformanceTable(document.body, performanceTimer, 2); |
| 82 |
|
| 83 |
// Next, run the benchmarks. |
| 84 |
for (var i = 0; i < names.length; i++) { |
| 85 |
performanceTable.run(benchmarks[names[i]], names[i]); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Onload handler that runs the benchmarks. |
| 92 |
* @param {Event} e The event object. |
| 93 |
*/ |
| 94 |
window.onload = function(e) { |
| 95 |
goog.testing.benchmark.run_(); |
| 96 |
}; |