Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,doc: add inspector API example for heapdump #26498

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions doc/api/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
30 changes: 30 additions & 0 deletions test/parallel/test-inspector-heapdump.js
Original file line number Diff line number Diff line change
@@ -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);
}));