node/test/sequential/test-performance-eventloopdelay.js
James M Snell f3eb224c83
perf_hooks: complete overhaul of the implementation
* Update the user timing implementation to conform to
  User Timing Level 3.
* Reimplement user timing and timerify with pure JavaScript
  implementations
* Simplify the C++ implementation for gc and http2 perf
* Runtime deprecate additional perf entry properties
  in favor of the standard detail argument
* Disable the `buffered` option on PerformanceObserver,
  all entries are queued and dispatched on setImmediate.
  Only entries with active observers are buffered.
* This does remove the user timing and timerify
  trace events. Because the trace_events are still
  considered experimental, those can be removed without
  a deprecation cycle. They are removed to improve
  performance and reduce complexity.

Old: `perf_hooks/usertiming.js n=100000: 92,378.01249733355`
New: perf_hooks/usertiming.js n=100000: 270,393.5280638482`

PR-URL: https://github.com/nodejs/node/pull/37136
Refs: https://github.com/nodejs/diagnostics/issues/464
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2021-02-22 08:46:11 -08:00

106 lines
2.6 KiB
JavaScript

// Flags: --expose-gc --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const {
monitorEventLoopDelay
} = require('perf_hooks');
const { sleep } = require('internal/util');
{
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) => {
assert.throws(
() => monitorEventLoopDelay(i),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
}
);
});
[null, 'a', false, {}, []].forEach((i) => {
assert.throws(
() => monitorEventLoopDelay({ resolution: i }),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
}
);
});
[-1, 0, Infinity].forEach((i) => {
assert.throws(
() => monitorEventLoopDelay({ resolution: i }),
{
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE'
}
);
});
}
{
const histogram = monitorEventLoopDelay({ resolution: 1 });
histogram.enable();
let m = 5;
function spinAWhile() {
sleep(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) => {
assert.throws(
() => histogram.percentile(i),
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE'
}
);
});
[-1, 0, 101, NaN].forEach((i) => {
assert.throws(
() => histogram.percentile(i),
{
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
}
);
});
}
}
spinAWhile();
}
// Make sure that the histogram instances can be garbage-collected without
// and not just implictly destroyed when the Environment is torn down.
process.on('exit', global.gc);