| 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 |
/** |
| 16 |
* @fileoverview JSON performance tests. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.jsonPerf'); |
| 20 |
|
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.json'); |
| 23 |
goog.require('goog.math'); |
| 24 |
goog.require('goog.string'); |
| 25 |
goog.require('goog.testing.PerformanceTable'); |
| 26 |
goog.require('goog.testing.PropertyReplacer'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
|
| 29 |
goog.setTestOnly('goog.jsonPerf'); |
| 30 |
|
| 31 |
var table = new goog.testing.PerformanceTable(goog.dom.getElement('perfTable')); |
| 32 |
|
| 33 |
var stubs = new goog.testing.PropertyReplacer(); |
| 34 |
|
| 35 |
function tearDown() { |
| 36 |
stubs.reset(); |
| 37 |
} |
| 38 |
|
| 39 |
function testSerialize() { |
| 40 |
var obj = populateObject({}, 50, 4); |
| 41 |
|
| 42 |
table.run(function() { |
| 43 |
var s = JSON.stringify(obj); |
| 44 |
}, 'Stringify using JSON.stringify'); |
| 45 |
|
| 46 |
table.run(function() { |
| 47 |
var s = goog.json.serialize(obj); |
| 48 |
}, 'Stringify using goog.json.serialize'); |
| 49 |
} |
| 50 |
|
| 51 |
function testParse() { |
| 52 |
var obj = populateObject({}, 50, 4); |
| 53 |
var s = JSON.stringify(obj); |
| 54 |
|
| 55 |
table.run(function() { var o = JSON.parse(s); }, 'Parse using JSON.parse'); |
| 56 |
|
| 57 |
table.run(function() { |
| 58 |
var o = goog.json.parse(s); |
| 59 |
}, 'Parse using goog.json.parse'); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* @param {!Object} obj The object to add properties to. |
| 65 |
* @param {number} numProperties The number of properties to add. |
| 66 |
* @param {number} depth The depth at which to recursively add properties. |
| 67 |
* @return {!Object} The object given in obj (for convenience). |
| 68 |
*/ |
| 69 |
function populateObject(obj, numProperties, depth) { |
| 70 |
if (depth == 0) { |
| 71 |
return randomLiteral(); |
| 72 |
} |
| 73 |
|
| 74 |
// Make an object with a mix of strings, numbers, arrays, objects, booleans |
| 75 |
// nulls as children. |
| 76 |
for (var i = 0; i < numProperties; i++) { |
| 77 |
var bucket = goog.math.randomInt(3); |
| 78 |
switch (bucket) { |
| 79 |
case 0: |
| 80 |
obj[i] = randomLiteral(); |
| 81 |
break; |
| 82 |
case 1: |
| 83 |
obj[i] = populateObject({}, numProperties, depth - 1); |
| 84 |
break; |
| 85 |
case 2: |
| 86 |
obj[i] = populateObject([], numProperties, depth - 1); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
return obj; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
function randomLiteral() { |
| 95 |
var bucket = goog.math.randomInt(3); |
| 96 |
switch (bucket) { |
| 97 |
case 0: |
| 98 |
return goog.string.getRandomString(); |
| 99 |
case 1: |
| 100 |
return Math.random(); |
| 101 |
case 2: |
| 102 |
return Math.random() >= .5; |
| 103 |
} |
| 104 |
return null; |
| 105 |
} |