From ef6a5cf40a7fe7f68fe85e1528970f351e31ed13 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 6 Nov 2016 16:47:41 -0800 Subject: [PATCH] benchmark: split timers benchmark and refactor The depth benchmark for timers sets a timer that sets a timer that sets a timer that... 500K of them. Since each timer has to wait for the next tick of the event loop this benchmark takes a very long time to run compared to the breadth test that is already in the file. This may be more of an event loop benchmark than a timer benchmark. Reduce the number of iterations for the depth test as it's really just running the iterations in sequence, not in parallel. And even on an infinitely fast machine, it would take over 8 minutes to run because each tick of the event loop would have to wait 1ms before firing the timer. Split the depth and breadth benchmarks so that their `N` values can be set independently. Do some minor refactoring to the benchmarks (but no ES6 additions so that the benchmarks can still be run with old versions of Node.js). Refs: https://github.com/nodejs/node/issues/9493 PR-URL: https://github.com/nodejs/node/pull/9497 Reviewed-By: Andreas Madsen Reviewed-By: Colin Ihrig Reviewed-By: Jeremiah Senkpiel --- benchmark/timers/timers-breadth.js | 20 +++++++++++++++ benchmark/timers/timers-depth.js | 20 +++++++++++++++ benchmark/timers/timers.js | 41 ------------------------------ 3 files changed, 40 insertions(+), 41 deletions(-) create mode 100644 benchmark/timers/timers-breadth.js create mode 100644 benchmark/timers/timers-depth.js delete mode 100644 benchmark/timers/timers.js diff --git a/benchmark/timers/timers-breadth.js b/benchmark/timers/timers-breadth.js new file mode 100644 index 0000000000..1101ee7dbf --- /dev/null +++ b/benchmark/timers/timers-breadth.js @@ -0,0 +1,20 @@ +'use strict'; +var common = require('../common.js'); + +var bench = common.createBenchmark(main, { + thousands: [500], +}); + +function main(conf) { + var N = +conf.thousands * 1e3; + var n = 0; + bench.start(); + function cb() { + n++; + if (n === N) + bench.end(N / 1e3); + } + for (var i = 0; i < N; i++) { + setTimeout(cb, 1); + } +} diff --git a/benchmark/timers/timers-depth.js b/benchmark/timers/timers-depth.js new file mode 100644 index 0000000000..d5efc5c672 --- /dev/null +++ b/benchmark/timers/timers-depth.js @@ -0,0 +1,20 @@ +'use strict'; +var common = require('../common.js'); + +var bench = common.createBenchmark(main, { + thousands: [1], +}); + +function main(conf) { + var N = +conf.thousands * 1e3; + var n = 0; + bench.start(); + setTimeout(cb, 1); + function cb() { + n++; + if (n === N) + bench.end(N / 1e3); + else + setTimeout(cb, 1); + } +} diff --git a/benchmark/timers/timers.js b/benchmark/timers/timers.js deleted file mode 100644 index 13b18fffc5..0000000000 --- a/benchmark/timers/timers.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; -var common = require('../common.js'); - -var bench = common.createBenchmark(main, { - thousands: [500], - type: ['depth', 'breadth'] -}); - -function main(conf) { - var n = +conf.thousands * 1e3; - if (conf.type === 'breadth') - breadth(n); - else - depth(n); -} - -function depth(N) { - var n = 0; - bench.start(); - setTimeout(cb); - function cb() { - n++; - if (n === N) - bench.end(N / 1e3); - else - setTimeout(cb); - } -} - -function breadth(N) { - var n = 0; - bench.start(); - function cb() { - n++; - if (n === N) - bench.end(N / 1e3); - } - for (var i = 0; i < N; i++) { - setTimeout(cb); - } -}