Skip to content

Commit 216cb3f

Browse files
refackaddaleax
authored andcommitted
test,benchmark: stabilize child-process
also some cleanup PR-URL: #13457 Refs: #12817 Reviewed-By: Tobias Nießen <[email protected]>
1 parent 3d12e1b commit 216cb3f

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
'use strict';
22
const common = require('../common.js');
3+
const { exec, execSync } = require('child_process');
4+
const isWindows = process.platform === 'win32';
35

46
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, {
911
len: messagesLength,
1012
dur: [5]
1113
});
1214

13-
const child_process = require('child_process');
14-
const exec = child_process.exec;
15-
function main(conf) {
15+
function childProcessExecStdout(conf) {
1616
bench.start();
1717

18-
const dur = +conf.dur;
18+
const maxDuration = conf.dur * 1000;
1919
const len = +conf.len;
2020

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'] });
2623

2724
var bytes = 0;
28-
child.stdout.on('data', function(msg) {
25+
child.stdout.on('data', (msg) => {
2926
bytes += msg.length;
3027
});
3128

32-
setTimeout(function() {
29+
setTimeout(() => {
3330
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+
}
3738
} else {
3839
child.kill();
3940
}
40-
}, dur * 1000);
41+
}, maxDuration);
4142
}

0 commit comments

Comments
 (0)