From 0a66361d7c43a80f37e133a349e299685624874c Mon Sep 17 00:00:00 2001 From: peterwmwong Date: Wed, 15 Nov 2017 21:41:49 -0600 Subject: [PATCH] [js-perf-test] Add Collection constructor and double type microbenchmarks - Add {Map/Set/WeakMap/WeakSet}-Constructor microbenchmarks - Add {Map/Set}-Double microbenchmarks (testing heap number keys) Bug: v8:6604 Change-Id: Icadd5c81bfb59a58a2a65e119663d3f22637165d Reviewed-on: https://chromium-review.googlesource.com/773595 Reviewed-by: Jakob Gruber Commit-Queue: Jakob Gruber Cr-Commit-Position: refs/heads/master@{#49398} --- test/js-perf-test/Collections/common.js | 26 ++++++++ test/js-perf-test/Collections/map.js | 84 ++++++++++++++++++++++++ test/js-perf-test/Collections/set.js | 76 +++++++++++++++++++++ test/js-perf-test/Collections/weakmap.js | 9 +++ test/js-perf-test/Collections/weakset.js | 9 +++ test/js-perf-test/JSTests.json | 7 +- 6 files changed, 210 insertions(+), 1 deletion(-) diff --git a/test/js-perf-test/Collections/common.js b/test/js-perf-test/Collections/common.js index 471648531e..7ae835f2ec 100644 --- a/test/js-perf-test/Collections/common.js +++ b/test/js-perf-test/Collections/common.js @@ -5,18 +5,44 @@ var N = 10; var LargeN = 1e4; var keys; +var keyValuePairs; +function SetupKeyValuePairsFromKeys() { + keyValuePairs = keys.map((v) => [v, v]); +} function SetupSmiKeys(count = 2 * N) { keys = Array.from({ length : count }, (v, i) => i); } +function SetupSmiKeyValuePairs(count = 2 * N) { + SetupSmiKeys(count); + SetupKeyValuePairsFromKeys(); +} function SetupStringKeys(count = 2 * N) { keys = Array.from({ length : count }, (v, i) => 's' + i); } +function SetupStringKeyValuePairs(count = 2 * N) { + SetupStringKeys(count); + SetupKeyValuePairsFromKeys(); +} function SetupObjectKeys(count = 2 * N) { keys = Array.from({ length : count }, (v, i) => ({})); } + +function SetupObjectKeyValuePairs(count = 2 * N) { + SetupObjectKeys(count); + SetupKeyValuePairsFromKeys(); +} + +function SetupDoubleKeys(count = 2 * N) { + keys = Array.from({ length : count }, (v, i) => i + 0.234); +} + +function SetupDoubleKeyValuePairs(count = 2 * N) { + SetupDoubleKeys(count); + SetupKeyValuePairsFromKeys(); +} diff --git a/test/js-perf-test/Collections/map.js b/test/js-perf-test/Collections/map.js index 2e0147c2b8..39e97552b9 100644 --- a/test/js-perf-test/Collections/map.js +++ b/test/js-perf-test/Collections/map.js @@ -24,6 +24,13 @@ var MapObjectBenchmark = new BenchmarkSuite('Map-Object', [1000], [ new Benchmark('Delete', false, false, 0, MapDeleteObject, MapSetupObject, MapTearDown), ]); +var MapDoubleBenchmark = new BenchmarkSuite('Map-Double', [1000], [ + new Benchmark('Set', false, false, 0, MapSetDouble, MapSetupDoubleBase, MapTearDown), + new Benchmark('Has', false, false, 0, MapHasDouble, MapSetupDouble, MapTearDown), + new Benchmark('Get', false, false, 0, MapGetDouble, MapSetupDouble, MapTearDown), + new Benchmark('Delete', false, false, 0, MapDeleteDouble, MapSetupDouble, MapTearDown), +]); + var MapObjectLargeBenchmark = new BenchmarkSuite('Map-Object-Set-Get-Large', [1e7], [ new Benchmark('Set-Get', false, false, 0, MapSetGetObjectLarge, MapSetupObjectBaseLarge, MapTearDown), @@ -37,6 +44,13 @@ var MapIterationBenchmark = new BenchmarkSuite('Map-Iterator', [1000], [ new Benchmark('Iterator', false, false, 0, MapIterator, MapSetupSmi, MapTearDown), ]); +var MapConstructorBenchmark = new BenchmarkSuite('Map-Constructor', [1000], [ + new Benchmark('Smi', false, false, 0, MapConstructorSmi, SetupSmiKeyValuePairs, MapTearDown), + new Benchmark('String', false, false, 0, MapConstructorString, SetupStringKeyValuePairs, MapTearDown), + new Benchmark('Object', false, false, 0, MapConstructorObject, SetupObjectKeyValuePairs, MapTearDown), + new Benchmark('Double', false, false, 0, MapConstructorDouble, SetupDoubleKeyValuePairs, MapTearDown), +]); + var map; function MapSetupSmiBase() { @@ -74,10 +88,24 @@ function MapSetupObject() { MapSetObject(); } +function MapSetupDoubleBase() { + SetupDoubleKeys(); + map = new Map; +} + +function MapSetupDouble() { + MapSetupDoubleBase(); + MapSetDouble(); +} + function MapTearDown() { map = null; } +function MapConstructorSmi() { + map = new Map(keyValuePairs); +} + function MapSetSmi() { for (var i = 0; i < N; i++) { map.set(keys[i], i); @@ -119,6 +147,11 @@ function MapDeleteSmi() { } } + +function MapConstructorString() { + map = new Map(keyValuePairs); +} + function MapSetString() { for (var i = 0; i < N; i++) { map.set(keys[i], i); @@ -160,6 +193,11 @@ function MapDeleteString() { } } + +function MapConstructorObject() { + map = new Map(keyValuePairs); +} + function MapSetObject() { for (var i = 0; i < N; i++) { map.set(keys[i], i); @@ -216,6 +254,52 @@ function MapDeleteObject() { } } + +function MapConstructorDouble() { + map = new Map(keyValuePairs); +} + +function MapSetDouble() { + for (var i = 0; i < N; i++) { + map.set(keys[i], i); + } +} + +function MapHasDouble() { + for (var i = 0; i < N; i++) { + if (!map.has(keys[i])) { + throw new Error(); + } + } + for (var i = N; i < 2 * N; i++) { + if (map.has(keys[i])) { + throw new Error(); + } + } +} + +function MapGetDouble() { + for (var i = 0; i < N; i++) { + if (map.get(keys[i]) !== i) { + throw new Error(); + } + } + for (var i = N; i < 2 * N; i++) { + if (map.get(keys[i]) !== undefined) { + throw new Error(); + } + } +} + +function MapDeleteDouble() { + // This is run more than once per setup so we will end up deleting items + // more than once. Therefore, we do not the return value of delete. + for (var i = 0; i < N; i++) { + map.delete(keys[i]); + } +} + + function MapForEach() { map.forEach(function(v, k) { if (v !== k) { diff --git a/test/js-perf-test/Collections/set.js b/test/js-perf-test/Collections/set.js index dd4092530f..bcf98405ea 100644 --- a/test/js-perf-test/Collections/set.js +++ b/test/js-perf-test/Collections/set.js @@ -24,6 +24,13 @@ var SetObjectBenchmark = new BenchmarkSuite('Set-Object', [1000], [ ]); +var SetDoubleBenchmark = new BenchmarkSuite('Set-Double', [1000], [ + new Benchmark('Set', false, false, 0, SetAddDouble, SetSetupDoubleBase, SetTearDown), + new Benchmark('Has', false, false, 0, SetHasDouble, SetSetupDouble, SetTearDown), + new Benchmark('Delete', false, false, 0, SetDeleteDouble, SetSetupDouble, SetTearDown), +]); + + var SetIterationBenchmark = new BenchmarkSuite('Set-Iteration', [1000], [ new Benchmark('ForEach', false, false, 0, SetForEach, SetSetupSmi, SetTearDown), ]); @@ -34,6 +41,13 @@ var SetIterationBenchmark = new BenchmarkSuite('Set-Iterator', [1000], [ ]); +var SetConstructorBenchmark = new BenchmarkSuite('Set-Constructor', [1000], [ + new Benchmark('Smi', false, false, 0, SetConstructorSmi, SetupSmiKeys, SetTearDown), + new Benchmark('String', false, false, 0, SetConstructorString, SetupStringKeys, SetTearDown), + new Benchmark('Object', false, false, 0, SetConstructorObject, SetupObjectKeys, SetTearDown), + new Benchmark('Double', false, false, 0, SetConstructorDouble, SetupDoubleKeys, SetTearDown), +]); + var set; @@ -73,11 +87,28 @@ function SetSetupObject() { } +function SetSetupDoubleBase() { + SetupDoubleKeys(); + set = new Set; +} + + +function SetSetupDouble() { + SetSetupDoubleBase(); + SetAddDouble(); +} + + function SetTearDown() { set = null; } +function SetConstructorSmi() { + set = new Set(keys); +} + + function SetAddSmi() { for (var i = 0; i < N; i++) { set.add(keys[i], i); @@ -108,6 +139,11 @@ function SetDeleteSmi() { } +function SetConstructorString() { + set = new Set(keys); +} + + function SetAddString() { for (var i = 0; i < N; i++) { set.add(keys[i], i); @@ -138,6 +174,11 @@ function SetDeleteString() { } +function SetConstructorObject() { + set = new Set(keys); +} + + function SetAddObject() { for (var i = 0; i < N; i++) { set.add(keys[i], i); @@ -168,6 +209,41 @@ function SetDeleteObject() { } +function SetConstructorDouble() { + set = new Set(keys); +} + + +function SetAddDouble() { + for (var i = 0; i < N; i++) { + set.add(keys[i], i); + } +} + + +function SetHasDouble() { + for (var i = 0; i < N; i++) { + if (!set.has(keys[i])) { + throw new Error(); + } + } + for (var i = N; i < 2 * N; i++) { + if (set.has(keys[i])) { + throw new Error(); + } + } +} + + +function SetDeleteDouble() { + // This is run more than once per setup so we will end up deleting items + // more than once. Therefore, we do not the return value of delete. + for (var i = 0; i < N; i++) { + set.delete(keys[i]); + } +} + + function SetForEach() { set.forEach(function(v, k) { if (v !== k) { diff --git a/test/js-perf-test/Collections/weakmap.js b/test/js-perf-test/Collections/weakmap.js index 430b3d2b45..e13d1b36d7 100644 --- a/test/js-perf-test/Collections/weakmap.js +++ b/test/js-perf-test/Collections/weakmap.js @@ -19,6 +19,11 @@ var MapBenchmark = new BenchmarkSuite('WeakMapSetGet-Large', [1e7], [ WeakMapSetupBaseLarge, WeakMapTearDown), ]); +var MapBenchmark = new BenchmarkSuite('WeakMap-Constructor', [1000], [ + new Benchmark('Constructor', false, false, 0, WeakMapConstructor, SetupObjectKeyValuePairs, + WeakMapTearDown), +]); + var wm; @@ -46,6 +51,10 @@ function WeakMapTearDown() { } +function WeakMapConstructor() { + wm = new WeakMap(keyValuePairs); +} + function WeakMapSet() { for (var i = 0; i < N; i++) { wm.set(keys[i], i); diff --git a/test/js-perf-test/Collections/weakset.js b/test/js-perf-test/Collections/weakset.js index 2936477b35..1dca043c34 100644 --- a/test/js-perf-test/Collections/weakset.js +++ b/test/js-perf-test/Collections/weakset.js @@ -12,6 +12,10 @@ var SetBenchmark = new BenchmarkSuite('WeakSet', [1000], [ WeakSetTearDown), ]); +var SetBenchmark = new BenchmarkSuite('WeakSet-Constructor', [1000], [ + new Benchmark('Constructor', false, false, 0, WeakSetConstructor, SetupObjectKeys, + WeakSetTearDown), +]); var ws; @@ -33,6 +37,11 @@ function WeakSetTearDown() { } +function WeakSetConstructor() { + ws = new WeakSet(keys); +} + + function WeakSetAdd() { for (var i = 0; i < N; i++) { ws.add(keys[i]); diff --git a/test/js-perf-test/JSTests.json b/test/js-perf-test/JSTests.json index c194abd9de..6b0076f6fb 100644 --- a/test/js-perf-test/JSTests.json +++ b/test/js-perf-test/JSTests.json @@ -150,15 +150,20 @@ {"name": "Map-Smi"}, {"name": "Map-String"}, {"name": "Map-Object"}, + {"name": "Map-Object-Set-Get-Large"}, + {"name": "Map-Double"}, {"name": "Map-Iteration"}, {"name": "Map-Iterator"}, {"name": "Set-Smi"}, {"name": "Set-String"}, {"name": "Set-Object"}, + {"name": "Set-Double"}, {"name": "Set-Iteration"}, {"name": "Set-Iterator"}, {"name": "WeakMap"}, - {"name": "WeakSet"} + {"name": "WeakMap-Constructor"}, + {"name": "WeakSet"}, + {"name": "WeakSet-Constructor"} ] }, {