Skip to content

Commit 02691ce

Browse files
committed
doc: add inspector API example for heapdump
1 parent ed465eb commit 02691ce

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

doc/api/inspector.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ to the run-time events.
154154

155155
## Example usage
156156

157+
Apart from the debugger, various V8 Profilers are available through the DevTools
158+
protocol.
159+
157160
### CPU Profiler
158161

159-
Apart from the debugger, various V8 Profilers are available through the DevTools
160-
protocol. Here's a simple example showing how to use the [CPU profiler][]:
162+
Here's an example showing how to use the [CPU Profiler][]:
161163

162164
```js
163165
const inspector = require('inspector');
@@ -180,8 +182,33 @@ session.post('Profiler.enable', () => {
180182
});
181183
```
182184

185+
### Heap Profiler
186+
187+
Here's an example showing how to use the [Heap Profiler][]:
188+
189+
```js
190+
const inspector = require('inspector');
191+
const fs = require('fs');
192+
const session = new inspector.Session();
193+
194+
const fd = fs.openSync('profile.heapsnapshot', 'w');
195+
196+
session.connect();
197+
198+
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
199+
fs.writeSync(fd, m.params.chunk);
200+
});
201+
202+
session.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => {
203+
console.log('Runtime.takeHeapSnapshot done:', err, r);
204+
session.disconnect();
205+
fs.closeSync(fd);
206+
});
207+
```
208+
183209
[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused
184210
[`EventEmitter`]: events.html#events_class_eventemitter
185211
[`session.connect()`]: #inspector_session_connect
186212
[CPU Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/Profiler
187213
[Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/
214+
[Heap Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
common.skipIfInspectorDisabled();
5+
6+
// Test that example code in doc/api/inspector.md continues to work with the V8
7+
// experimental API.
8+
9+
const assert = require('assert');
10+
const inspector = require('inspector');
11+
const session = new inspector.Session();
12+
13+
session.connect();
14+
15+
const chunks = [];
16+
17+
session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
18+
chunks.push(m.params.chunk);
19+
});
20+
21+
session.post('HeapProfiler.takeHeapSnapshot', null, common.mustCall((e, r) => {
22+
assert.ifError(e);
23+
assert.deepStrictEqual(r, {});
24+
session.disconnect();
25+
26+
const profile = JSON.parse(chunks.join(''));
27+
assert(profile.snapshot.meta);
28+
assert(profile.snapshot.node_count > 0);
29+
assert(profile.snapshot.edge_count > 0);
30+
}));

0 commit comments

Comments
 (0)