perf_hooks,http2: add performance.clear()

Add missing clear() method to `perf_hooks.performance` to
remove the entries from the master timeline to prevent
that from being a memory leak.

PR-URL: https://github.com/nodejs/node/pull/18046
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell 2018-01-08 13:32:30 -08:00
parent 47a282293f
commit 20fe04f113
3 changed files with 21 additions and 1 deletions

View File

@ -29,6 +29,14 @@ added: v8.5.0
The `Performance` provides access to performance metric data. A single
instance of this class is provided via the `performance` property.
### performance.clearEntries(name)
<!-- YAML
added: REPLACEME
-->
Remove all performance entry objects with `entryType` equal to `name` from the
Performance Timeline.
### performance.clearFunctions([name])
<!-- YAML
added: v8.5.0

View File

@ -471,6 +471,10 @@ class Performance extends PerformanceObserverEntryList {
this[kClearEntry]('function', name);
}
clearEntries(name) {
this[kClearEntry](name);
}
timerify(fn) {
if (typeof fn !== 'function') {
const errors = lazyErrors();

View File

@ -6,7 +6,7 @@ if (!common.hasCrypto)
const assert = require('assert');
const h2 = require('http2');
const { PerformanceObserver } = require('perf_hooks');
const { PerformanceObserver, performance } = require('perf_hooks');
const obs = new PerformanceObserver(common.mustCall((items) => {
const entry = items.getEntries()[0];
@ -46,6 +46,7 @@ const obs = new PerformanceObserver(common.mustCall((items) => {
default:
assert.fail('invalid entry name');
}
performance.clearEntries('http2');
}, 4));
obs.observe({ entryTypes: ['http2'] });
@ -100,3 +101,10 @@ server.on('listening', common.mustCall(() => {
}));
}));
process.on('exit', () => {
const entries = performance.getEntries();
// There shouldn't be any http2 entries left over.
assert.strictEqual(entries.length, 1);
assert.strictEqual(entries[0], performance.nodeTiming);
});