Skip to content

Commit 896962f

Browse files
committed
test: add Worker + --prof regression test
Fixes: #24016 PR-URL: #26011 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 6c1e928 commit 896962f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/parallel/test-worker-prof.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const { spawnSync } = require('child_process');
7+
const { Worker } = require('worker_threads');
8+
9+
if (!common.isMainThread)
10+
common.skip('process.chdir is not available in Workers');
11+
12+
// Test that --prof also tracks Worker threads.
13+
// Refs: https://github.com/nodejs/node/issues/24016
14+
15+
if (process.argv[2] === 'child') {
16+
const spin = `
17+
const start = Date.now();
18+
while (Date.now() - start < 200);
19+
`;
20+
new Worker(spin, { eval: true });
21+
eval(spin);
22+
return;
23+
}
24+
25+
tmpdir.refresh();
26+
process.chdir(tmpdir.path);
27+
spawnSync(process.execPath, ['--prof', __filename, 'child']);
28+
const logfiles = fs.readdirSync('.').filter((name) => /\.log$/.test(name));
29+
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
30+
31+
for (const logfile of logfiles) {
32+
const lines = fs.readFileSync(logfile, 'utf8').split('\n');
33+
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
34+
35+
// Test that at least 20 ticks have been recorded for both parent and child
36+
// threads. When not tracking Worker threads, only 1 or 2 ticks would
37+
// have been recorded.
38+
// When running locally on x64 Linux, this number is usually at least 150
39+
// for both threads, so 15 seems like a very safe threshold.
40+
assert(ticks >= 15, `${ticks} >= 15`);
41+
}

0 commit comments

Comments
 (0)