From 72429b39811cc8e981aeda03be254121bef81fb9 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 18 May 2017 16:31:56 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/13110 Fixes: https://github.com/nodejs/node/issues/13102 Fixes: https://github.com/nodejs/node/issues/12433 Reviewed-By: Refael Ackermann Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung --- benchmark/common.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/benchmark/common.js b/benchmark/common.js index 32a1697422570a..bc9c21b3902ad7 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -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;