Created
August 28, 2016 08:33
-
-
Save AMEYCHAVAN/6186994be2b414a3f7a68a3f74ec5777 to your computer and use it in GitHub Desktop.
expresscluster.js
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'); | |
if(cluster.isMaster) { | |
var numWorkers = require('os').cpus().length; | |
console.log('Master cluster setting up ' + numWorkers + ' workers...'); | |
for(var i = 0; i < numWorkers; i++) { | |
cluster.fork(); | |
} | |
cluster.on('online', function(worker) { | |
console.log('Worker ' + worker.process.pid + ' is online'); | |
}); | |
cluster.on('exit', function(worker, code, signal) { | |
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); | |
console.log('Starting a new worker'); | |
cluster.fork(); | |
}); | |
} else { | |
var app = require('express')(); | |
app.get('/a', function (req, res) { | |
//ab -n 100000 -c 100 http://localhost:3000/a | |
res.send('Hello World!'); | |
}); | |
var server = app.listen(8000, function() { | |
console.log('Process ' + process.pid + ' is listening to all incoming requests'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment