diff options
Diffstat (limited to 'benchmark/perf_hooks/bench-eventlooputil.js')
-rw-r--r-- | benchmark/perf_hooks/bench-eventlooputil.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/benchmark/perf_hooks/bench-eventlooputil.js b/benchmark/perf_hooks/bench-eventlooputil.js new file mode 100644 index 00000000000..984b2b66aec --- /dev/null +++ b/benchmark/perf_hooks/bench-eventlooputil.js @@ -0,0 +1,64 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert').ok; +const { performance } = require('perf_hooks'); +const { nodeTiming, eventLoopUtilization } = performance; + +const bench = common.createBenchmark(main, { + n: [1e6], + method: [ + 'idleTime', + 'ELU_simple', + 'ELU_passed', + ], +}); + +function main({ method, n }) { + switch (method) { + case 'idleTime': + benchIdleTime(n); + break; + case 'ELU_simple': + benchELUSimple(n); + break; + case 'ELU_passed': + benchELUPassed(n); + break; + default: + throw new Error(`Unsupported method ${method}`); + } +} + +function benchIdleTime(n) { + bench.start(); + for (let i = 0; i < n; i++) + nodeTiming.idleTime; + bench.end(n); +} + +function benchELUSimple(n) { + // Need to put this in setImmediate or will always return 0. + setImmediate(() => { + const elu = eventLoopUtilization(); + assert(elu.active + elu.idle > 0); + + bench.start(); + for (let i = 0; i < n; i++) + eventLoopUtilization(); + bench.end(n); + }); +} + +function benchELUPassed(n) { + // Need to put this in setImmediate or will always return 0. + setImmediate(() => { + let elu = eventLoopUtilization(); + assert(elu.active + elu.idle > 0); + + bench.start(); + for (let i = 0; i < n; i++) + elu = eventLoopUtilization(elu); + bench.end(n); + }); +} |