Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 718
Navya Krishna Avatar asked Nov 04 '25 05:11

Navya Krishna


1 Answers

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));
      }
    });
  });
});
like image 136
ZachB Avatar answered Nov 07 '25 02:11

ZachB