Reputation: 1091
I tried node clusters without any performance improvement. May be I might be measuring it wrongly.
Using Node.js 7.4.0
server.js
const http = require('http');
const server = http.createServer((req, res) => {
for (let i = 0; i < 10000; i++) {
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end("Hello World!");
}).listen(4002);
I followed this tutorial to create cluster server http://rowanmanning.com/posts/node-cluster-and-express/
cluster.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(cluster.worker.id);
cluster.fork();
});
} else {
http.createServer((req, res) => {
for (let i = 0; i < 10000; i++) {
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end("Hello World!");
}).listen(4001);
}
I can see pids for master and workers on 4 cores when I do
$ps axl | grep node
0 0 8799 8789 20 0 682196 28056 ep_pol Sl+ pts/1 0:00 node index.js
0 0 8805 8799 20 0 682196 27708 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js
0 0 8811 8799 20 0 682196 27608 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js
0 0 8812 8799 20 0 682196 27756 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js
0 0 8818 8799 20 0 682196 27604 ep_pol Sl+ pts/1 0:00 /usr/bin/nodejs /home/ubuntu/index.js
As Rowan claims 4x performance increase, I barely got any performance out of clustering.
My siege results on t2.xlarge AWS instance (4 core) ubuntu machine.
server.js - $siege -c100 -t1M http://xx.xx.xx.xx:4002/
Transactions: 13545 hits
Availability: 100.00 %
Elapsed time: 59.39 secs
Data transferred: 0.16 MB
Response time: 0.19 secs
Transaction rate: 228.07 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 42.30
Successful transactions: 13545
Failed transactions: 0
Longest transaction: 11.61
Shortest transaction: 0.02
Now cluster.js
cluster.js - $siege -c100 -t1M http://xx.xx.xx.xx:4001/
Transactions: 13223 hits
Availability: 100.00 %
Elapsed time: 59.96 secs
Data transferred: 0.15 MB
Response time: 0.16 secs
Transaction rate: 220.53 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 35.95
Successful transactions: 13223
Failed transactions: 0
Longest transaction: 11.56
Shortest transaction: 0.02
Surprisingly, cluster.js performed poorly compared to server.js.
Am I doing something wrong? Is it a change in node.js version? or are these claims false?
https://keyholesoftware.com/2015/01/26/improve-node-js-performance-by-turning-it-into-a-clusterfork/
https://www.sitepoint.com/how-to-create-a-node-js-cluster-for-speeding-up-your-apps/
Any help is much appreciated.
Upvotes: 1
Views: 284
Reputation: 1519
So this is similar to:
NodeJS clustering sending all the request to one worker (Windows)
and
clustering in node.js is not working. Only one worker is always responding
Try something like:
http.createServer(function(req, res) {
console.log('worker:' + cluster.worker.id + " going to send response ");
//(Heavy I/O operation...followed by res.send(result))
}).listen(8000);
Upvotes: 1