From 589b2a1244da2f04843652bda97ef55397cdf5b7 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 25 Aug 2020 13:36:37 -0600 Subject: perf_hooks: add idleTime and event loop util MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use uv_metrics_idle_time() to return a high resolution millisecond timer of the amount of time the event loop has been idle since it was initialized. Include performance.eventLoopUtilization() API to handle the math of calculating the idle and active times. This has been added to prevent accidental miscalculations of the event loop utilization. Such as not taking into consideration offsetting nodeTiming.loopStart or timing differences when being called from a Worker thread. PR-URL: https://github.com/nodejs/node/pull/34938 Reviewed-By: James M Snell Reviewed-By: Adrian Estrada Reviewed-By: Juan José Arboleda Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Gireesh Punathil Reviewed-By: Gerhard Stöbich --- benchmark/README.md | 1 + benchmark/perf_hooks/bench-eventlooputil.js | 64 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 benchmark/perf_hooks/bench-eventlooputil.js (limited to 'benchmark') diff --git a/benchmark/README.md b/benchmark/README.md index 6a40d7af3e1..dc02db4abd8 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -32,6 +32,7 @@ directory, see [the guide on benchmarks](../doc/guides/writing-and-running-bench | module | Benchmarks for the `module` subsystem. | | net | Benchmarks for the `net` subsystem. | | path | Benchmarks for the `path` subsystem. | +| perf_hooks | Benchmarks for the `perf_hooks` subsystem. | | process | Benchmarks for the `process` subsystem. | | querystring | Benchmarks for the `querystring` subsystem. | | streams | Benchmarks for the `streams` subsystem. | 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); + }); +} -- cgit v1.2.3