|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common.js');
|
3 |
| -const { spawn } = require('child_process'); |
| 3 | +const { spawnSync } = require('child_process'); |
4 | 4 | const path = require('path');
|
5 | 5 |
|
6 | 6 | let Worker; // Lazy loaded in main
|
7 | 7 |
|
8 | 8 | const bench = common.createBenchmark(main, {
|
9 |
| - dur: [1], |
10 | 9 | script: [
|
11 | 10 | 'benchmark/fixtures/require-builtins',
|
12 |
| - 'benchmark/fixtures/require-cachable', |
13 | 11 | 'test/fixtures/semicolon',
|
14 | 12 | ],
|
15 |
| - mode: ['process', 'worker'] |
16 |
| -}, { |
17 |
| - flags: ['--expose-internals'] |
| 13 | + mode: ['process', 'worker'], |
| 14 | + count: [30], |
18 | 15 | });
|
19 | 16 |
|
20 |
| -function spawnProcess(script) { |
| 17 | +function spawnProcess(script, bench, state) { |
21 | 18 | const cmd = process.execPath || process.argv[0];
|
22 |
| - const argv = ['--expose-internals', script]; |
23 |
| - return spawn(cmd, argv); |
24 |
| -} |
| 19 | + while (state.finished < state.count) { |
| 20 | + const child = spawnSync(cmd, [script]); |
| 21 | + if (child.status !== 0) { |
| 22 | + console.log('---- STDOUT ----'); |
| 23 | + console.log(child.stdout.toString()); |
| 24 | + console.log('---- STDERR ----'); |
| 25 | + console.log(child.stderr.toString()); |
| 26 | + throw new Error(`Child process stopped with exit code ${child.status}`); |
| 27 | + } |
| 28 | + state.finished++; |
| 29 | + if (state.finished === 0) { |
| 30 | + // Finished warmup. |
| 31 | + bench.start(); |
| 32 | + } |
25 | 33 |
|
26 |
| -function spawnWorker(script) { |
27 |
| - return new Worker(script, { stderr: true, stdout: true }); |
| 34 | + if (state.finished === state.count) { |
| 35 | + bench.end(state.count); |
| 36 | + } |
| 37 | + } |
28 | 38 | }
|
29 | 39 |
|
30 |
| -function start(state, script, bench, getNode) { |
31 |
| - const node = getNode(script); |
32 |
| - let stdout = ''; |
33 |
| - let stderr = ''; |
34 |
| - |
35 |
| - node.stdout.on('data', (data) => { |
36 |
| - stdout += data; |
37 |
| - }); |
38 |
| - |
39 |
| - node.stderr.on('data', (data) => { |
40 |
| - stderr += data; |
41 |
| - }); |
42 |
| - |
43 |
| - node.on('exit', (code) => { |
| 40 | +function spawnWorker(script, bench, state) { |
| 41 | + const child = new Worker(script); |
| 42 | + child.on('exit', (code) => { |
44 | 43 | if (code !== 0) {
|
45 |
| - console.error('------ stdout ------'); |
46 |
| - console.error(stdout); |
47 |
| - console.error('------ stderr ------'); |
48 |
| - console.error(stderr); |
49 |
| - throw new Error(`Error during node startup, exit code ${code}`); |
| 44 | + throw new Error(`Worker stopped with exit code ${code}`); |
50 | 45 | }
|
51 |
| - state.throughput++; |
52 |
| - |
53 |
| - if (state.go) { |
54 |
| - start(state, script, bench, getNode); |
| 46 | + state.finished++; |
| 47 | + if (state.finished === 0) { |
| 48 | + // Finished warmup. |
| 49 | + bench.start(); |
| 50 | + } |
| 51 | + if (state.finished < state.count) { |
| 52 | + spawnProcess(script, bench, state); |
55 | 53 | } else {
|
56 |
| - bench.end(state.throughput); |
| 54 | + bench.end(state.count); |
57 | 55 | }
|
58 | 56 | });
|
59 | 57 | }
|
60 | 58 |
|
61 |
| -function main({ dur, script, mode }) { |
62 |
| - const state = { |
63 |
| - go: true, |
64 |
| - throughput: 0 |
65 |
| - }; |
66 |
| - |
67 |
| - setTimeout(() => { |
68 |
| - state.go = false; |
69 |
| - }, dur * 1000); |
70 |
| - |
| 59 | +function main({ count, script, mode }) { |
71 | 60 | script = path.resolve(__dirname, '../../', `${script}.js`);
|
| 61 | + const warmup = 3; |
| 62 | + const state = { count, finished: -warmup }; |
72 | 63 | if (mode === 'worker') {
|
73 | 64 | Worker = require('worker_threads').Worker;
|
74 |
| - bench.start(); |
75 |
| - start(state, script, bench, spawnWorker); |
| 65 | + spawnWorker(script, bench, state); |
76 | 66 | } else {
|
77 |
| - bench.start(); |
78 |
| - start(state, script, bench, spawnProcess); |
| 67 | + spawnProcess(script, bench, state); |
79 | 68 | }
|
80 | 69 | }
|
0 commit comments