benchmark.js 2.8 KB

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