Created
April 5, 2012 19:48
-
-
Save kfox/2313556 to your computer and use it in GitHub Desktop.
A clustered HTTP server for HTTP connection testing
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 http = require('http'), | |
cluster = require('cluster'); | |
var numCPUs = require('os').cpus().length; | |
var host = process.argv[2] || '3.0.0.2'; | |
var port = process.argv[3] || 80; | |
if (cluster.isMaster) { | |
// this is the master, so spawn workers | |
for (var i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('death', function(worker) { | |
console.log('worker ' + worker.pid + ' died'); | |
cluster.fork(); | |
}); | |
} else { | |
// do worker stuff | |
server = http.createServer(); | |
worker = process.env.NODE_WORKER_ID; | |
server.on('request', function(req, res) { | |
res.writeHead(200); | |
res.end(); | |
}); | |
server.on('clientError', function(e) { | |
console.log('!! client error:', e); | |
}); | |
server.listen(port, host, function () { | |
console.log('==> worker %d listening on %s:%d', worker, host, port); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment