From ac7c42e1d1591994a73c82657503a2dca558995c Mon Sep 17 00:00:00 2001 From: theanarkh Date: Wed, 17 Aug 2022 01:59:28 +0800 Subject: [PATCH] trace_events: add example PR-URL: https://github.com/nodejs/node/pull/43253 Reviewed-By: Erick Wendel --- doc/api/tracing.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/doc/api/tracing.md b/doc/api/tracing.md index e4ab6f16c502214..a0bd4060ca4d512 100644 --- a/doc/api/tracing.md +++ b/doc/api/tracing.md @@ -228,6 +228,48 @@ t2.enable(); console.log(trace_events.getEnabledCategories()); ``` +## Examples + +### Collect trace events data by inspector + +```js +'use strict'; + +const { Session } = require('inspector'); +const session = new Session(); +session.connect(); + +function post(message, data) { + return new Promise((resolve, reject) => { + session.post(message, data, (err, result) => { + if (err) + reject(new Error(JSON.stringify(err))); + else + resolve(result); + }); + }); +} + +async function collect() { + const data = []; + session.on('NodeTracing.dataCollected', (chunk) => data.push(chunk)); + session.on('NodeTracing.tracingComplete', () => { + // done + }); + const traceConfig = { includedCategories: ['v8'] }; + await post('NodeTracing.start', { traceConfig }); + // do something + setTimeout(() => { + post('NodeTracing.stop').then(() => { + session.disconnect(); + console.log(data); + }); + }, 1000); +} + +collect(); +``` + [Performance API]: perf_hooks.md [V8]: v8.md [`Worker`]: worker_threads.md#class-worker