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: nodejs#13110
Fixes: nodejs#13102
Fixes: nodejs#12433
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
Trott committed May 22, 2017
1 parent 171a43a commit c3067b5
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 c3067b5

Please sign in to comment.