|
| 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