worker.startCpuProfile(name)


使用给定的 name 启动 CPU 分析,然后返回一个 Promise,该 Promise 会返回一个错误或包含 stop 方法的对象。调用 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();
});