Created
December 12, 2012 18:43
-
-
Save brettkiefer/4270418 to your computer and use it in GitHub Desktop.
This is a tiny adaptation of the cluster example at http://nodejs.org/api/cluster.html to illustrate the problem i'm seeing in https://github.com/joyent/node/issues/3241
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
// a tiny adaptation of the cluster example at http://nodejs.org/api/cluster.html | |
// 1. save as test.js | |
// 2. run with "./node test.js > pid.log" | |
// 3. from another shell do "siege -t1m -c 50 http://localhost:8000" | |
// 5. wait for the siege to finish | |
// 6. ctrl-c the node process | |
// 7. sort pid.log | uniq -c | |
// The left side is # of requests served, the right side is pid. | |
// If it's balanced (as it is for me on the 2.6.32 linux kernel) then yay! | |
// If it's nowhere near balanced (as it is for me on the the 3.2.0 linux kernel) then oh no! | |
var cluster = require('cluster'); | |
var http = require('http'); | |
var numCPUs = Math.max(1, Math.floor(require('os').cpus().length / 2)); | |
if (cluster.isMaster) { | |
// Fork workers. | |
for (var i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('exit', function(worker, code, signal) { | |
console.log('worker ' + worker.process.pid + ' died'); | |
}); | |
} else { | |
// Workers can share any TCP connection | |
// In this case its a HTTP server | |
http.createServer(function(req, res) { | |
res.writeHead(200); | |
res.end("hello world from pid " + process.pid + "\n"); | |
console.log(process.pid); | |
}).listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment