Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#23 Option to pass custom headers #25

Closed
wants to merge 1 commit into from
Closed
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
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
45 changes: 38 additions & 7 deletions bin/thor
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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('-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)
.option('-A, --amount <connections>', 'the amount of persistent connections to generate', parseInteger, 10000)
.option('-C, --concurrent <connections>', 'how many concurrent-connections per second', parseInteger, 0)
.option('-M, --messages <messages>', 'messages to be send per connection', parseInteger, 1)
.option('-H, --header [header]', 'a header to pass to each connection', collect, [])
.option('-P, --protocol <protocol>', 'WebSocket protocol version', parseInteger, 13)
.option('-B, --buffer <size>', 'size of the messages that are send', parseInteger, 1024)
.option('-W, --workers <cpus>', 'workers to be spawned', parseInteger, os.cpus().length)
.option('-G, --generator <file>', 'custom message generators')
.option('-M, --masked', 'send the messaged with a mask')
.option('-b, --binary', 'send binary messages instead of utf-8')
Expand Down Expand Up @@ -47,6 +62,21 @@ var cluster = require('cluster')
, received = 0
, robin = [];

//
// Process headers
//
var headers = {};
cli.header.forEach(function(header){
var keyValue = header.split(':');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header Val might have ':' therefore the split will be wrong. Try:
var keyValue = header.split(/:/);
var key = keyValue.shift();
var value = keyValue.join(':');

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
Expand All @@ -56,7 +86,8 @@ cluster.setupMaster({
: path.resolve(__dirname, '../generator.js'),
cli.protocol,
!!cli.masked,
!!cli.binary
!!cli.binary,
JSON.stringify(headers)
]
});

Expand Down
8 changes: 5 additions & 3 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 = JSON.parse(process.argv[6]);

process.on('message', function message(task) {
var now = Date.now();
Expand All @@ -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() {
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