Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

add w3cwebsocket protocol #40

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Nodejs cli tool for benchmarking websocket servers. Currently supports:
* [Faye](https://github.com/faye/faye)
* [Primus](https://github.com/primus/primus)
* [WAMP](https://github.com/tavendo/AutobahnJS)
* [Websocket](https://github.com/theturtle32/WebSocket-Node)

## Installation

Expand Down Expand Up @@ -45,7 +46,7 @@ command help
-g, --generator <file> Js file for generate message or special event
-m, --message <n> Number of message for a client. Default to 0
-o, --output <output> Output file
-t, --type <type> Type of websocket server to bench(socket.io, engine.io, faye, primus, wamp). Default to socket.io
-t, --type <type> Type of websocket server to bench(socket.io, engine.io, faye, primus, wamp, websocket). Default to socket.io
-p, --transport <type> Type of transport to websocket(engine.io, websockets, browserchannel, sockjs, socket.io). Default to websockets (Just for Primus)
-k, --keep-alive Keep alive connection
-v, --verbose Verbose Logging
Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*global require, process*/

var Benchmark = require('./lib/benchmark.js'),
DefaultReporter = require('./lib/defaultreporter.js'),
fs = require('fs'),
program = require('commander'),
logger = require('./lib/logger');
Expand All @@ -13,9 +12,10 @@ program
.option('-c, --concurency <n>', 'Concurent connection per second, Default to 20', parseInt)
.option('-w, --worker <n>', 'number of worker', parseInt)
.option('-g, --generator <file>', 'js file for generate message or special event')
.option('-r, --reporter <file>', 'js file for reporting result')
.option('-m, --message <n>', 'number of message for a client. Default to 0', parseInt)
.option('-o, --output <output>', 'Output file')
.option('-t, --type <type>', 'type of websocket server to bench(socket.io, engine.io, faye, primus, wamp). Default to io')
.option('-t, --type <type>', 'type of websocket server to bench(socket.io, engine.io, faye, primus, wamp, websocket). Default to io')
.option('-p, --transport <type>', 'type of transport to websocket(engine.io, websockets, browserchannel, sockjs, socket.io). Default to websockets')
.option('-k, --keep-alive', 'Keep alive connection')
.option('-v, --verbose', 'Verbose Logging')
Expand Down Expand Up @@ -56,6 +56,12 @@ if (!program.message) {
program.message = 0;
}

if (!program.reporter) {
program.reporter = __dirname + '/lib/defaultreporter.js';
}
if (program.reporter.indexOf('/') !== 0){
program.reporter = process.cwd() + '/' + program.reporter;
}
if (!program.type) {
program.type = 'socket.io';
}
Expand All @@ -71,6 +77,7 @@ logger.info('WS server : ' + program.type);

var options = {
generatorFile : program.generator,
reporterFile : program.reporter,
type : program.type,
transport : program.transport,
keepAlive : program.keepAlive,
Expand All @@ -81,6 +88,8 @@ if (program.verbose) {
logger.debug("Benchmark Options " + JSON.stringify(options));
}

var Reporter = require(program.reporter);

var outputStream = null;

if (program.output) {
Expand All @@ -90,7 +99,7 @@ if (program.output) {
outputStream = fs.createWriteStream(program.output);
}

var reporter = new DefaultReporter(outputStream);
var reporter = new Reporter(outputStream);
var bench = new Benchmark(server, reporter, options);

// On ctrl+c
Expand Down
4 changes: 3 additions & 1 deletion lib/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ Benchmark.prototype.terminate = function () {
this.close();
}

// process.exit will be called after report
// because we want to end the outputstream before exit
this._report();
};

Expand All @@ -162,7 +164,7 @@ Benchmark.prototype.terminate = function () {
*/
Benchmark.prototype._report = function () {
if (this.reporter) {
this.reporter.report(this.steps.getSteps(), this.monitor, this.stopwatch);
return this.reporter.report(this.steps.getSteps(), this.monitor, this.stopwatch);
}
};

Expand Down
9 changes: 7 additions & 2 deletions lib/defaultreporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var DefaultReporter = function (outputStream) {

};

DefaultReporter.prototype.report = function (steps, monitor, stopwatch) {
DefaultReporter.prototype.output = function (steps, monitor, stopwatch) {

var tableSteps = new Table({
head : ['Number', 'Connections', 'Errors', 'Duration(ms)']
Expand Down Expand Up @@ -48,4 +48,9 @@ DefaultReporter.prototype.report = function (steps, monitor, stopwatch) {

};

module.exports = DefaultReporter;
DefaultReporter.prototype.report = function (steps, monitor, stopwatch){
this.output(steps, monitor, stopwatch);
this.outputStream.end(function(){process.exit(monitor.results.errors ? 1 : 0)});
}

module.exports = DefaultReporter;
3 changes: 3 additions & 0 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ switch (workerType) {
case 'wamp':
BenchmarkWorker = require('./workers/wampworker.js');
break;
case 'websocket':
BenchmarkWorker = require('./workers/websocketworker.js');
break;
default:
logger.error('error workerType ' + workerType);
}
Expand Down
37 changes: 37 additions & 0 deletions lib/workers/websocketworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*global module, require*/
var WebsocketClient = require('websocket').w3cwebsocket,
util = require('util'),
BaseWorker = require('./baseworker.js'),
logger = require('../logger.js');

var WebsocketWorker = function(server, generator){
WebsocketWorker.super_.apply(this,arguments);
}

util.inherits(WebsocketWorker, BaseWorker);

WebsocketWorker.prototype.createClient = function (callback) {
var self = this;
var client = new WebsocketClient(this.server);

client.onopen = function (){
callback(false, client);
};
client.onerror = function(err){
if (self.verbose) {
logger.error("Websocket Worker error: " + JSON.parse(err));
}
callback(true,client);
};
}

WebsocketWorker.prototype.close =function () {
this.running = false;

for (var i = 0; i < this.clients.length; i++) {
try { this.clients[i].close(); } catch (err) {}
}
};

module.exports = WebsocketWorker;

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
"author": "",
"license": "BSD",
"dependencies": {
"socket.io-client": "~1.3.5",
"engine.io-client": "~0.9.0",
"commander": "~1.1.1",
"colors": "~0.6",
"autobahn": "~0.9.5",
"cli-table": "~0.3.0",
"colors": "~0.6",
"commander": "~1.1.1",
"engine.io-client": "~0.9.0",
"faye": "~0.8.9",
"winston": "~0.7.1",
"socket.io": "~1.3.5",
"primus": "~1.5.2",
"autobahn": "~0.9.5"
"socket.io": "~1.3.5",
"socket.io-client": "~1.3.5",
"websocket": "^1.0.22",
"winston": "~0.7.1"
},
"devDependencies": {
"chai": "~1.6.0",
Expand Down