forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-worker-prof.js
79 lines (73 loc) · 2.86 KB
/
test-worker-prof.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const assert = require('assert');
const util = require('util');
const { join } = require('path');
const { spawnSync } = require('child_process');
// Test that --prof also tracks Worker threads.
// Refs: https://github.com/nodejs/node/issues/24016
if (process.argv[2] === 'child') {
const fs = require('fs');
let files = fs.readdirSync(tmpdir.path);
const plog = files.filter((name) => /\.log$/.test(name))[0];
if (plog === undefined) {
console.error('`--prof` did not produce a profile log for parent thread!');
process.exit(1);
}
const pingpong = `
let counter = 0;
const fs = require('fs');
const { Worker, parentPort } = require('worker_threads');
parentPort.on('message', (m) => {
if (counter++ === 10)
process.exit(0);
parentPort.postMessage(
fs.readFileSync(m.toString()).slice(0, 1024 * 1024));
});
`;
const { Worker } = require('worker_threads');
const w = new Worker(pingpong, { eval: true });
w.on('message', (m) => {
w.postMessage(process.execPath);
});
w.on('exit', common.mustCall(() => {
files = fs.readdirSync(tmpdir.path);
const wlog = files.filter((name) => /\.log$/.test(name) && name !== plog)[0];
if (wlog === undefined) {
console.error('`--prof` did not produce a profile log' +
' for worker thread!');
process.exit(1);
}
process.exit(0);
}));
w.postMessage(process.execPath);
} else {
tmpdir.refresh();
const spawnResult = spawnSync(
process.execPath, ['--prof', __filename, 'child'],
{ cwd: tmpdir.path, encoding: 'utf8', timeout: 30_000 });
assert.strictEqual(spawnResult.stderr.toString(), '',
`child exited with an error: \
${util.inspect(spawnResult)}`);
assert.strictEqual(spawnResult.signal, null,
`child exited with signal: ${util.inspect(spawnResult)}`);
assert.strictEqual(spawnResult.status, 0,
`child exited with non-zero status: \
${util.inspect(spawnResult)}`);
const files = fs.readdirSync(tmpdir.path);
const logfiles = files.filter((name) => /\.log$/.test(name));
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
for (const logfile of logfiles) {
const lines = fs.readFileSync(
join(tmpdir.path, logfile), 'utf8').split('\n');
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
// Test that at least 15 ticks have been recorded for both parent and child
// threads. When not tracking Worker threads, only 1 or 2 ticks would
// have been recorded.
// When running locally on x64 Linux, this number is usually at least 200
// for both threads, so 15 seems like a very safe threshold.
assert(ticks >= 15, `${ticks} >= 15`);
}
}