control_perf.html 5.8 KB

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