From fbf7ff92d7f7a7f63ad4649519e8316d90b04826 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Tue, 6 Mar 2018 09:08:47 -0800 Subject: [PATCH] doc: add inspector usage example Add a simple example showing how to use the inspector API to access the CPU profiler. PR-URL: https://github.com/nodejs/node/pull/19172 Reviewed-By: Eugene Ostroukhov Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Khaidi Chu --- doc/api/inspector.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/api/inspector.md b/doc/api/inspector.md index b6f702f0dd5b60..c801510b7563f2 100644 --- a/doc/api/inspector.md +++ b/doc/api/inspector.md @@ -136,8 +136,34 @@ with an error. [`session.connect()`] will need to be called to be able to send messages again. Reconnected session will lose all inspector state, such as enabled agents or configured breakpoints. +## Example usage + +### 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][]: + +```js +const inspector = require('inspector'); + +const session = new inspector.Session(); +session.connect(); + +session.post('Profiler.enable', () => { + session.post('Profiler.start', () => { + // invoke business logic under measurement here... + + // some time later... + session.post('Profiler.stop', ({ profile }) => { + // write profile to disk, upload, etc. + }); + }); +}); +``` + [`session.connect()`]: #inspector_session_connect [`Debugger.paused`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger/#event-paused [`EventEmitter`]: events.html#events_class_eventemitter [Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/ +[CPU Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/Profiler