Node.js trace_events.createTracing() Method
Last Updated :
19 Aug, 2020
The trace_events.createTracing(options) (Added in v10.0.0) method is an inbuilt application programming interface of the ‘trace_events’ module which creates the Tracing object and returns the same for the given set of categories. The trace_events module contains Tracing objects which is used to enable or disable tracing for sets of categories.
Syntax:
trace_events.createTracing(options)
In order to use this (trace_events.createTracing()) method, we need to import ‘trace_events’ module.
const trace_events = require('trace_events');
Parameters: This function accepts objects as parameters as mentioned above and described below:
Return Value<Tracing>: Returns an object which contains categories <string[]> and enabled <boolean> values.
Below programs illustrate the trace_events.createTracing() Method in Node.js:
Example 1: Filename: index.js
const tracing_events = require( "trace_events" );
const categories = [ 'myapp.category' ,
'v8' , 'node' , 'node.async_hooks' ,
'node.promises.rejections' ,
'node.vm.script' , 'node.perf.usertiming' ,
'node.perf.timerify'
];
const newTracing = tracing_events.createTracing(
{ categories });
console.log(newTracing);
|
Output:
>> Tracing {
enabled: false,
categories: ‘myapp.category, v8, node, node.async_hooks,
node.promises.rejections, node.vm.script, node.perf.usertiming, node.perf.timerify’
}
Example 2: Filename: index.js
const { createTracing } = require( 'trace_events' );
const tracing = createTracing({ categories: [ 'perf_hooks' ,
'node.promises.rejections' ] });
console.log( "Tracing Created..." );
console.log(tracing);
tracing.enable();
const perf_hooks = require( "perf_hooks" );
perf_hooks.performance.mark( "A" );
() => {
perf_hooks.performance.mark( "B" );
perf_hooks.performance.measure( "A to B" , "C" , "D" );
};
tracing.disable();
|
Run index.js file using the following command:
node index.js
Output:
Tracing Created…
Tracing { enabled: false, categories: ‘perf_hooks, node.promises.rejections’ }
Reference: https://nodejs.org/api/tracing.html#tracing_trace_events_createtracing_options
Share your thoughts in the comments
Please Login to comment...