diff options
author | James M Snell <jasnell@gmail.com> | 2019-01-07 11:36:35 -0800 |
---|---|---|
committer | jasnell <jasnell@gmail.com> | 2019-02-08 09:20:18 -0800 |
commit | bcdd228f90b3e9e428b584814e7d52627616456a (patch) | |
tree | eed1b405a1fd7e2f78bbf10e82d65c57d2c29105 /test | |
parent | 679c23f2ae1e62443f8d90e653824007ce174139 (diff) | |
download | android-node-v8-bcdd228f90b3e9e428b584814e7d52627616456a.tar.gz android-node-v8-bcdd228f90b3e9e428b584814e7d52627616456a.tar.bz2 android-node-v8-bcdd228f90b3e9e428b584814e7d52627616456a.zip |
perf_hooks: implement histogram based api
Add a sampling-based event loop delay monitor.
```js
const { monitorEventLoopDelay } = require('perf_hooks');
const h = monitorEventLoopDelay();
h.enable();
h.disable();
console.log(h.percentiles);
console.log(h.min);
console.log(h.max);
console.log(h.mean);
console.log(h.stddev);
console.log(h.percentile(50));
```
PR-URL: https://github.com/nodejs/node/pull/25378
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/sequential/test-performance-eventloopdelay.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/test/sequential/test-performance-eventloopdelay.js b/test/sequential/test-performance-eventloopdelay.js new file mode 100644 index 0000000000..82f47b6fb2 --- /dev/null +++ b/test/sequential/test-performance-eventloopdelay.js @@ -0,0 +1,99 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { + monitorEventLoopDelay +} = require('perf_hooks'); + +{ + const histogram = monitorEventLoopDelay(); + assert(histogram); + assert(histogram.enable()); + assert(!histogram.enable()); + histogram.reset(); + assert(histogram.disable()); + assert(!histogram.disable()); +} + +{ + [null, 'a', 1, false, Infinity].forEach((i) => { + common.expectsError( + () => monitorEventLoopDelay(i), + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE' + } + ); + }); + + [null, 'a', false, {}, []].forEach((i) => { + common.expectsError( + () => monitorEventLoopDelay({ resolution: i }), + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE' + } + ); + }); + + [-1, 0, Infinity].forEach((i) => { + common.expectsError( + () => monitorEventLoopDelay({ resolution: i }), + { + type: RangeError, + code: 'ERR_INVALID_OPT_VALUE' + } + ); + }); +} + +{ + const histogram = monitorEventLoopDelay({ resolution: 1 }); + histogram.enable(); + let m = 5; + function spinAWhile() { + common.busyLoop(1000); + if (--m > 0) { + setTimeout(spinAWhile, common.platformTimeout(500)); + } else { + histogram.disable(); + // The values are non-deterministic, so we just check that a value is + // present, as opposed to a specific value. + assert(histogram.min > 0); + assert(histogram.max > 0); + assert(histogram.stddev > 0); + assert(histogram.mean > 0); + assert(histogram.percentiles.size > 0); + for (let n = 1; n < 100; n = n + 0.1) { + assert(histogram.percentile(n) >= 0); + } + histogram.reset(); + assert.strictEqual(histogram.min, 9223372036854776000); + assert.strictEqual(histogram.max, 0); + assert(Number.isNaN(histogram.stddev)); + assert(Number.isNaN(histogram.mean)); + assert.strictEqual(histogram.percentiles.size, 1); + + ['a', false, {}, []].forEach((i) => { + common.expectsError( + () => histogram.percentile(i), + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE' + } + ); + }); + [-1, 0, 101].forEach((i) => { + common.expectsError( + () => histogram.percentile(i), + { + type: RangeError, + code: 'ERR_INVALID_ARG_VALUE' + } + ); + }); + } + } + spinAWhile(); +} |