test,benchmark: stabilize child-process

also some cleanup

PR-URL: https://github.com/nodejs/node/pull/13457
Refs: https://github.com/nodejs/node/issues/12817
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Refael Ackermann 2017-06-04 14:25:44 -04:00
parent 47b9772f52
commit 8d2bd5fa88

View File

@ -1,41 +1,42 @@
'use strict'; 'use strict';
const common = require('../common.js'); const common = require('../common.js');
const { exec, execSync } = require('child_process');
const isWindows = process.platform === 'win32';
var messagesLength = [64, 256, 1024, 4096]; var messagesLength = [64, 256, 1024, 4096];
// Windows does not support that long arguments // Windows does not support command lines longer than 8191 characters
if (process.platform !== 'win32') if (!isWindows) messagesLength.push(32768);
messagesLength.push(32768);
const bench = common.createBenchmark(main, { const bench = common.createBenchmark(childProcessExecStdout, {
len: messagesLength, len: messagesLength,
dur: [5] dur: [5]
}); });
const child_process = require('child_process'); function childProcessExecStdout(conf) {
const exec = child_process.exec;
function main(conf) {
bench.start(); bench.start();
const dur = +conf.dur; const maxDuration = conf.dur * 1000;
const len = +conf.len; const len = +conf.len;
const msg = `"${'.'.repeat(len)}"`; const cmd = `yes "${'.'.repeat(len)}"`;
// eslint-disable-next-line no-unescaped-regexp-dot const child = exec(cmd, { 'stdio': ['ignore', 'pipe', 'ignore'] });
msg.match(/./);
const options = {'stdio': ['ignore', 'pipe', 'ignore']};
const child = exec(`yes ${msg}`, options);
var bytes = 0; var bytes = 0;
child.stdout.on('data', function(msg) { child.stdout.on('data', (msg) => {
bytes += msg.length; bytes += msg.length;
}); });
setTimeout(function() { setTimeout(() => {
bench.end(bytes); bench.end(bytes);
if (process.platform === 'win32') { if (isWindows) {
// Sometimes there's a yes.exe process left hanging around on Windows... // Sometimes there's a yes.exe process left hanging around on Windows.
child_process.execSync(`taskkill /f /t /pid ${child.pid}`); try {
execSync(`taskkill /f /t /pid ${child.pid}`);
} catch (_) {
// this is a best effort kill. stderr is piped to parent for tracing.
}
} else { } else {
child.kill(); child.kill();
} }
}, dur * 1000); }, maxDuration);
} }