From bfc8c8bbf5a8727fff50dd4b0324b0dd190b5cef Mon Sep 17 00:00:00 2001 From: Chylli Date: Tue, 22 Dec 2015 09:39:49 +0800 Subject: [PATCH 01/13] add dep --- package.json | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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", From 7e900cb1128a77e515929df6612f5d416cc9532b Mon Sep 17 00:00:00 2001 From: Chylli Date: Tue, 22 Dec 2015 09:49:42 +0800 Subject: [PATCH 02/13] add websocket worker --- lib/worker.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/worker.js b/lib/worker.js index 27f2d01..991e64a 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -29,6 +29,8 @@ switch (workerType) { case 'wamp': BenchmarkWorker = require('./workers/wampworker.js'); break; + case 'websocket': + BenchmarkWorker = require('./workers/websocket.js'); default: logger.error('error workerType ' + workerType); } From 0db415d1675dca6c0eab26f6745d322189036067 Mon Sep 17 00:00:00 2001 From: Chylli Date: Wed, 23 Dec 2015 14:44:19 +0800 Subject: [PATCH 03/13] add websocketworker --- lib/worker.js | 3 ++- lib/workers/websocketworker.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/workers/websocketworker.js diff --git a/lib/worker.js b/lib/worker.js index 991e64a..4e3433c 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -30,7 +30,8 @@ switch (workerType) { BenchmarkWorker = require('./workers/wampworker.js'); break; case 'websocket': - BenchmarkWorker = require('./workers/websocket.js'); + 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..5c5da02 --- /dev/null +++ b/lib/workers/websocketworker.js @@ -0,0 +1,38 @@ +/*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('ws://localhost:8080/'); + + client.onopen = function (){ + callback(false, client); + }; + client.onerror = function(err){ + if (self.verbose) { + logger.error("SocketIO Worker error: " + JSON.stringify(err)); + } + callback(true,client); + }; + + client.onmessage = function(e) { + if(self.verbose){ + logger.error("REceived: " + JSON.stringify(err)); + } + }; + //client.on('message') + + +} + +module.exports = WebsocketWorker; + From 40ac19e71cb3f7afbd177bdf454e378cfc7003d2 Mon Sep 17 00:00:00 2001 From: Chylli Date: Wed, 23 Dec 2015 15:08:30 +0800 Subject: [PATCH 04/13] fix bugs --- lib/workers/websocketworker.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/workers/websocketworker.js b/lib/workers/websocketworker.js index 5c5da02..ecd882d 100644 --- a/lib/workers/websocketworker.js +++ b/lib/workers/websocketworker.js @@ -12,21 +12,22 @@ util.inherits(WebsocketWorker, BaseWorker); WebsocketWorker.prototype.createClient = function (callback) { var self = this; - var client = new WebsocketClient('ws://localhost:8080/'); + var client = new WebsocketClient(this.server); client.onopen = function (){ callback(false, client); }; client.onerror = function(err){ if (self.verbose) { - logger.error("SocketIO Worker error: " + JSON.stringify(err)); + logger.error("Websocket Worker error: " + JSON.parse(err)); } callback(true,client); }; - client.onmessage = function(e) { + client.onmessage = function(msg) { if(self.verbose){ - logger.error("REceived: " + JSON.stringify(err)); + var data = JSON.parse(msg.data) + logger.error("Received: " + data); } }; //client.on('message') From 32833cd7cacf94f59702599dc41878ded29a32d3 Mon Sep 17 00:00:00 2001 From: Chylli Date: Wed, 23 Dec 2015 16:13:46 +0800 Subject: [PATCH 05/13] add generator.js --- generator.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 generator.js diff --git a/generator.js b/generator.js new file mode 100644 index 0000000..6089b63 --- /dev/null +++ b/generator.js @@ -0,0 +1,62 @@ +var logger = require('./lib/logger.js'); +module.exports = { + /** + * Before connection (optional, just for faye) + * @param {client} client connection + */ + beforeConnect : function(client) { + logger.error('call beforeconnect'); + // Example: + // client.setHeader('Authorization', 'OAuth abcd-1234'); + // client.disable('websocket'); + }, + + /** + * On client connection (required) + * @param {client} client connection + * @param {done} callback function(err) {} + */ + onConnect : function(client, done) { + logger.error('call onconnect'); + // Faye client + // client.subscribe('/channel', function(message) { }); + + // Socket.io client + // client.emit('test', { hello: 'world' }); + + // Primus client + // client.write('Sailing the seas of cheese'); + + // WAMP session + // client.subscribe('com.myapp.hello').then(function(args) { }); + + client.onmessage = function(msg) { + var data = JSON.parse(msg.data); + logger.error('ticks update: ' + data); + }; + client.send(JSON.stringify({ticks:'R_100'})); + + done(); + }, + + /** + * Send a message (required) + * @param {client} client connection + * @param {done} callback function(err) {} + */ + sendMessage : function(client, done) { + // Example: + // client.emit('test', { hello: 'world' }); + // client.publish('/test', { hello: 'world' }); + // client.call('com.myapp.add2', [2, 3]).then(function (res) { }); + logger.error('call sendmessage'); + done(); + }, + + /** + * WAMP connection options + */ + options : { + // realm: 'chat' + } +}; From 515b962f416ae5a2de15105e10e3f16e19689631 Mon Sep 17 00:00:00 2001 From: Chylli Date: Fri, 25 Dec 2015 12:19:54 +0800 Subject: [PATCH 06/13] remove useless lines --- lib/workers/websocketworker.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/workers/websocketworker.js b/lib/workers/websocketworker.js index ecd882d..1525502 100644 --- a/lib/workers/websocketworker.js +++ b/lib/workers/websocketworker.js @@ -23,16 +23,6 @@ WebsocketWorker.prototype.createClient = function (callback) { } callback(true,client); }; - - client.onmessage = function(msg) { - if(self.verbose){ - var data = JSON.parse(msg.data) - logger.error("Received: " + data); - } - }; - //client.on('message') - - } module.exports = WebsocketWorker; From 23fd8d6849af397d9b953b1830b1b65862757a5f Mon Sep 17 00:00:00 2001 From: Chylli Date: Fri, 25 Dec 2015 12:20:08 +0800 Subject: [PATCH 07/13] remove useles file --- generator.js | 62 ---------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 generator.js diff --git a/generator.js b/generator.js deleted file mode 100644 index 6089b63..0000000 --- a/generator.js +++ /dev/null @@ -1,62 +0,0 @@ -var logger = require('./lib/logger.js'); -module.exports = { - /** - * Before connection (optional, just for faye) - * @param {client} client connection - */ - beforeConnect : function(client) { - logger.error('call beforeconnect'); - // Example: - // client.setHeader('Authorization', 'OAuth abcd-1234'); - // client.disable('websocket'); - }, - - /** - * On client connection (required) - * @param {client} client connection - * @param {done} callback function(err) {} - */ - onConnect : function(client, done) { - logger.error('call onconnect'); - // Faye client - // client.subscribe('/channel', function(message) { }); - - // Socket.io client - // client.emit('test', { hello: 'world' }); - - // Primus client - // client.write('Sailing the seas of cheese'); - - // WAMP session - // client.subscribe('com.myapp.hello').then(function(args) { }); - - client.onmessage = function(msg) { - var data = JSON.parse(msg.data); - logger.error('ticks update: ' + data); - }; - client.send(JSON.stringify({ticks:'R_100'})); - - done(); - }, - - /** - * Send a message (required) - * @param {client} client connection - * @param {done} callback function(err) {} - */ - sendMessage : function(client, done) { - // Example: - // client.emit('test', { hello: 'world' }); - // client.publish('/test', { hello: 'world' }); - // client.call('com.myapp.add2', [2, 3]).then(function (res) { }); - logger.error('call sendmessage'); - done(); - }, - - /** - * WAMP connection options - */ - options : { - // realm: 'chat' - } -}; From 83003df7ce5fc9a9650352e2ef2baafeed5eac00 Mon Sep 17 00:00:00 2001 From: Chylli Date: Fri, 25 Dec 2015 12:25:39 +0800 Subject: [PATCH 08/13] add doc --- README.md | 3 ++- index.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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..14b4858 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ program .option('-g, --generator ', 'js file for generate message or special event') .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') From dc61035c7a3b959f80fba9a2f581513b7a389f00 Mon Sep 17 00:00:00 2001 From: Chylli Date: Tue, 12 Jan 2016 15:27:29 +0800 Subject: [PATCH 09/13] add reporter option --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index 14b4858..02e8fa0 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ 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, websocket). Default to io') @@ -56,6 +57,12 @@ if (!program.message) { program.message = 0; } +if (!program.reporter) { + program.reporter = __dirname + '/lib/defaultreporter.js'; +} +if (program.generator.indexOf('/') !== 0){ + program.generator = process.cwd() + '/' + program.generator; +} if (!program.type) { program.type = 'socket.io'; } @@ -71,6 +78,7 @@ logger.info('WS server : ' + program.type); var options = { generatorFile : program.generator, + reporterFile : program.repoter, type : program.type, transport : program.transport, keepAlive : program.keepAlive, From 5e6061e1a43e45ae87be4e6f5388a4fd3538e1b6 Mon Sep 17 00:00:00 2001 From: Chylli Date: Tue, 12 Jan 2016 15:47:54 +0800 Subject: [PATCH 10/13] support report --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 02e8fa0..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'); @@ -60,8 +59,8 @@ if (!program.message) { if (!program.reporter) { program.reporter = __dirname + '/lib/defaultreporter.js'; } -if (program.generator.indexOf('/') !== 0){ - program.generator = process.cwd() + '/' + program.generator; +if (program.reporter.indexOf('/') !== 0){ + program.reporter = process.cwd() + '/' + program.reporter; } if (!program.type) { program.type = 'socket.io'; @@ -78,7 +77,7 @@ logger.info('WS server : ' + program.type); var options = { generatorFile : program.generator, - reporterFile : program.repoter, + reporterFile : program.reporter, type : program.type, transport : program.transport, keepAlive : program.keepAlive, @@ -89,6 +88,8 @@ if (program.verbose) { logger.debug("Benchmark Options " + JSON.stringify(options)); } +var Reporter = require(program.reporter); + var outputStream = null; if (program.output) { @@ -98,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 From 97ec0364c4e817d24214055f41930008b24cdc96 Mon Sep 17 00:00:00 2001 From: Chylli Date: Tue, 12 Jan 2016 16:52:44 +0800 Subject: [PATCH 11/13] exit code --- lib/benchmark.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/benchmark.js b/lib/benchmark.js index d114f99..0a67270 100644 --- a/lib/benchmark.js +++ b/lib/benchmark.js @@ -153,7 +153,7 @@ Benchmark.prototype.terminate = function () { this.close(); } - this._report(); + process.exit(this._report()); }; /** @@ -162,7 +162,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); } }; From 6e9fc15dd5c3fc92520e2c830a74887042def201 Mon Sep 17 00:00:00 2001 From: Chylli Date: Wed, 13 Jan 2016 17:26:42 +0800 Subject: [PATCH 12/13] redefine disconnect --- lib/workers/websocketworker.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/workers/websocketworker.js b/lib/workers/websocketworker.js index 1525502..79a5bbe 100644 --- a/lib/workers/websocketworker.js +++ b/lib/workers/websocketworker.js @@ -25,5 +25,13 @@ WebsocketWorker.prototype.createClient = function (callback) { }; } +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; From 720acbdcb2080efaf6df6db3b24377bc4df22e3b Mon Sep 17 00:00:00 2001 From: Chylli Date: Thu, 14 Jan 2016 14:32:46 +0800 Subject: [PATCH 13/13] fix problem: flush outstream before exit --- lib/benchmark.js | 4 +++- lib/defaultreporter.js | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/benchmark.js b/lib/benchmark.js index 0a67270..c512234 100644 --- a/lib/benchmark.js +++ b/lib/benchmark.js @@ -153,7 +153,9 @@ Benchmark.prototype.terminate = function () { this.close(); } - process.exit(this._report()); + // process.exit will be called after report + // because we want to end the outputstream before exit + this._report(); }; /** 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;