Path: blob/trunk/third_party/closure/goog/labs/structs/map_perf.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Performance test for goog.structs.Map and16* goog.labs.structs.Map. To run this test fairly, you would have to17* compile this via JsCompiler (with --export_test_functions), and18* pull the compiled JS into an empty HTML file.19* @author [email protected] (Chris Henry)20*/2122goog.provide('goog.labs.structs.MapPerf');23goog.setTestOnly('goog.labs.structs.MapPerf');2425goog.require('goog.asserts');26goog.require('goog.dom');27goog.require('goog.dom.TagName');28goog.require('goog.labs.structs.Map');29goog.require('goog.structs.Map');30goog.require('goog.testing.PerformanceTable');31goog.require('goog.testing.jsunit');3233goog.scope(function() {34var MapPerf = goog.labs.structs.MapPerf;353637/**38* @typedef {goog.labs.structs.Map|goog.structs.Map}39*/40MapPerf.MapType;414243/**44* @type {goog.testing.PerformanceTable}45*/46MapPerf.perfTable;474849/**50* A key list. This maps loop index to key name to be used during51* benchmark. This ensure that we do not need to pay the cost of52* string concatenation/GC whenever we derive a key from loop index.53*54* This is filled once in setUpPage and then remain unchanged for the55* rest of the test case.56*57* @type {!Array<string>}58*/59MapPerf.keyList = [];606162/**63* Maxium number of keys in keyList (and, by extension, the map under64* test).65* @type {number}66*/67MapPerf.MAX_NUM_KEY = 10000;686970/**71* Fills the given map with generated key-value pair.72* @param {MapPerf.MapType} map The map to fill.73* @param {number} numKeys The number of key-value pair to fill.74*/75MapPerf.fillMap = function(map, numKeys) {76goog.asserts.assert(numKeys <= MapPerf.MAX_NUM_KEY);7778for (var i = 0; i < numKeys; ++i) {79map.set(MapPerf.keyList[i], i);80}81};828384/**85* Primes the given map with deletion of keys.86* @param {MapPerf.MapType} map The map to prime.87* @return {MapPerf.MapType} The primed map (for chaining).88*/89MapPerf.primeMapWithDeletion = function(map) {90for (var i = 0; i < 1000; ++i) {91map.set(MapPerf.keyList[i], i);92}93for (var i = 0; i < 1000; ++i) {94map.remove(MapPerf.keyList[i]);95}96return map;97};9899100/**101* Runs performance test for Map#get with the given map.102* @param {MapPerf.MapType} map The map to stress.103* @param {string} message Message to be put in performance table.104*/105MapPerf.runPerformanceTestForMapGet = function(map, message) {106MapPerf.fillMap(map, 10000);107108MapPerf.perfTable.run(function() {109// Creates local alias for map and keyList.110var localMap = map;111var localKeyList = MapPerf.keyList;112113for (var i = 0; i < 500; ++i) {114var sum = 0;115for (var j = 0; j < 10000; ++j) {116sum += localMap.get(localKeyList[j]);117}118}119}, message);120};121122123/**124* Runs performance test for Map#set with the given map.125* @param {MapPerf.MapType} map The map to stress.126* @param {string} message Message to be put in performance table.127*/128MapPerf.runPerformanceTestForMapSet = function(map, message) {129MapPerf.perfTable.run(function() {130// Creates local alias for map and keyList.131var localMap = map;132var localKeyList = MapPerf.keyList;133134for (var i = 0; i < 500; ++i) {135for (var j = 0; j < 10000; ++j) {136localMap.set(localKeyList[i], i);137}138}139}, message);140};141142143goog.global['setUpPage'] = function() {144var content = goog.dom.createDom(goog.dom.TagName.DIV);145goog.dom.insertChildAt(document.body, content, 0);146goog.dom.append(147content,148goog.dom.createDom(149goog.dom.TagName.H1, null, 'Closure Performance Tests - Map'),150goog.dom.createDom(151goog.dom.TagName.P, null,152goog.dom.createDom(goog.dom.TagName.STRONG, null, 'User-agent: '),153goog.dom.createDom(154goog.dom.TagName.SPAN, {'id': 'ua'}, navigator.userAgent)),155goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'perf-table'}),156goog.dom.createDom(goog.dom.TagName.HR));157158MapPerf.perfTable =159new goog.testing.PerformanceTable(goog.dom.getElement('perf-table'));160161// Fills keyList.162for (var i = 0; i < MapPerf.MAX_NUM_KEY; ++i) {163MapPerf.keyList.push('k' + i);164}165};166167168goog.global['testGetFromLabsMap'] = function() {169MapPerf.runPerformanceTestForMapGet(170new goog.labs.structs.Map(), '#get: no previous deletion (Labs)');171};172173174goog.global['testGetFromOriginalMap'] = function() {175MapPerf.runPerformanceTestForMapGet(176new goog.structs.Map(), '#get: no previous deletion (Original)');177};178179180goog.global['testGetWithPreviousDeletionFromLabsMap'] = function() {181MapPerf.runPerformanceTestForMapGet(182MapPerf.primeMapWithDeletion(new goog.labs.structs.Map()),183'#get: with previous deletion (Labs)');184};185186187goog.global['testGetWithPreviousDeletionFromOriginalMap'] = function() {188MapPerf.runPerformanceTestForMapGet(189MapPerf.primeMapWithDeletion(new goog.structs.Map()),190'#get: with previous deletion (Original)');191};192193194goog.global['testSetFromLabsMap'] = function() {195MapPerf.runPerformanceTestForMapSet(196new goog.labs.structs.Map(), '#set: no previous deletion (Labs)');197};198199200goog.global['testSetFromOriginalMap'] = function() {201MapPerf.runPerformanceTestForMapSet(202new goog.structs.Map(), '#set: no previous deletion (Original)');203};204205}); // goog.scope206207208