Created
April 21, 2011 04:04
-
-
Save shinout/933692 to your computer and use it in GitHub Desktop.
multiprocess node test (requires linestream)
This file contains 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 spawn = require('child_process').spawn; | |
var exec = require('child_process').exec; | |
var LineStream = require('linestream'); | |
var parallel = Number(process.argv[2]) || 40; | |
var N = 1000; | |
var script = 'number.js'; | |
var total = 0; | |
var endcount = 0; | |
exec('which node', function(err, stdout, stderr) { | |
var exepath = (stdout.charAt(stdout.length -1) == '\n') ? stdout.slice(0, -1) : stdout; | |
for (var i=0; i < parallel; i++) { | |
var node = spawn(exepath, [script, N]); | |
var lstream = new LineStream(node.stdout, {trim: true}); | |
lstream.on('data', function(data) { | |
total += Number(data); | |
}); | |
lstream.on('error', function(data) { | |
console.log(data); | |
}); | |
lstream.on('end', function() { | |
endcount++; | |
if (endcount == parallel) { | |
doEnd(); | |
} | |
}); | |
} | |
}); | |
function doEnd() { | |
console.log('total: ' + total); | |
var expected = 0; | |
for (var i=0; i < N; i++) { | |
expected += i; | |
} | |
console.log('expected: ' + expected * parallel); | |
} |
This file contains 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 limit = Number(process.argv[2]) || 100000; | |
for (var i=0; i < limit; i++) { | |
console.log(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment