Skip to content

Instantly share code, notes, and snippets.

@AndreasMadsen
Created January 4, 2012 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasMadsen/1558988 to your computer and use it in GitHub Desktop.
Save AndreasMadsen/1558988 to your computer and use it in GitHub Desktop.
stdout and stderr pipe bechmark
$ node ./test.js
test: 262ms
GOT: 10240 KB
use pipe: no
$ node ./test.js
test: 248ms
GOT: 10240 KB
use pipe: no
$ node ./test.js
test: 257ms
GOT: 10240 KB
use pipe: no
$ node ./test.js
test: 248ms
GOT: 10240 KB
use pipe: no
$ node ./test.js pipe
test: 307ms
GOT: 10240 KB
use pipe: yes
$ node ./test.js pipe
test: 301ms
GOT: 10240 KB
use pipe: yes
$ node ./test.js pipe
test: 295ms
GOT: 10240 KB
use pipe: yes
$ node ./test.js pipe
test: 298ms
GOT: 10240 KB
use pipe: yes
var childProcess = require('child_process');
//Child
if (process.argv[2] === 'child') {
var oneKB = (new Array(1025)).join('a');
for (var i = 0; i < 10000; i++) {
process.stdout.write(oneKB);
}
}
//Parent
else if (process.argv[2] === 'parent') {
var doPipe = process.argv[3] == 'pipe';
var child = childProcess.fork(process.argv[1], ['child'], doPipe ? {silent: true} : undefined);
if (doPipe) {
child.stdout.pipe(process.stdout, {end: false});
child.stderr.pipe(process.stderr, {end: false});
}
child._channel.close();
child._channel = null;
child.on('exit', function () {
process.exit(0);
});
}
//testcase
else {
console.time('test');
var parent = childProcess.spawn(process.execPath, [process.argv[1], 'parent', process.argv[2]]);
var byte = 0;
parent.stdout.on('data', function (data) {
byte += data.length;
});
parent.on('exit', function () {
console.timeEnd('test');
console.log('GOT: ' + (byte/1000) + ' KB');
console.log('use pipe: ' + (process.argv[2] === 'pipe' ? 'yes' : 'no'));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment