MstartCpuProfile
Bun

method

worker_threads.Worker.startCpuProfile

): Promise<CPUProfileHandle>;

Starting a CPU profile then return a Promise that fulfills with an error or an CPUProfileHandle object. This API supports await using syntax.

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({ sampleInterval: 1 });
  const profile = await handle.stop();
  console.log(profile);
  worker.terminate();
});

await using example.

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

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

w.on('online', async () => {
  // Stop profile automatically when return and profile will be discarded
  await using handle = await w.startCpuProfile();
});

Referenced types

interface CPUProfileOptions

  • maxBufferSize?: number

    Maximum number of samples to keep before older entries are discarded. Default: 4294967295.

  • sampleInterval?: number

    Requested sampling interval in milliseconds. Default: 0.

interface CPUProfileHandle

  • [Symbol.asyncDispose](): Promise<void>;

    Stopping collecting the profile and the profile will be discarded.

  • stop(): Promise<string>;

    Stopping collecting the profile, then return a Promise that fulfills with an error or the profile data.