Skip to content

Inspector

该模块主要作用就是用于使用浏览器进行调试 nodejs 程序

可以使用以下方式访问它:

js
const inspector = require('node:inspector/promises');
// 或
const inspector = require('node:inspector');

下面是一个示例,演示如何使用 CPU Profiler:

js
const inspector = require('node:inspector')
const fs = require('node:fs')
const session = new inspector.Session();
session.connect();

function fn() {
  session.post('Profiler.enable');
  session.post('Profiler.start');
  // 在此调用需要测试的业务逻辑...

  // 异步一段时间后...
  session.post('Profiler.stop', (err, params) => {
    if (err) throw err;

    // 将配置文件写入磁盘、上传等
    fs.writeFileSync('./profile.cpuprofile', JSON.stringify(params.profile)); 
  });

}

fn()

Released under the MIT License.