doc: add inspector API example for heapdump

PR-URL: https://github.com/nodejs/node/pull/26498
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Sam Roberts 2019-03-07 08:48:54 -08:00 committed by Daniel Bevenius
parent 8d669bbeb1
commit a445244a0c
2 changed files with 59 additions and 2 deletions

View File

@ -154,10 +154,12 @@ to the run-time events.
## Example usage
Apart from the debugger, various V8 Profilers are available through the DevTools
protocol.
### CPU Profiler
Apart from the debugger, various V8 Profilers are available through the DevTools
protocol. Here's a simple example showing how to use the [CPU profiler][]:
Here's an example showing how to use the [CPU Profiler][]:
```js
const inspector = require('inspector');
@ -180,8 +182,33 @@ session.post('Profiler.enable', () => {
});
```
### Heap Profiler
Here's an example showing how to use the [Heap Profiler][]:
```js
const inspector = require('inspector');
const fs = require('fs');
const session = new inspector.Session();
const fd = fs.openSync('profile.heapsnapshot', 'w');
session.connect();
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
fs.writeSync(fd, m.params.chunk);
});
session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
console.log('Runtime.takeHeapSnapshot done:', err, r);
session.disconnect();
fs.closeSync(fd);
});
```
[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused
[`EventEmitter`]: events.html#events_class_eventemitter
[`session.connect()`]: #inspector_session_connect
[CPU Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/Profiler
[Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/
[Heap Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler

View File

@ -0,0 +1,30 @@
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
// Test that example code in doc/api/inspector.md continues to work with the V8
// experimental API.
const assert = require('assert');
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
const chunks = [];
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
chunks.push(m.params.chunk);
});
session.post('HeapProfiler.takeHeapSnapshot', null, common.mustCall((e, r) => {
assert.ifError(e);
assert.deepStrictEqual(r, {});
session.disconnect();
const profile = JSON.parse(chunks.join(''));
assert(profile.snapshot.meta);
assert(profile.snapshot.node_count > 0);
assert(profile.snapshot.edge_count > 0);
}));