Skip to content

Commit 72429b3

Browse files
Trottjasnell
authored andcommitted
benchmark: allow no duration in benchmark tests
Imprecision in process.hrtime() in some situations can result in a zero duration being used as a denominator in benchmark tests. This would almost certainly never happen in real benchmarks. It is only likely in very short benchmarks like the type we run in our test suite to just make sure that the benchmark code is runnable. So, if the environment variable that we use in tests to indicate "allow ludicrously short benchmarks" is set, convert a zero duration for a benchmark to 1 nano-second. PR-URL: #13110 Fixes: #13102 Fixes: #12433 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent e29477a commit 72429b3

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

benchmark/common.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ Benchmark.prototype.end = function(operations) {
197197
throw new Error('called end() with operation count <= 0');
198198
}
199199
if (elapsed[0] === 0 && elapsed[1] === 0) {
200-
throw new Error('insufficient time precision for short benchmark');
200+
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED)
201+
throw new Error('insufficient clock precision for short benchmark');
202+
// avoid dividing by zero
203+
elapsed[1] = 1;
201204
}
202205

203206
const time = elapsed[0] + elapsed[1] / 1e9;

0 commit comments

Comments
 (0)