Skip to content

Commit

Permalink
observing#23 Option to pass custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
reda-alaoui committed Jan 8, 2016
1 parent 6bfac2e commit 48edc1e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Google it.
-A, --amount <connections> the amount of persistent connections to generate
-C, --concurrent <connections> how many concurrent-connections per second
-M, --messages <messages> messages to be send per connection
-H, --header [header] a header to pass to each connection
-P, --protocol <protocol> WebSocket protocol version
-B, --buffer <size> size of the messages that are send
-W, --workers <cpus> workers to be spawned
Expand Down
28 changes: 27 additions & 1 deletion bin/thor
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ var Metrics = require('../metrics')
, path = require('path')
, os = require('os');

function collect(val, memo) {
if(!memo){
memo = [];
}
memo.push(val);
return memo;
}

//
// Setup the Command-Line Interface.
//
Expand All @@ -16,6 +24,7 @@ cli.usage('[options] ws://localhost')
.option('-A, --amount <connections>', 'the amount of persistent connections to generate', parseInt, 10000)
.option('-C, --concurrent <connections>', 'how many concurrent-connections per second', parseInt, 0)
.option('-M, --messages <messages>', 'messages to be send per connection', parseInt, 1)
.option('-H, --header [header]', 'a header to pass to each connection', collect, [])
.option('-P, --protocol <protocol>', 'WebSocket protocol version', parseInt, 13)
.option('-B, --buffer <size>', 'size of the messages that are send', parseInt, 1024)
.option('-W, --workers <cpus>', 'workers to be spawned', parseInt, os.cpus().length)
Expand Down Expand Up @@ -47,6 +56,22 @@ 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();
});
console.log(headers);

cluster.setupMaster({
exec: path.resolve(__dirname, '../mjolnir.js')
, silent: false
Expand All @@ -56,7 +81,8 @@ cluster.setupMaster({
: path.resolve(__dirname, '../generator.js'),
cli.protocol,
!!cli.masked,
!!cli.binary
!!cli.binary,
headers
]
});

Expand Down
6 changes: 4 additions & 2 deletions mjolnir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = process.argv[6];

process.on('message', function message(task) {
var now = Date.now();
Expand All @@ -41,7 +42,8 @@ process.on('message', function message(task) {
if (!task.url) return;

var socket = new Socket(task.url, {
protocolVersion: protocol
protocolVersion: protocol,
headers: headers
});

socket.on('open', function open() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"author": "Arnout Kazemier <[email protected]>",
"license": "MIT",
"dependencies": {
"commander": "1.1.x",
"commander": "2.9.0",
"async": "0.2.x",
"tab": "0.1.x",
"colors": "0.6.x",
Expand Down

0 comments on commit 48edc1e

Please sign in to comment.