diff --git a/README.md b/README.md index 782cbd8..bff7056 100644 --- a/README.md +++ b/README.md @@ -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 @@ -45,7 +46,7 @@ command help -g, --generator Js file for generate message or special event -m, --message Number of message for a client. Default to 0 -o, --output Output file - -t, --type Type of websocket server to bench(socket.io, engine.io, faye, primus, wamp). Default to socket.io + -t, --type Type of websocket server to bench(socket.io, engine.io, faye, primus, wamp, websocket). Default to socket.io -p, --transport 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 diff --git a/index.js b/index.js index 341884e..f787abd 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -13,9 +12,10 @@ program .option('-c, --concurency ', 'Concurent connection per second, Default to 20', parseInt) .option('-w, --worker ', 'number of worker', parseInt) .option('-g, --generator ', 'js file for generate message or special event') + .option('-r, --reporter ', 'js file for reporting result') .option('-m, --message ', 'number of message for a client. Default to 0', parseInt) .option('-o, --output ', 'Output file') - .option('-t, --type ', 'type of websocket server to bench(socket.io, engine.io, faye, primus, wamp). Default to io') + .option('-t, --type ', 'type of websocket server to bench(socket.io, engine.io, faye, primus, wamp, websocket). Default to io') .option('-p, --transport ', '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') @@ -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'; } @@ -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, @@ -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) { @@ -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 diff --git a/lib/benchmark.js b/lib/benchmark.js index d114f99..c512234 100644 --- a/lib/benchmark.js +++ b/lib/benchmark.js @@ -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(); }; @@ -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); } }; diff --git a/lib/defaultreporter.js b/lib/defaultreporter.js index a78c0e8..95c64e3 100644 --- a/lib/defaultreporter.js +++ b/lib/defaultreporter.js @@ -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)'] @@ -48,4 +48,9 @@ DefaultReporter.prototype.report = function (steps, monitor, stopwatch) { }; -module.exports = DefaultReporter; \ No newline at end of file +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; diff --git a/lib/worker.js b/lib/worker.js index 27f2d01..4e3433c 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -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); } diff --git a/lib/workers/websocketworker.js b/lib/workers/websocketworker.js new file mode 100644 index 0000000..79a5bbe --- /dev/null +++ b/lib/workers/websocketworker.js @@ -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; + diff --git a/package.json b/package.json index f6c8182..a2de0ac 100644 --- a/package.json +++ b/package.json @@ -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",