node/test/parallel/test-performance-gc.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

56 lines
1.5 KiB
JavaScript

// Flags: --expose-gc --no-warnings
'use strict';
const common = require('../common');
const assert = require('assert');
const {
PerformanceObserver,
constants
} = require('perf_hooks');
const {
NODE_PERFORMANCE_GC_MAJOR,
NODE_PERFORMANCE_GC_MINOR,
NODE_PERFORMANCE_GC_INCREMENTAL,
NODE_PERFORMANCE_GC_WEAKCB,
NODE_PERFORMANCE_GC_FLAGS_FORCED
} = constants;
const kinds = [
NODE_PERFORMANCE_GC_MAJOR,
NODE_PERFORMANCE_GC_MINOR,
NODE_PERFORMANCE_GC_INCREMENTAL,
NODE_PERFORMANCE_GC_WEAKCB
];
// Adding an observer should force at least one gc to appear
{
const obs = new PerformanceObserver(common.mustCallAtLeast((list) => {
const entry = list.getEntries()[0];
assert(entry);
assert.strictEqual(entry.name, 'gc');
assert.strictEqual(entry.entryType, 'gc');
assert(kinds.includes(entry.kind));
assert(kinds.includes(entry.detail.kind));
assert.strictEqual(entry.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED);
assert.strictEqual(entry.detail.flags, NODE_PERFORMANCE_GC_FLAGS_FORCED);
assert.strictEqual(typeof entry.startTime, 'number');
assert.strictEqual(typeof entry.duration, 'number');
obs.disconnect();
}));
obs.observe({ entryTypes: ['gc'] });
global.gc();
// Keep the event loop alive to witness the GC async callback happen.
setImmediate(() => setImmediate(() => 0));
}
// GC should not keep the event loop alive
{
let didCall = false;
process.on('beforeExit', () => {
assert(!didCall);
didCall = true;
global.gc();
});
}