Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #23 from einaros/master

Add clustering to node.js' ws benchmark, to utilize more cores
  • Loading branch information...
commit 55d31c19de931ab7bb25d63f6e0b892544051863 2 parents 1cd9ff6 + 55a0651
@ericmoritz ericmoritz authored
Showing with 22 additions and 7 deletions.
  1. +22 −7 competition/wsdemo-ws.js
View
29 competition/wsdemo-ws.js
@@ -1,10 +1,25 @@
#!/usr/bin/env node
-var ws = require('ws')
-, wss = new ws.Server({port:8000, host: "0.0.0.0"})
+var http = require('http')
+ , cluster = require('cluster')
+ , numCPUs = require('os').cpus().length
-wss.on('connection', function(con) {
-
- con.on('message', con.send)
-
-})
+if (cluster.isMaster) {
+ // Master Process
+ var versionInfo = process.version.substr(1).split('.').map(function(v) { return parseInt(v); });
+ if (versionInfo[1] !== 7 || versionInfo[2] < 11) {
+ console.error('Please use node v0.7.11+\nAfter installing v0.7, please run "npm rebuild ws".');
+ process.exit(-1);
+ }
+ for (var i = 0; i < numCPUs; i++) cluster.fork();
+}
+else {
+ // Worker Process
+ var ws = require('ws')
+ , server = http.createServer()
+ , wss = new ws.Server({server: server});
+ server.listen(8000, 3000); // increased socket backlog
+ wss.on('connection', function(con) {
+ con.on('message', con.send);
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.