Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: use process.hrtime.bigint() #38369

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions benchmark/common.js
Expand Up @@ -12,7 +12,7 @@ class Benchmark {
this._ended = false;

// Holds process.hrtime value
this._time = [0, 0];
this._time = 0n;

// Use the file name as the name of the benchmark
this.name = require.main.filename.slice(__dirname.length + 1);
Expand Down Expand Up @@ -218,12 +218,12 @@ class Benchmark {
throw new Error('Called start more than once in a single benchmark');
}
this._started = true;
this._time = process.hrtime();
this._time = process.hrtime.bigint();
}

end(operations) {
// Get elapsed time now and do error checking later for accuracy.
const elapsed = process.hrtime(this._time);
const time = process.hrtime.bigint();

if (!this._started) {
throw new Error('called end without start');
Expand All @@ -237,16 +237,19 @@ class Benchmark {
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED && operations <= 0) {
throw new Error('called end() with operation count <= 0');
}
if (elapsed[0] === 0 && elapsed[1] === 0) {

this._ended = true;

if (time === this._time) {
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED)
throw new Error('insufficient clock precision for short benchmark');
// Avoid dividing by zero
elapsed[1] = 1;
this.report(operations && Number.MAX_VALUE, 0n);
return;
}

this._ended = true;
const time = elapsed[0] + elapsed[1] / 1e9;
const rate = operations / time;
const elapsed = time - this._time;
const rate = operations / (Number(elapsed) / 1e9);
this.report(rate, elapsed);
}

Expand All @@ -255,12 +258,21 @@ class Benchmark {
name: this.name,
conf: this.config,
rate,
time: elapsed[0] + elapsed[1] / 1e9,
time: nanoSecondsToString(elapsed),
type: 'report',
});
}
}

function nanoSecondsToString(bigint) {
const str = bigint.toString();
const decimalPointIndex = str.length - 9;
if (decimalPointIndex < 0) {
return `0.${'0'.repeat(-decimalPointIndex)}${str}`;
}
return `${str.slice(0, decimalPointIndex)}.${str.slice(decimalPointIndex)}`;
}

function formatResult(data) {
// Construct configuration string, " A=a, B=b, ..."
let conf = '';
Expand Down