Skip to content

Instantly share code, notes, and snippets.

@jameshartig
Last active August 29, 2015 14:16
Show Gist options
  • Save jameshartig/145f72c21aedf620abce to your computer and use it in GitHub Desktop.
Save jameshartig/145f72c21aedf620abce to your computer and use it in GitHub Desktop.
Cluster vs manually forking
//Straight from http://nodejs.org/api/cluster.html
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end();
}).listen(80);
}
var net = require('net'),
child_process = require('child_process'),
http = require('http'),
numCPUs = require('os').cpus().length,
handle, child;
if (process.argv.indexOf('--child') !== -1) {
//child
process.on('message', function(message, handle) {
if (message === 'listen') {
var server = new http.Server();
server.on('request', function(req, resp) {
res.writeHead(200);
res.end();
});
server.listen(handle);
}
});
process.send('handle');
return;
} else {
//parent
handle = net._createServerHandle('0.0.0.0', 80, 4);
for (var i = 0; i < numCPUs; i++) {
child = child_process.fork('./run.js', ['--child'], {stdio: [0, 1, 2, 'ipc']});
(function(c) {
c.on('message', function(message) {
if (message === 'handle') {
c.send('listen', handle);
}
});
}(child));
}
}
$ echo "{\"test\":true}" > send.txt
$ ab -c 1000 -n 10000 -p send.txt http://127.0.0.1/
cluster.js
=======================================================================================================
Concurrency Level: 1000
Time taken for tests: 1.895 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 750000 bytes
Total POSTed: 1380000
HTML transferred: 0 bytes
Requests per second: 5277.64 [#/sec] (mean)
Time per request: 189.479 [ms] (mean)
Time per request: 0.189 [ms] (mean, across all concurrent requests)
Transfer rate: 386.55 [Kbytes/sec] received
711.24 kb/s sent
1097.79 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 4 11.3 0 52
Processing: 37 175 31.2 174 232
Waiting: 31 174 31.5 173 231
Total: 76 179 24.2 176 248
manual.js
======================================================================================================
Concurrency Level: 1000
Time taken for tests: 1.346 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 753225 bytes
Total POSTed: 1425540
HTML transferred: 0 bytes
Requests per second: 7427.31 [#/sec] (mean)
Time per request: 134.638 [ms] (mean)
Time per request: 0.135 [ms] (mean, across all concurrent requests)
Transfer rate: 546.33 [Kbytes/sec] received
1033.98 kb/s sent
1580.31 kb/s total
Connection Times (ms)
min mean[+/-sd] median max
Connect: 17 49 9.8 49 79
Processing: 10 79 26.0 78 216
Waiting: 7 63 25.4 64 195
Total: 29 128 23.5 128 245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment