| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<!-- |
| 10 |
Author: [email protected] (Attila Bodis) |
| 11 |
--> |
| 12 |
<head> |
| 13 |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 14 |
<title>Closure Performance Tests - goog.ui.Control</title> |
| 15 |
<link rel="stylesheet" type="text/css" href="../testing/performancetable.css" /> |
| 16 |
<script src="../base.js"></script> |
| 17 |
<script> |
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.testing.PerformanceTable'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.ui.Control'); |
| 22 |
goog.require('goog.dom.TagName'); |
| 23 |
</script> |
| 24 |
</head> |
| 25 |
<body> |
| 26 |
<h1>goog.ui.Control Performance Tests</h1> |
| 27 |
<p> |
| 28 |
<b>User-agent:</b> <script>document.write(navigator.userAgent);</script> |
| 29 |
</p> |
| 30 |
<div id="perfTable"></div> |
| 31 |
<hr> |
| 32 |
<div id="renderSandbox"></div> |
| 33 |
<div id="decorateSandbox"></div> |
| 34 |
<script> |
| 35 |
// The sandboxen. |
| 36 |
var renderSandbox = goog.dom.getElement('renderSandbox'); |
| 37 |
var decorateSandbox = goog.dom.getElement('decorateSandbox'); |
| 38 |
|
| 39 |
// Arrays of rendered/decorated controls (so we can dispose of them). |
| 40 |
var renderedControls; |
| 41 |
var decoratedControls; |
| 42 |
|
| 43 |
// 0-based index of the control currently being rendered/decorated. |
| 44 |
var renderIndex; |
| 45 |
var decorateIndex; |
| 46 |
|
| 47 |
// Element currently being decorated. |
| 48 |
var elementToDecorate; |
| 49 |
|
| 50 |
// Number of controls to create/decorate per test run. |
| 51 |
var SAMPLES_PER_RUN = 100; |
| 52 |
|
| 53 |
// The performance table. |
| 54 |
var table; |
| 55 |
|
| 56 |
// Sets up a render test. |
| 57 |
function setUpRenderTest() { |
| 58 |
renderedControls = []; |
| 59 |
renderIndex = 0; |
| 60 |
} |
| 61 |
|
| 62 |
// Cleans up after a render test. |
| 63 |
function cleanUpAfterRenderTest() { |
| 64 |
for (var i = 0, count = renderedControls.length; i < count; i++) { |
| 65 |
renderedControls[i].dispose(); |
| 66 |
} |
| 67 |
renderedControls = null; |
| 68 |
goog.dom.removeChildren(renderSandbox); |
| 69 |
} |
| 70 |
|
| 71 |
// Sets up a decorate test. |
| 72 |
function setUpDecorateTest(opt_count) { |
| 73 |
var count = opt_count || 1000; |
| 74 |
for (var i = 0; i < count; i++) { |
| 75 |
decorateSandbox.appendChild(goog.dom.createDom(goog.dom.TagName.DIV, 'goog-control', |
| 76 |
'W00t!')); |
| 77 |
} |
| 78 |
elementToDecorate = decorateSandbox.firstChild; |
| 79 |
decoratedControls = []; |
| 80 |
decorateIndex = 0; |
| 81 |
} |
| 82 |
|
| 83 |
// Cleans up after a decorate test. |
| 84 |
function cleanUpAfterDecorateTest() { |
| 85 |
for (var i = 0, count = decoratedControls.length; i < count; i++) { |
| 86 |
decoratedControls[i].dispose(); |
| 87 |
} |
| 88 |
decoratedControls = null; |
| 89 |
goog.dom.removeChildren(decorateSandbox); |
| 90 |
} |
| 91 |
|
| 92 |
// Renders the given number of controls. Since children are appended to |
| 93 |
// the same parent element in each performance test run, we keep track of |
| 94 |
// the current index via the global renderIndex variable. |
| 95 |
function renderControls(count, autoDetectBiDi) { |
| 96 |
for (var i = 0; i < count; i++) { |
| 97 |
var control = new goog.ui.Control('W00t!'); |
| 98 |
if (!autoDetectBiDi) { |
| 99 |
control.setRightToLeft(false); |
| 100 |
} |
| 101 |
control.render(renderSandbox); |
| 102 |
renderedControls[renderIndex++] = control; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Decorates "count" controls. The decorate sandbox contains enough child |
| 107 |
// elements for the whole test, but we only decorate up to "count" elements |
| 108 |
// per test run, so we need to keep track of where we are via the global |
| 109 |
// decorateIndex and elementToDecorate variables. |
| 110 |
function decorateControls(count, autoDetectBiDi) { |
| 111 |
for (var i = 0; i < count; i++) { |
| 112 |
var next = elementToDecorate.nextSibling; |
| 113 |
var control = new goog.ui.Control(); |
| 114 |
if (!autoDetectBiDi) { |
| 115 |
control.setRightToLeft(false); |
| 116 |
} |
| 117 |
control.decorate(elementToDecorate); |
| 118 |
decoratedControls[decorateIndex++] = control; |
| 119 |
elementToDecorate = next; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
function setUpPage() { |
| 124 |
table = new goog.testing.PerformanceTable( |
| 125 |
goog.dom.getElement('perfTable')); |
| 126 |
} |
| 127 |
|
| 128 |
function testRender() { |
| 129 |
setUpRenderTest(); |
| 130 |
table.run(goog.partial(renderControls, SAMPLES_PER_RUN, true), |
| 131 |
'Render ' + SAMPLES_PER_RUN + ' controls (default)'); |
| 132 |
cleanUpAfterRenderTest(); |
| 133 |
assertEquals('The expected number of controls must have been rendered', |
| 134 |
SAMPLES_PER_RUN * table.getTimer().getNumSamples(), renderIndex); |
| 135 |
} |
| 136 |
|
| 137 |
function testDecorate() { |
| 138 |
setUpDecorateTest(SAMPLES_PER_RUN * table.getTimer().getNumSamples()); |
| 139 |
table.run(goog.partial(decorateControls, SAMPLES_PER_RUN, true), |
| 140 |
'Decorate ' + SAMPLES_PER_RUN + ' controls (default)'); |
| 141 |
cleanUpAfterDecorateTest(); |
| 142 |
assertEquals('The expected number of controls must have been decorated', |
| 143 |
SAMPLES_PER_RUN * table.getTimer().getNumSamples(), decorateIndex); |
| 144 |
assertNull('All controls must have been decorated', elementToDecorate); |
| 145 |
} |
| 146 |
|
| 147 |
function testRenderNoBiDiAutoDetect() { |
| 148 |
setUpRenderTest(); |
| 149 |
table.run(goog.partial(renderControls, SAMPLES_PER_RUN, false), |
| 150 |
'Render ' + SAMPLES_PER_RUN + ' controls (no BiDi auto-detect)'); |
| 151 |
cleanUpAfterRenderTest(); |
| 152 |
assertEquals('The expected number of controls must have been rendered', |
| 153 |
SAMPLES_PER_RUN * table.getTimer().getNumSamples(), renderIndex); |
| 154 |
} |
| 155 |
|
| 156 |
function testDecorateNoBiDiAutoDetect() { |
| 157 |
setUpDecorateTest(SAMPLES_PER_RUN * table.getTimer().getNumSamples()); |
| 158 |
table.run(goog.partial(decorateControls, SAMPLES_PER_RUN, false), |
| 159 |
'Decorate ' + SAMPLES_PER_RUN + ' controls (no BiDi auto-detect)'); |
| 160 |
cleanUpAfterDecorateTest(); |
| 161 |
assertEquals('The expected number of controls must have been decorated', |
| 162 |
SAMPLES_PER_RUN * table.getTimer().getNumSamples(), decorateIndex); |
| 163 |
assertNull('All controls must have been decorated', elementToDecorate); |
| 164 |
} |
| 165 |
</script> |
| 166 |
</body> |
| 167 |
</html> |