|
1 | 1 | 'use strict'; |
2 | 2 | const common = require('../common'); |
3 | 3 | const tmpdir = require('../common/tmpdir'); |
| 4 | +const fixtures = require('../common/fixtures'); |
4 | 5 | const fs = require('fs'); |
5 | 6 | const assert = require('assert'); |
6 | | -const util = require('util'); |
7 | | -const { join } = require('path'); |
8 | | -const { spawnSync } = require('child_process'); |
| 7 | +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); |
9 | 8 |
|
10 | 9 | // Test that --prof also tracks Worker threads. |
11 | 10 | // Refs: https://github.com/nodejs/node/issues/24016 |
12 | 11 |
|
13 | 12 | if (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 | | - `; |
| 13 | + let files = fs.readdirSync(process.cwd()); |
| 14 | + console.log('files found in working directory before worker starts', files); |
| 15 | + const parentProf = files.find((name) => name.endsWith('.log')); |
| 16 | + assert(parentProf, '--prof did not produce a profile log for parent thread'); |
| 17 | + console.log('parent prof file:', parentProf); |
32 | 18 |
|
33 | 19 | const { Worker } = require('worker_threads'); |
34 | | - const w = new Worker(pingpong, { eval: true }); |
35 | | - w.on('message', (m) => { |
36 | | - w.postMessage(__filename); |
| 20 | + const w = new Worker(fixtures.path('workload', 'fibonacci.js'), { |
| 21 | + env: { FIB: 20, } |
37 | 22 | }); |
38 | 23 |
|
39 | 24 | 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); |
| 25 | + files = fs.readdirSync(process.cwd()); |
| 26 | + console.log('files found in working directory before worker exits', files); |
| 27 | + const workerProf = files.find((name) => name !== parentProf && name.endsWith('.log')); |
| 28 | + assert(workerProf, '--prof did not produce a profile log for worker thread'); |
| 29 | + console.log('worker prof file:', workerProf); |
48 | 30 | })); |
49 | | - w.postMessage(__filename); |
50 | 31 | } else { |
51 | 32 | tmpdir.refresh(); |
52 | | - const timeout = common.platformTimeout(30_000); |
53 | | - const spawnResult = spawnSync( |
| 33 | + |
| 34 | + const workerProfRegexp = /worker prof file: (.+\.log)/; |
| 35 | + const parentProfRegexp = /parent prof file: (.+\.log)/; |
| 36 | + const { stdout } = spawnSyncAndExitWithoutError( |
54 | 37 | 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. |
| 38 | + { cwd: tmpdir.path, encoding: 'utf8' |
| 39 | + }, { |
| 40 | + stdout(output) { |
| 41 | + assert.match(output, workerProfRegexp); |
| 42 | + assert.match(output, parentProfRegexp); |
| 43 | + return true; |
| 44 | + } |
| 45 | + }); |
| 46 | + const workerProf = stdout.match(workerProfRegexp)[1]; |
| 47 | + const parentProf = stdout.match(parentProfRegexp)[1]; |
| 48 | + |
| 49 | + const logfiles = fs.readdirSync(tmpdir.path).filter((name) => name.endsWith('.log')); |
| 50 | + assert.deepStrictEqual(new Set(logfiles), new Set([workerProf, parentProf])); |
| 51 | + |
| 52 | + const workerLines = fs.readFileSync(tmpdir.resolve(workerProf), 'utf8').split('\n'); |
| 53 | + const parentLines = fs.readFileSync(tmpdir.resolve(parentProf), 'utf8').split('\n'); |
67 | 54 |
|
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; |
| 55 | + const workerTicks = workerLines.filter((line) => line.startsWith('tick')); |
| 56 | + const parentTicks = parentLines.filter((line) => line.startsWith('tick')); |
72 | 57 |
|
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 | | - } |
| 58 | + // Test that at least 15 ticks have been recorded for both parent and child |
| 59 | + // threads. When not tracking Worker threads, only 1 or 2 ticks would |
| 60 | + // have been recorded. |
| 61 | + // When running locally, this number is usually around 100 |
| 62 | + // for both threads, so 15 seems like a very safe threshold. |
| 63 | + // TODO(joyeecheung): if this still flakes, we could log the time spent on the |
| 64 | + // workload in the worker and the parent, and divide that with v8's |
| 65 | + // prof_sampling_interval to get a better expectation. |
| 66 | + assert(workerTicks.length > 15, `worker ticks ${workerTicks} <= 15`); |
| 67 | + assert(parentTicks.length > 15, `parent ticks ${parentTicks} <= 15`); |
80 | 68 | } |
0 commit comments