test: strengthen test-worker-prof
Force main and worker to stay for some deterministic time Add some more validation check around profile file generation Fixes: https://github.com/nodejs/node/issues/26401 PR-URL: https://github.com/nodejs/node/pull/26608 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
parent
c0f031c5bd
commit
ed849f812d
@ -1,40 +1,78 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const tmpdir = require('../common/tmpdir');
|
const tmpdir = require('../common/tmpdir');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const util = require('util');
|
||||||
const { join } = require('path');
|
const { join } = require('path');
|
||||||
const { spawnSync } = require('child_process');
|
const { spawnSync } = require('child_process');
|
||||||
const { Worker } = require('worker_threads');
|
|
||||||
|
|
||||||
// Test that --prof also tracks Worker threads.
|
// Test that --prof also tracks Worker threads.
|
||||||
// Refs: https://github.com/nodejs/node/issues/24016
|
// Refs: https://github.com/nodejs/node/issues/24016
|
||||||
|
|
||||||
if (process.argv[2] === 'child') {
|
if (process.argv[2] === 'child') {
|
||||||
const spin = `
|
let files = fs.readdirSync(tmpdir.path);
|
||||||
const start = Date.now();
|
const plog = files.filter((name) => /\.log$/.test(name))[0];
|
||||||
while (Date.now() - start < 1000);
|
if (plog === undefined) {
|
||||||
|
console.error('`--prof` did not produce a profile log for parent thread!');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const pingpong = `
|
||||||
|
let counter = 0;
|
||||||
|
const { Worker, parentPort } = require('worker_threads');
|
||||||
|
parentPort.on('message', (m) => {
|
||||||
|
if (counter++ === 1024)
|
||||||
|
process.exit(0);
|
||||||
|
parentPort.postMessage(
|
||||||
|
m.toString().split('').reverse().toString().replace(/,/g, ''));
|
||||||
|
});
|
||||||
`;
|
`;
|
||||||
new Worker(spin, { eval: true });
|
|
||||||
eval(spin);
|
const { Worker } = require('worker_threads');
|
||||||
return;
|
const data = 'x'.repeat(1024);
|
||||||
}
|
const w = new Worker(pingpong, { eval: true });
|
||||||
|
w.on('message', (m) => {
|
||||||
tmpdir.refresh();
|
w.postMessage(m.toString().split('').reverse().toString().replace(/,/g, ''));
|
||||||
spawnSync(process.execPath, ['--prof', __filename, 'child'],
|
});
|
||||||
{ cwd: tmpdir.path });
|
|
||||||
const files = fs.readdirSync(tmpdir.path);
|
w.on('exit', common.mustCall(() => {
|
||||||
const logfiles = files.filter((name) => /\.log$/.test(name));
|
files = fs.readdirSync(tmpdir.path);
|
||||||
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread.
|
const wlog = files.filter((name) => /\.log$/.test(name) && name !== plog)[0];
|
||||||
|
if (wlog === undefined) {
|
||||||
for (const logfile of logfiles) {
|
console.error('`--prof` did not produce a profile log' +
|
||||||
const lines = fs.readFileSync(join(tmpdir.path, logfile), 'utf8').split('\n');
|
' for worker thread!');
|
||||||
const ticks = lines.filter((line) => /^tick,/.test(line)).length;
|
process.exit(1);
|
||||||
|
}
|
||||||
// Test that at least 15 ticks have been recorded for both parent and child
|
process.exit(0);
|
||||||
// threads. When not tracking Worker threads, only 1 or 2 ticks would
|
}));
|
||||||
// have been recorded.
|
w.postMessage(data);
|
||||||
// When running locally on x64 Linux, this number is usually at least 700
|
} else {
|
||||||
// for both threads, so 15 seems like a very safe threshold.
|
tmpdir.refresh();
|
||||||
assert(ticks >= 15, `${ticks} >= 15`);
|
const spawnResult = spawnSync(
|
||||||
|
process.execPath, ['--prof', __filename, 'child'],
|
||||||
|
{ cwd: tmpdir.path, encoding: 'utf8' });
|
||||||
|
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`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user