mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
benchmark: use process.hrtime.bigint()
PR-URL: https://github.com/nodejs/node/pull/38369 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
0577fe3f26
commit
4e9ad206e2
1 changed files with 21 additions and 9 deletions
|
@ -12,7 +12,7 @@ class Benchmark {
|
||||||
this._ended = false;
|
this._ended = false;
|
||||||
|
|
||||||
// Holds process.hrtime value
|
// Holds process.hrtime value
|
||||||
this._time = [0, 0];
|
this._time = 0n;
|
||||||
|
|
||||||
// Use the file name as the name of the benchmark
|
// Use the file name as the name of the benchmark
|
||||||
this.name = require.main.filename.slice(__dirname.length + 1);
|
this.name = require.main.filename.slice(__dirname.length + 1);
|
||||||
|
@ -218,12 +218,12 @@ class Benchmark {
|
||||||
throw new Error('Called start more than once in a single benchmark');
|
throw new Error('Called start more than once in a single benchmark');
|
||||||
}
|
}
|
||||||
this._started = true;
|
this._started = true;
|
||||||
this._time = process.hrtime();
|
this._time = process.hrtime.bigint();
|
||||||
}
|
}
|
||||||
|
|
||||||
end(operations) {
|
end(operations) {
|
||||||
// Get elapsed time now and do error checking later for accuracy.
|
// 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) {
|
if (!this._started) {
|
||||||
throw new Error('called end without start');
|
throw new Error('called end without start');
|
||||||
|
@ -237,16 +237,19 @@ class Benchmark {
|
||||||
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED && operations <= 0) {
|
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED && operations <= 0) {
|
||||||
throw new Error('called end() with operation count <= 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)
|
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED)
|
||||||
throw new Error('insufficient clock precision for short benchmark');
|
throw new Error('insufficient clock precision for short benchmark');
|
||||||
// Avoid dividing by zero
|
// Avoid dividing by zero
|
||||||
elapsed[1] = 1;
|
this.report(operations && Number.MAX_VALUE, 0n);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._ended = true;
|
const elapsed = time - this._time;
|
||||||
const time = elapsed[0] + elapsed[1] / 1e9;
|
const rate = operations / (Number(elapsed) / 1e9);
|
||||||
const rate = operations / time;
|
|
||||||
this.report(rate, elapsed);
|
this.report(rate, elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,12 +258,21 @@ class Benchmark {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
conf: this.config,
|
conf: this.config,
|
||||||
rate,
|
rate,
|
||||||
time: elapsed[0] + elapsed[1] / 1e9,
|
time: nanoSecondsToString(elapsed),
|
||||||
type: 'report',
|
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) {
|
function formatResult(data) {
|
||||||
// Construct configuration string, " A=a, B=b, ..."
|
// Construct configuration string, " A=a, B=b, ..."
|
||||||
let conf = '';
|
let conf = '';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue