Skip to content

Commit

Permalink
perf_hooks,http2: add performance.clear()
Browse files Browse the repository at this point in the history
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: #18046
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell authored and evanlucas committed Jan 30, 2018
1 parent 30e2221 commit 645be73
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-http2-perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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'] });

Expand Down Expand Up @@ -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);
});

0 comments on commit 645be73

Please sign in to comment.