map_perf.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2012 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. * @fileoverview Performance test for goog.structs.Map and
  16. * goog.labs.structs.Map. To run this test fairly, you would have to
  17. * compile this via JsCompiler (with --export_test_functions), and
  18. * pull the compiled JS into an empty HTML file.
  19. * @author chrishenry@google.com (Chris Henry)
  20. */
  21. goog.provide('goog.labs.structs.MapPerf');
  22. goog.setTestOnly('goog.labs.structs.MapPerf');
  23. goog.require('goog.asserts');
  24. goog.require('goog.dom');
  25. goog.require('goog.dom.TagName');
  26. goog.require('goog.labs.structs.Map');
  27. goog.require('goog.structs.Map');
  28. goog.require('goog.testing.PerformanceTable');
  29. goog.require('goog.testing.jsunit');
  30. goog.scope(function() {
  31. var MapPerf = goog.labs.structs.MapPerf;
  32. /**
  33. * @typedef {goog.labs.structs.Map|goog.structs.Map}
  34. */
  35. MapPerf.MapType;
  36. /**
  37. * @type {goog.testing.PerformanceTable}
  38. */
  39. MapPerf.perfTable;
  40. /**
  41. * A key list. This maps loop index to key name to be used during
  42. * benchmark. This ensure that we do not need to pay the cost of
  43. * string concatenation/GC whenever we derive a key from loop index.
  44. *
  45. * This is filled once in setUpPage and then remain unchanged for the
  46. * rest of the test case.
  47. *
  48. * @type {!Array<string>}
  49. */
  50. MapPerf.keyList = [];
  51. /**
  52. * Maxium number of keys in keyList (and, by extension, the map under
  53. * test).
  54. * @type {number}
  55. */
  56. MapPerf.MAX_NUM_KEY = 10000;
  57. /**
  58. * Fills the given map with generated key-value pair.
  59. * @param {MapPerf.MapType} map The map to fill.
  60. * @param {number} numKeys The number of key-value pair to fill.
  61. */
  62. MapPerf.fillMap = function(map, numKeys) {
  63. goog.asserts.assert(numKeys <= MapPerf.MAX_NUM_KEY);
  64. for (var i = 0; i < numKeys; ++i) {
  65. map.set(MapPerf.keyList[i], i);
  66. }
  67. };
  68. /**
  69. * Primes the given map with deletion of keys.
  70. * @param {MapPerf.MapType} map The map to prime.
  71. * @return {MapPerf.MapType} The primed map (for chaining).
  72. */
  73. MapPerf.primeMapWithDeletion = function(map) {
  74. for (var i = 0; i < 1000; ++i) {
  75. map.set(MapPerf.keyList[i], i);
  76. }
  77. for (var i = 0; i < 1000; ++i) {
  78. map.remove(MapPerf.keyList[i]);
  79. }
  80. return map;
  81. };
  82. /**
  83. * Runs performance test for Map#get with the given map.
  84. * @param {MapPerf.MapType} map The map to stress.
  85. * @param {string} message Message to be put in performance table.
  86. */
  87. MapPerf.runPerformanceTestForMapGet = function(map, message) {
  88. MapPerf.fillMap(map, 10000);
  89. MapPerf.perfTable.run(function() {
  90. // Creates local alias for map and keyList.
  91. var localMap = map;
  92. var localKeyList = MapPerf.keyList;
  93. for (var i = 0; i < 500; ++i) {
  94. var sum = 0;
  95. for (var j = 0; j < 10000; ++j) {
  96. sum += localMap.get(localKeyList[j]);
  97. }
  98. }
  99. }, message);
  100. };
  101. /**
  102. * Runs performance test for Map#set with the given map.
  103. * @param {MapPerf.MapType} map The map to stress.
  104. * @param {string} message Message to be put in performance table.
  105. */
  106. MapPerf.runPerformanceTestForMapSet = function(map, message) {
  107. MapPerf.perfTable.run(function() {
  108. // Creates local alias for map and keyList.
  109. var localMap = map;
  110. var localKeyList = MapPerf.keyList;
  111. for (var i = 0; i < 500; ++i) {
  112. for (var j = 0; j < 10000; ++j) {
  113. localMap.set(localKeyList[i], i);
  114. }
  115. }
  116. }, message);
  117. };
  118. goog.global['setUpPage'] = function() {
  119. var content = goog.dom.createDom(goog.dom.TagName.DIV);
  120. goog.dom.insertChildAt(document.body, content, 0);
  121. goog.dom.append(
  122. content,
  123. goog.dom.createDom(
  124. goog.dom.TagName.H1, null, 'Closure Performance Tests - Map'),
  125. goog.dom.createDom(
  126. goog.dom.TagName.P, null,
  127. goog.dom.createDom(goog.dom.TagName.STRONG, null, 'User-agent: '),
  128. goog.dom.createDom(
  129. goog.dom.TagName.SPAN, {'id': 'ua'}, navigator.userAgent)),
  130. goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'perf-table'}),
  131. goog.dom.createDom(goog.dom.TagName.HR));
  132. MapPerf.perfTable =
  133. new goog.testing.PerformanceTable(goog.dom.getElement('perf-table'));
  134. // Fills keyList.
  135. for (var i = 0; i < MapPerf.MAX_NUM_KEY; ++i) {
  136. MapPerf.keyList.push('k' + i);
  137. }
  138. };
  139. goog.global['testGetFromLabsMap'] = function() {
  140. MapPerf.runPerformanceTestForMapGet(
  141. new goog.labs.structs.Map(), '#get: no previous deletion (Labs)');
  142. };
  143. goog.global['testGetFromOriginalMap'] = function() {
  144. MapPerf.runPerformanceTestForMapGet(
  145. new goog.structs.Map(), '#get: no previous deletion (Original)');
  146. };
  147. goog.global['testGetWithPreviousDeletionFromLabsMap'] = function() {
  148. MapPerf.runPerformanceTestForMapGet(
  149. MapPerf.primeMapWithDeletion(new goog.labs.structs.Map()),
  150. '#get: with previous deletion (Labs)');
  151. };
  152. goog.global['testGetWithPreviousDeletionFromOriginalMap'] = function() {
  153. MapPerf.runPerformanceTestForMapGet(
  154. MapPerf.primeMapWithDeletion(new goog.structs.Map()),
  155. '#get: with previous deletion (Original)');
  156. };
  157. goog.global['testSetFromLabsMap'] = function() {
  158. MapPerf.runPerformanceTestForMapSet(
  159. new goog.labs.structs.Map(), '#set: no previous deletion (Labs)');
  160. };
  161. goog.global['testSetFromOriginalMap'] = function() {
  162. MapPerf.runPerformanceTestForMapSet(
  163. new goog.structs.Map(), '#set: no previous deletion (Original)');
  164. };
  165. }); // goog.scope