From cff862c0364a7bdafbbecdef98b7926230b34d89 Mon Sep 17 00:00:00 2001 From: Nico Hartmann Date: Tue, 22 Oct 2019 14:28:43 +0200 Subject: [PATCH] [js-perf-tests] Adds performance tests for BigInt subtraction BigInt performance benchmarks are restructured in JSTest1.json in such a way that it is easier to run meaningful subsets of BigInt test cases. Bug: v8:9213 Change-Id: Ibf94bfb0f14cf8afa890927d97f920659e8b28d0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1872390 Commit-Queue: Nico Hartmann Reviewed-by: Georg Neis Cr-Commit-Position: refs/heads/master@{#64468} --- test/js-perf-test/BigInt/add.js | 5 + test/js-perf-test/BigInt/as-uint-n.js | 2 + test/js-perf-test/BigInt/bigint-util.js | 6 + test/js-perf-test/BigInt/run.js | 9 +- test/js-perf-test/BigInt/subtract.js | 148 +++++++++++++++++++++++ test/js-perf-test/BigInt/test-config.js | 10 -- test/js-perf-test/BigInt/to-boolean.js | 15 ++- test/js-perf-test/JSTests1.json | 149 ++++++++++++++++-------- 8 files changed, 272 insertions(+), 72 deletions(-) create mode 100644 test/js-perf-test/BigInt/subtract.js delete mode 100644 test/js-perf-test/BigInt/test-config.js diff --git a/test/js-perf-test/BigInt/add.js b/test/js-perf-test/BigInt/add.js index f0397f2393..a18bf91270 100644 --- a/test/js-perf-test/BigInt/add.js +++ b/test/js-perf-test/BigInt/add.js @@ -56,6 +56,7 @@ function SetUpTestAddTypeError() { initial_sum = 42n; } + function TestAddTypeError() { let sum = initial_sum; for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) { @@ -73,6 +74,7 @@ function SetUpTestAddZero() { initial_sum = 42n; } + function TestAddZero() { let sum = initial_sum; @@ -91,6 +93,7 @@ function SetUpTestAddSameSign(bits) { a = SmallRandomBigIntWithBits(bits); } + function TestAddSameSign() { let sum = initial_sum; @@ -109,6 +112,7 @@ function SetUpTestAddDifferentSign(bits) { a = -SmallRandomBigIntWithBits(bits); } + function TestAddDifferentSign() { let sum = initial_sum; @@ -131,6 +135,7 @@ function SetUpTestAddRandom() { } } + function TestAddRandom() { let sum = 0n; diff --git a/test/js-perf-test/BigInt/as-uint-n.js b/test/js-perf-test/BigInt/as-uint-n.js index f69d4978db..9c174f7b8e 100644 --- a/test/js-perf-test/BigInt/as-uint-n.js +++ b/test/js-perf-test/BigInt/as-uint-n.js @@ -6,6 +6,8 @@ load('bigint-util.js'); +let random_bigints = []; + // This dummy ensures that the feedback for benchmark.run() in the Measure // function from base.js is not monomorphic, thereby preventing the benchmarks // below from being inlined. This ensures consistent behavior and comparable diff --git a/test/js-perf-test/BigInt/bigint-util.js b/test/js-perf-test/BigInt/bigint-util.js index 7e426060c1..71bc61c480 100644 --- a/test/js-perf-test/BigInt/bigint-util.js +++ b/test/js-perf-test/BigInt/bigint-util.js @@ -4,6 +4,12 @@ "use strict"; +// Test configuration. +const TEST_ITERATIONS = 1000; +const SLOW_TEST_ITERATIONS = 50; +const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]; +const RANDOM_BIGINTS_MAX_BITS = 64 * 100; + function RandomHexDigit(allow_zero) { const chars = allow_zero ? '0123456789ABCDEF' : '123456789ABCDEF'; diff --git a/test/js-perf-test/BigInt/run.js b/test/js-perf-test/BigInt/run.js index 2f2cc30285..621b947dae 100644 --- a/test/js-perf-test/BigInt/run.js +++ b/test/js-perf-test/BigInt/run.js @@ -5,16 +5,13 @@ "use strict"; load('../base.js'); -load('test-config.js'); -load('to-boolean.js'); -load('add.js'); -load('as-uint-n.js'); - +load(arguments[0] + '.js'); var success = true; + function PrintResult(name, result) { - print(name + '-BigInt(Score): ' + result); + print(`BigInt-${name}(Score): ${result}`); } diff --git a/test/js-perf-test/BigInt/subtract.js b/test/js-perf-test/BigInt/subtract.js new file mode 100644 index 0000000000..076da37a8c --- /dev/null +++ b/test/js-perf-test/BigInt/subtract.js @@ -0,0 +1,148 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +"use strict"; + +load('bigint-util.js'); + +let initial_diff = 0n; +let a = 0n; +let random_bigints = []; + +// This dummy ensures that the feedback for benchmark.run() in the Measure +// function from base.js is not monomorphic, thereby preventing the benchmarks +// below from being inlined. This ensures consistent behavior and comparable +// results. +new BenchmarkSuite('Prevent-Inline-Dummy', [10000], [ + new Benchmark('Prevent-Inline-Dummy', true, false, 0, () => {}) +]); + + +new BenchmarkSuite('Subtract-TypeError', [10000], [ + new Benchmark('Subtract-TypeError', true, false, 0, TestSubtractTypeError, + SetUpTestSubtractTypeError) +]); + + +new BenchmarkSuite('Subtract-Zero', [1000], [ + new Benchmark('Subtract-Zero', true, false, 0, TestSubtractZero, + SetUpTestSubtractZero) +]); + + +BITS_CASES.forEach((d) => { + new BenchmarkSuite(`Subtract-SameSign-${d}`, [1000], [ + new Benchmark(`Subtract-SameSign-${d}`, true, false, 0, + TestSubtractSameSign, () => SetUpTestSubtractSameSign(d)) + ]); +}); + + +BITS_CASES.forEach((d) => { + new BenchmarkSuite(`Subtract-DifferentSign-${d}`, [1000], [ + new Benchmark(`Subtract-DifferentSign-${d}`, true, false, 0, + TestSubtractDifferentSign, () => SetUpTestSubtractDifferentSign(d)) + ]); +}); + + +new BenchmarkSuite('Subtract-Random', [1000], [ + new Benchmark('Subtract-Random', true, false, 0, TestSubtractRandom, + SetUpTestSubtractRandom) +]); + + +function SetUpTestSubtractSameSign(bits) { + // Subtract a small random negative value from the maximal negative number to + // make sure the result stays negative. + initial_diff = -MaxBigIntWithBits(bits); + a = -SmallRandomBigIntWithBits(bits); +} + + +function TestSubtractSameSign() { + let diff = initial_diff; + + for (let i = 0; i < TEST_ITERATIONS; ++i) { + diff = diff - a; + } + + return diff; +} + + +function SetUpTestSubtractDifferentSign(bits) { + // Subtract a small random negative value from a small positive value so that + // the differnce stays positive but does not grow in digits. + initial_diff = SmallRandomBigIntWithBits(bits); + a = -SmallRandomBigIntWithBits(bits); +} + + +function TestSubtractDifferentSign() { + let diff = initial_diff; + + for (let i = 0; i < TEST_ITERATIONS; ++i) { + diff = diff - a; + } + + return diff; +} + + +function SetUpTestSubtractRandom() { + random_bigints = []; + // RandomBigIntWithBits needs multiples of 4 bits. + const max_in_4bits = RANDOM_BIGINTS_MAX_BITS / 4; + for (let i = 0; i < TEST_ITERATIONS; ++i) { + const bits = Math.floor(Math.random() * max_in_4bits) * 4; + const bigint = RandomBigIntWithBits(bits); + random_bigints.push(Math.random() < 0.5 ? -bigint : bigint); + } +} + + +function TestSubtractRandom() { + let diff = 0n; + + for (let i = 0; i < TEST_ITERATIONS; ++i) { + diff = diff - random_bigints[i]; + } + + return diff; +} + + +function SetUpTestSubtractTypeError() { + initial_diff = 42n; +} + + +function TestSubtractTypeError() { + let diff = initial_diff; + for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) { + try { + diff = 0 - diff; + } + catch(e) { + } + } + return diff; +} + + +function SetUpTestSubtractZero() { + initial_diff = 42n; +} + + +function TestSubtractZero() { + let diff = initial_diff; + + for (let i = 0; i < TEST_ITERATIONS; ++i) { + diff = diff - 0n; + } + + return diff; +} diff --git a/test/js-perf-test/BigInt/test-config.js b/test/js-perf-test/BigInt/test-config.js deleted file mode 100644 index 8d77e70431..0000000000 --- a/test/js-perf-test/BigInt/test-config.js +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2019 the V8 project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -"use strict"; - -const TEST_ITERATIONS = 1000; -const SLOW_TEST_ITERATIONS = 50; -const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]; -const RANDOM_BIGINTS_MAX_BITS = 64 * 100; diff --git a/test/js-perf-test/BigInt/to-boolean.js b/test/js-perf-test/BigInt/to-boolean.js index 031c292a7d..c396da65b8 100644 --- a/test/js-perf-test/BigInt/to-boolean.js +++ b/test/js-perf-test/BigInt/to-boolean.js @@ -13,16 +13,19 @@ new BenchmarkSuite('Prevent-Inline-Dummy', [10000], [ new Benchmark('Prevent-Inline-Dummy', true, false, 0, () => {}) ]); -new BenchmarkSuite('BigInt-ToBoolean', [10000], [ - new Benchmark('BigInt-ToBoolean', true, false, 0, TestToBoolean), + +new BenchmarkSuite('ToBoolean', [10000], [ + new Benchmark('ToBoolean', true, false, 0, TestToBoolean), ]); -new BenchmarkSuite('BigInt-BooleanConstructor', [10000], [ - new Benchmark('BigInt-BooleanConstructor', true, false, 0, TestBooleanConstructor), + +new BenchmarkSuite('BooleanConstructor', [10000], [ + new Benchmark('BooleanConstructor', true, false, 0, TestBooleanConstructor), ]); -new BenchmarkSuite('BigInt-NewBooleanConstructor', [10000], [ - new Benchmark('BigInt-NewBooleanConstructor', true, false, 0, TestNewBooleanConstructor), + +new BenchmarkSuite('NewBooleanConstructor', [10000], [ + new Benchmark('NewBooleanConstructor', true, false, 0, TestNewBooleanConstructor), ]); diff --git a/test/js-perf-test/JSTests1.json b/test/js-perf-test/JSTests1.json index 9ac44cdfb8..d8c8a04684 100644 --- a/test/js-perf-test/JSTests1.json +++ b/test/js-perf-test/JSTests1.json @@ -48,53 +48,101 @@ { "name": "BigInt", "path": ["BigInt"], - "main": "run.js", - "resources": [ - "bigint-util.js", - "test-config.js", - "to-boolean.js", - "add.js", - "as-uint-n.js" - ], - "results_regexp": "^%s\\-BigInt\\(Score\\): (.+)$", "tests": [ - { "name": "BigInt-ToBoolean" }, - { "name": "BigInt-BooleanConstructor" }, - { "name": "BigInt-NewBooleanConstructor" }, - { "name": "Add-TypeError" }, - { "name": "Add-Zero" }, - { "name": "Add-SameSign-32" }, - { "name": "Add-DifferentSign-32" }, - { "name": "Add-SameSign-64" }, - { "name": "Add-DifferentSign-64" }, - { "name": "Add-SameSign-128" }, - { "name": "Add-DifferentSign-128" }, - { "name": "Add-SameSign-256" }, - { "name": "Add-DifferentSign-256" }, - { "name": "Add-SameSign-512" }, - { "name": "Add-DifferentSign-512" }, - { "name": "Add-SameSign-1024" }, - { "name": "Add-DifferentSign-1024" }, - { "name": "Add-SameSign-2048" }, - { "name": "Add-DifferentSign-2048" }, - { "name": "Add-SameSign-4096" }, - { "name": "Add-DifferentSign-4096" }, - { "name": "Add-SameSign-8192" }, - { "name": "Add-DifferentSign-8192" }, - { "name": "Add-Random" }, - { "name": "AsUint64-32" }, - { "name": "AsUint64-64" }, - { "name": "AsUint64-128" }, - { "name": "AsUint64-256" }, - { "name": "AsUint32-32" }, - { "name": "AsUint32-64" }, - { "name": "AsUint32-128" }, - { "name": "AsUint32-256" }, - { "name": "AsUint8-32" }, - { "name": "AsUint8-64" }, - { "name": "AsUint8-128" }, - { "name": "AsUint8-256" } - ] + { + "name": "ToBoolean", + "main": "run.js", + "resources": ["to-boolean.js"], + "test_flags": ["to-boolean"], + "results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$", + "tests": [ + { "name": "ToBoolean" }, + { "name": "BooleanConstructor" }, + { "name": "NewBooleanConstructor" } + ] + }, + { + "name": "Add", + "main": "run.js", + "resources": ["add.js", "bigint-util.js"], + "test_flags": ["add"], + "results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$", + "tests": [ + { "name": "Add-TypeError" }, + { "name": "Add-Zero" }, + { "name": "Add-SameSign-32" }, + { "name": "Add-DifferentSign-32" }, + { "name": "Add-SameSign-64" }, + { "name": "Add-DifferentSign-64" }, + { "name": "Add-SameSign-128" }, + { "name": "Add-DifferentSign-128" }, + { "name": "Add-SameSign-256" }, + { "name": "Add-DifferentSign-256" }, + { "name": "Add-SameSign-512" }, + { "name": "Add-DifferentSign-512" }, + { "name": "Add-SameSign-1024" }, + { "name": "Add-DifferentSign-1024" }, + { "name": "Add-SameSign-2048" }, + { "name": "Add-DifferentSign-2048" }, + { "name": "Add-SameSign-4096" }, + { "name": "Add-DifferentSign-4096" }, + { "name": "Add-SameSign-8192" }, + { "name": "Add-DifferentSign-8192" }, + { "name": "Add-Random" } + ] + }, + { + "name": "Subtract", + "main": "run.js", + "resources": ["subtract.js", "bigint-util.js"], + "test_flags": ["subtract"], + "results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$", + "tests": [ + { "name": "Subtract-TypeError" }, + { "name": "Subtract-Zero" }, + { "name": "Subtract-SameSign-32" }, + { "name": "Subtract-DifferentSign-32" }, + { "name": "Subtract-SameSign-64" }, + { "name": "Subtract-DifferentSign-64" }, + { "name": "Subtract-SameSign-128" }, + { "name": "Subtract-DifferentSign-128" }, + { "name": "Subtract-SameSign-256" }, + { "name": "Subtract-DifferentSign-256" }, + { "name": "Subtract-SameSign-512" }, + { "name": "Subtract-DifferentSign-512" }, + { "name": "Subtract-SameSign-1024" }, + { "name": "Subtract-DifferentSign-1024" }, + { "name": "Subtract-SameSign-2048" }, + { "name": "Subtract-DifferentSign-2048" }, + { "name": "Subtract-SameSign-4096" }, + { "name": "Subtract-DifferentSign-4096" }, + { "name": "Subtract-SameSign-8192" }, + { "name": "Subtract-DifferentSign-8192" }, + { "name": "Subtract-Random" } + ] + }, + { + "name": "AsUintN", + "main": "run.js", + "resources": ["as-uint-n.js", "bigint-util.js"], + "test_flags": ["as-uint-n"], + "results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$", + "tests": [ + { "name": "AsUint64-32" }, + { "name": "AsUint64-64" }, + { "name": "AsUint64-128" }, + { "name": "AsUint64-256" }, + { "name": "AsUint32-32" }, + { "name": "AsUint32-64" }, + { "name": "AsUint32-128" }, + { "name": "AsUint32-256" }, + { "name": "AsUint8-32" }, + { "name": "AsUint8-64" }, + { "name": "AsUint8-128" }, + { "name": "AsUint8-256" } + ] + } + ] }, { "name": "BigInt-Jitless", @@ -104,11 +152,12 @@ "to-boolean.js" ], "flags": ["--jitless"], - "results_regexp": "^%s\\-BigInt\\(Score\\): (.+)$", + "results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$", + "test_flags": ["to-boolean"], "tests": [ - { "name": "BigInt-ToBoolean" }, - { "name": "BigInt-BooleanConstructor" }, - { "name": "BigInt-NewBooleanConstructor" } + { "name": "ToBoolean" }, + { "name": "BooleanConstructor" }, + { "name": "NewBooleanConstructor" } ] }, {