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