v8/test/js-perf-test/BigInt/add.js
Nico Hartmann ea79fa426d [js-perf-test] Performance benchmark for adding random BigInts
Bug: v8:9213
Change-Id: Iee04bfe25e37cfe639964f8ab31e6702d3750bfb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1645319
Commit-Queue: Nico Hartmann <nicohartmann@google.com>
Reviewed-by: Georg Neis <neis@chromium.org>
Reviewed-by: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62016}
2019-06-06 10:05:16 +00:00

148 lines
3.3 KiB
JavaScript

// 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');
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;
let initial_sum = 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('Add-TypeError', [10000], [
new Benchmark('Add-TypeError', true, false, 0, TestAddTypeError,
SetUpTestAddTypeError)
]);
new BenchmarkSuite('Add-Zero', [1000], [
new Benchmark('Add-Zero', true, false, 0, TestAddZero, SetUpTestAddZero)
]);
BITS_CASES.forEach((d) => {
new BenchmarkSuite(`Add-SameSign-${d}`, [1000], [
new Benchmark(`Add-SameSign-${d}`, true, false, 0, TestAddSameSign,
() => SetUpTestAddSameSign(d))
]);
});
BITS_CASES.forEach((d) => {
new BenchmarkSuite(`Add-DifferentSign-${d}`, [1000], [
new Benchmark(`Add-DifferentSign-${d}`, true, false, 0,
TestAddDifferentSign, () => SetUpTestAddDifferentSign(d))
]);
});
new BenchmarkSuite('Add-Random', [1000], [
new Benchmark('Add-Random', true, false, 0, TestAddRandom,
SetUpTestAddRandom)
]);
function SetUpTestAddTypeError() {
initial_sum = 42n;
}
function TestAddTypeError() {
let sum = initial_sum;
for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) {
try {
sum = 0 + sum;
}
catch(e) {
}
}
return sum;
}
function SetUpTestAddZero() {
initial_sum = 42n;
}
function TestAddZero() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = 0n + sum;
}
return sum;
}
function SetUpTestAddSameSign(bits) {
// Add two small random positive values to make sure the sum does not grow
// in digits.
initial_sum = SmallRandomBigIntWithBits(bits);
a = SmallRandomBigIntWithBits(bits);
}
function TestAddSameSign() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = a + sum;
}
return sum;
}
function SetUpTestAddDifferentSign(bits) {
// Add a small random negative value to a large positive one to make sure the
// sum does not shrink in digits.
initial_sum = MaxBigIntWithBits(bits);
a = -SmallRandomBigIntWithBits(bits);
}
function TestAddDifferentSign() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = a + sum;
}
return sum;
}
function SetUpTestAddRandom() {
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 TestAddRandom() {
let sum = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = random_bigints[i] + sum;
}
return sum;
}