-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: strengthen test-worker-prof #26608
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,78 @@ | ||
'use strict'; | ||
require('../common'); | ||
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'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
// Test that --prof also tracks Worker threads. | ||
// Refs: https://github.com/nodejs/node/issues/24016 | ||
|
||
if (process.argv[2] === 'child') { | ||
const spin = ` | ||
const start = Date.now(); | ||
while (Date.now() - start < 1000); | ||
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 { 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); | ||
return; | ||
} | ||
|
||
tmpdir.refresh(); | ||
spawnSync(process.execPath, ['--prof', __filename, 'child'], | ||
{ cwd: tmpdir.path }); | ||
const files = fs.readdirSync(tmpdir.path); | ||
const logfiles = files.filter((name) => /\.log$/.test(name)); | ||
assert.strictEqual(logfiles.length, 2); // Parent thread + child thread. | ||
const { Worker } = require('worker_threads'); | ||
const data = 'x'.repeat(1024); | ||
const w = new Worker(pingpong, { eval: true }); | ||
w.on('message', (m) => { | ||
w.postMessage(m.toString().split('').reverse().toString().replace(/,/g, '')); | ||
}); | ||
|
||
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(data); | ||
} else { | ||
tmpdir.refresh(); | ||
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; | ||
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 700 | ||
// for both threads, so 15 seems like a very safe threshold. | ||
assert(ticks >= 15, `${ticks} >= 15`); | ||
// 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`); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An absolute micronit here but maybe add a comment above this (right below the
else
line) saying this is the parent process?