node/test/parallel/test-worker-cpu-profile.js
Ilyas Shabi 4e332e0aa8
v8: add cpu profile options
PR-URL: https://github.com/nodejs/node/pull/62684
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: theanarkh <theratliter@gmail.com>
2026-04-26 19:36:36 +00:00

56 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');
const worker = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.on('message', () => {});
`, { eval: true });
worker.on('online', common.mustCall(async () => {
{
const handle = await worker.startCpuProfile();
JSON.parse(await handle.stop());
// Stop again
JSON.parse(await handle.stop());
}
{
const handle = await worker.startCpuProfile({
sampleInterval: 0.5,
maxBufferSize: 8,
});
JSON.parse(await handle.stop());
}
{
const [handle1, handle2] = await Promise.all([
worker.startCpuProfile(),
worker.startCpuProfile(),
]);
const [profile1, profile2] = await Promise.all([
handle1.stop(),
handle2.stop(),
]);
JSON.parse(profile1);
JSON.parse(profile2);
}
{
await worker.startCpuProfile();
// It will be stopped automatically when the worker is terminated
}
worker.terminate();
}));
worker.once('exit', common.mustCall(async () => {
assert.throws(
() => worker.startCpuProfile({ maxBufferSize: 0 }),
common.expectsError({
code: 'ERR_OUT_OF_RANGE',
}));
await assert.rejects(worker.startCpuProfile(), {
code: 'ERR_WORKER_NOT_RUNNING'
});
}));