worker.startCpuProfile(name)


使用给定的 name 启动 CPU 配置文件,然后返回一个以错误或具有 stop 方法的对象完成的 Promise。调用 stop 方法将停止收集配置文件,然后返回一个 Promise,该 Promise 会以错误或配置文件数据的形式完成。

¥Starting a CPU profile with the given name, then return a Promise that fulfills with an error or an object which has a stop method. Calling the stop method will stop collecting the profile, then return a Promise that fulfills with an error or the profile data.

const { Worker } = require('node:worker_threads');

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  parentPort.on('message', () => {});
  `, { eval: true });

worker.on('online', async () => {
  const handle = await worker.startCpuProfile('demo');
  const profile = await handle.stop();
  console.log(profile);
  worker.terminate();
});