Skip to content

Commit

Permalink
benchmark: allow no duration in benchmark tests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Trott authored and jasnell committed May 23, 2017
1 parent 46fa6f9 commit 592c14c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ Benchmark.prototype.end = function(operations) {
throw new Error('called end() with operation count <= 0');
}
if (elapsed[0] === 0 && elapsed[1] === 0) {
throw new Error('insufficient time precision for short benchmark');
if (!process.env.NODEJS_BENCHMARK_ZERO_ALLOWED)
throw new Error('insufficient clock precision for short benchmark');
// avoid dividing by zero
elapsed[1] = 1;
}

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

0 comments on commit 592c14c

Please sign in to comment.