Navya Krishna
Navya Krishna

Reputation: 43

Is there any option to Start Profiler programatically from node.js?

I am looking on implementing the performance analysis for the web page. I have gone through some of the tools available and found Dev tools will be helpful. Is there any REST API to trigger the profiler from node.js?

Upvotes: 4

Views: 277

Answers (1)

ZachB
ZachB

Reputation: 15366

Yes, see documentation here: https://nodejs.org/api/inspector.html#inspector_cpu_profiler

const inspector = require('inspector');
const fs = require('fs');
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', (err, { profile }) => {
      // Write profile to disk, upload, etc.
      if (!err) {
        fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile));
      }
    });
  });
});

Upvotes: 2

Related Questions