|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common.js');
|
| 3 | +const { exec, execSync } = require('child_process'); |
| 4 | +const isWindows = process.platform === 'win32'; |
3 | 5 |
|
4 | 6 | var messagesLength = [64, 256, 1024, 4096];
|
5 |
| -// Windows does not support that long arguments |
6 |
| -if (process.platform !== 'win32') |
7 |
| - messagesLength.push(32768); |
8 |
| -const bench = common.createBenchmark(main, { |
| 7 | +// Windows does not support command lines longer than 8191 characters |
| 8 | +if (!isWindows) messagesLength.push(32768); |
| 9 | + |
| 10 | +const bench = common.createBenchmark(childProcessExecStdout, { |
9 | 11 | len: messagesLength,
|
10 | 12 | dur: [5]
|
11 | 13 | });
|
12 | 14 |
|
13 |
| -const child_process = require('child_process'); |
14 |
| -const exec = child_process.exec; |
15 |
| -function main(conf) { |
| 15 | +function childProcessExecStdout(conf) { |
16 | 16 | bench.start();
|
17 | 17 |
|
18 |
| - const dur = +conf.dur; |
| 18 | + const maxDuration = conf.dur * 1000; |
19 | 19 | const len = +conf.len;
|
20 | 20 |
|
21 |
| - const msg = `"${'.'.repeat(len)}"`; |
22 |
| - // eslint-disable-next-line no-unescaped-regexp-dot |
23 |
| - msg.match(/./); |
24 |
| - const options = {'stdio': ['ignore', 'pipe', 'ignore']}; |
25 |
| - const child = exec(`yes ${msg}`, options); |
| 21 | + const cmd = `yes "${'.'.repeat(len)}"`; |
| 22 | + const child = exec(cmd, { 'stdio': ['ignore', 'pipe', 'ignore'] }); |
26 | 23 |
|
27 | 24 | var bytes = 0;
|
28 |
| - child.stdout.on('data', function(msg) { |
| 25 | + child.stdout.on('data', (msg) => { |
29 | 26 | bytes += msg.length;
|
30 | 27 | });
|
31 | 28 |
|
32 |
| - setTimeout(function() { |
| 29 | + setTimeout(() => { |
33 | 30 | bench.end(bytes);
|
34 |
| - if (process.platform === 'win32') { |
35 |
| - // Sometimes there's a yes.exe process left hanging around on Windows... |
36 |
| - child_process.execSync(`taskkill /f /t /pid ${child.pid}`); |
| 31 | + if (isWindows) { |
| 32 | + // Sometimes there's a yes.exe process left hanging around on Windows. |
| 33 | + try { |
| 34 | + execSync(`taskkill /f /t /pid ${child.pid}`); |
| 35 | + } catch (_) { |
| 36 | + // this is a best effort kill. stderr is piped to parent for tracing. |
| 37 | + } |
37 | 38 | } else {
|
38 | 39 | child.kill();
|
39 | 40 | }
|
40 |
| - }, dur * 1000); |
| 41 | + }, maxDuration); |
41 | 42 | }
|
0 commit comments