Created
January 4, 2012 07:39
-
-
Save AndreasMadsen/1558988 to your computer and use it in GitHub Desktop.
stdout and stderr pipe bechmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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