diff --git a/README.md b/README.md index 6b97fe5..2a8baa5 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Google it. -A, --amount the amount of persistent connections to generate -C, --concurrent how many concurrent-connections per second -M, --messages messages to be send per connection + -H, --header [header] a header to pass to each connection -P, --protocol WebSocket protocol version -B, --buffer size of the messages that are send -W, --workers workers to be spawned diff --git a/bin/thor b/bin/thor index 9db08f2..31e4e7e 100755 --- a/bin/thor +++ b/bin/thor @@ -7,18 +7,33 @@ var Metrics = require('../metrics') , path = require('path') , os = require('os'); +function collect(val, memo) { + if(!memo){ + memo = []; + } + memo.push(val); + return memo; +} + +function parseInteger(val){ + if(val){ + return parseInt(val); + } +} + // // Setup the Command-Line Interface. // var cli = require('commander'); cli.usage('[options] ws://localhost') - .option('-A, --amount ', 'the amount of persistent connections to generate', parseInt, 10000) - .option('-C, --concurrent ', 'how many concurrent-connections per second', parseInt, 0) - .option('-M, --messages ', 'messages to be send per connection', parseInt, 1) - .option('-P, --protocol ', 'WebSocket protocol version', parseInt, 13) - .option('-B, --buffer ', 'size of the messages that are send', parseInt, 1024) - .option('-W, --workers ', 'workers to be spawned', parseInt, os.cpus().length) + .option('-A, --amount ', 'the amount of persistent connections to generate', parseInteger, 10000) + .option('-C, --concurrent ', 'how many concurrent-connections per second', parseInteger, 0) + .option('-M, --messages ', 'messages to be send per connection', parseInteger, 1) + .option('-H, --header [header]', 'a header to pass to each connection', collect, []) + .option('-P, --protocol ', 'WebSocket protocol version', parseInteger, 13) + .option('-B, --buffer ', 'size of the messages that are send', parseInteger, 1024) + .option('-W, --workers ', 'workers to be spawned', parseInteger, os.cpus().length) .option('-G, --generator ', 'custom message generators') .option('-M, --masked', 'send the messaged with a mask') .option('-b, --binary', 'send binary messages instead of utf-8') @@ -47,6 +62,21 @@ var cluster = require('cluster') , received = 0 , robin = []; +// +// Process headers +// +var headers = {}; +cli.header.forEach(function(header){ + var keyValue = header.split(':'); + var key = keyValue[0]; + var value = keyValue[1]; + if(!key || !value){ + console.warn('Header \'' + + '\' is wrongly defined. A header should looks like \'key:value\'.'); + return; + } + headers[key.trim()] = value.trim(); +}); + cluster.setupMaster({ exec: path.resolve(__dirname, '../mjolnir.js') , silent: false @@ -56,7 +86,8 @@ cluster.setupMaster({ : path.resolve(__dirname, '../generator.js'), cli.protocol, !!cli.masked, - !!cli.binary + !!cli.binary, + JSON.stringify(headers) ] }); diff --git a/mjolnir.js b/mjolnir.js index c22f517..1434948 100644 --- a/mjolnir.js +++ b/mjolnir.js @@ -14,7 +14,8 @@ var session = require(process.argv[2]); // var masked = process.argv[4] === 'true' , binary = process.argv[5] === 'true' - , protocol = +process.argv[3] || 13; + , protocol = +process.argv[3] || 13 + , headers = JSON.parse(process.argv[6]); process.on('message', function message(task) { var now = Date.now(); @@ -40,8 +41,9 @@ process.on('message', function message(task) { // End of the line, we are gonna start generating new connections. if (!task.url) return; - var socket = new Socket(task.url, { - protocolVersion: protocol + var socket = new Socket(task.url, [], { + protocolVersion: protocol, + headers: headers }); socket.on('open', function open() { diff --git a/package.json b/package.json index 48c2b49..205468b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "author": "Arnout Kazemier ", "license": "MIT", "dependencies": { - "commander": "1.1.x", + "commander": "2.9.0", "async": "0.2.x", "tab": "0.1.x", "colors": "0.6.x",