Created
July 23, 2012 01:30
-
-
Save blmarket/3161601 to your computer and use it in GitHub Desktop.
Test 4 cluster with expressjs
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 app = require('express').createServer(); | |
var fs = require('fs'); | |
app.get('/', function(req, res, next) { | |
console.log(process.pid); | |
res.send("Hi"); | |
}); | |
var stream = fs.createWriteStream("logfile", { flags: 'a' }); | |
function logandlog() { | |
stream.write(process.pid + "Says : " + Math.random() + "\n"); | |
setTimeout(logandlog, Math.random() * 50); | |
} | |
setTimeout(logandlog, Math.random() * 50); | |
module.exports = app; |
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 cluster = require('cluster'); | |
var nCPU = require('os').cpus().length; | |
if (cluster.isMaster) { | |
cluster.fork(); | |
for (var i = 1; i < nCPU - 1; i++) { | |
cluster.fork(); | |
} | |
cluster.on('exit', function(worker, code, signal) { | |
var exitCode = worker.process.exitCode; | |
console.log('worker ' + worker.process.pid + ' died (' + exitCode + '). restarting...'); | |
cluster.fork(); | |
}); | |
} else { | |
console.log(process.pid + " spawned."); | |
require('./app.js').listen(1234); | |
}; |
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 request = require('request'); | |
for (var i = 0; i < 100000; i++) { | |
request.get('http://127.0.0.1:1234', function(err, res, body) { | |
console.log(res.statusCode); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment