Created
March 19, 2013 19:53
-
-
Save baudehlo/5199506 to your computer and use it in GitHub Desktop.
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 cluster = require('cluster'); | |
var numCPUs = parseInt(process.argv[2]) || 1; | |
var ready_workers = []; | |
if (cluster.isMaster) { | |
for (var i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('online', function (worker) { | |
ready_workers.push(worker); | |
if (ready_workers.length === numCPUs) send_msg(); | |
}); | |
var scount = 0; | |
var time = process.hrtime(); | |
var i = 0; | |
function send_msg () { | |
// send 5000 then give the event loop a look-in. | |
for (var n=0; n < 5000; n++) { | |
var worker = ready_workers[i++]; | |
worker.send('msg'); | |
scount++; | |
if (i === numCPUs) i = 0; | |
} | |
process.nextTick(send_msg); | |
} | |
setInterval(function () { | |
var diff = process.hrtime(time); | |
time = process.hrtime(); | |
var secs = (diff[0] * 1e9 + diff[1]) / 1e9; | |
process.stdout.write("Rate: " + parseInt(scount/secs) + " \r"); | |
scount = 0; | |
}, 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment