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