#231 基于Node.js的性能测试用的简易模拟器 rename
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
var http = require('http');
|
||||
var cluster = require('cluster');
|
||||
|
||||
var port = 8000;
|
||||
var port = 8888;
|
||||
var interval =10000;
|
||||
var numReqs = 0;
|
||||
|
||||
//worker handle http request
|
||||
function requestHandler(req, res){
|
||||
function requestHandler(request, response){
|
||||
try {
|
||||
// send response
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
var result='Hello world\n';
|
||||
res.end(result);
|
||||
response.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
var body='Hello world';
|
||||
response.end(body);
|
||||
|
||||
// notify master about the request
|
||||
process.send({ cmd: 'notifyRequest' });
|
||||
@@ -20,14 +20,14 @@ function requestHandler(req, res){
|
||||
}
|
||||
}
|
||||
|
||||
//master count requestes
|
||||
//master count requests
|
||||
function messageHandler(msg) {
|
||||
if (msg.cmd && msg.cmd == 'notifyRequest') {
|
||||
numReqs += 1;
|
||||
}
|
||||
}
|
||||
|
||||
//master print the statistics
|
||||
//master print statistics
|
||||
function flushStatistics(){
|
||||
var msg = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + ", " +numReqs + " requests, " + (numReqs*1000)/interval+ " TPS.";
|
||||
console.log(msg);
|
||||
@@ -35,14 +35,14 @@ function flushStatistics(){
|
||||
}
|
||||
|
||||
if (cluster.isMaster) {
|
||||
// Start workers
|
||||
// Master start workers
|
||||
var numCPUs = require('os').cpus().length;
|
||||
for (var i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
console.log(numCPUs + " servers running at port " + port);
|
||||
|
||||
//define the notify hanlder
|
||||
//define notification hanlder
|
||||
Object.keys(cluster.workers).forEach(function(id) {
|
||||
cluster.workers[id].on('message', messageHandler);
|
||||
});
|
||||
Reference in New Issue
Block a user