Skip to content

Commit

Permalink
[minor] Adding a messages generator so you can supply it with your me…
Browse files Browse the repository at this point in the history
…ssage file
  • Loading branch information
3rd-Eden committed Apr 5, 2013
1 parent 36338b0 commit 36862cc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
25 changes: 19 additions & 6 deletions bin/thor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

var async = require('async')
, path = require('path')
, os = require('os');

//
Expand All @@ -15,18 +16,17 @@ cli.usage('[options] ws://localhost')
.option('-M, --messages <messages>', 'messages to be send per connection', parseInt, 0)
.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('-G, --generator <file>', 'custom message generators')
.version(require('../package.json').version)
.parse(process.argv);

//
// Check if all required arguments are supplied, if we don't have a valid url we
// should bail out
//
var url = cli.args[0];

if (!url) return [
if (!cli.args.length) return [
'Thor:'
, ''
, 'Odin is disapointed you... pithy human! You forgot to supply the urls.'
].forEach(function stderr(line) {
console.error(line);
});
Expand All @@ -38,7 +38,14 @@ var cluster = require('cluster')
, workers = cli.workers || 1
, robin = [];

cluster.setupMaster({ exec: '../thunderbolt.js', silent: true });
cluster.setupMaster({
exec: '../thunderbolt.js'
, silent: true
, args: cli.generator
? path.resolve(process.cwd(), cli.generator)
: path.resolve(__dirname, '../session.js')
});

while (workers--) cluster.fork();

Object.keys(cluster.workers).forEach(function each(id) {
Expand Down Expand Up @@ -73,14 +80,20 @@ var tick = cli.concurrent > 1000 ? 1 : 1000 / cli.concurrent
, ''
, 'Thou shall:'
, '- Spawn '+ cli.workers +' workers.'
, '- Create '+ cli.concurrent + ' concurrent connections.'
, '- Create '+ cli.concurrent + ' concurrent/parallel connections.'
, '- Smash '+ (cli.amount || 'infinite') +' connections with the mighty Mjölnir.'
, ''
, 'The answers you seek shall be yours, once I claim what is mine.'
].forEach(function stdout(line) {
console.log(line);
});

// Iterate over all the urls so we can target multiple locations at once, which
// is helpfull if you are testing multiple loadbalancer endpoints for example.
cli.args.forEach(function (url) {

});

process.once('SIGINT', process.exit.bind(process, 0));
process.once('exit', function summary() {

Expand Down
46 changes: 46 additions & 0 deletions session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

/**
* Generate a UTF-8 messages that we will be send to a connected client.
*
* @async
* @param {Number} size The specified in bytes for the message.
* @param {Function} fn The callback function for the data.
* @public
*/
exports.utf8 = function utf(size, fn) {
var key = 'utf8::'+ size
, cached = cache[key];

// We have a cached version of this size, return that instead.
if (cached) return fn(undefined, cached);

cached = cache[key] = new Buffer(size).toString('utf-8');
fn(undefined, cached);
};

/**
* Generate a binary message that we will be send to a connected client.
*
* @async
* @param {Number} size The specified in bytes for the message.
* @param {Function} fn The callback function for the data.
* @public
*/
exports.binary = function binary(size, fn) {
var key = 'binary::'+ size
, cached = cache[key];

// We have a cached version of this size, return that instead.
if (cached) return fn(undefined, cached);

cached = cache[key] = new Buffer(size);
fn(undefined, cached);
};

//
// The following is not needed to create a session file. We don't want to
// re-create & re-allocate memory every time we receive a message so we cache
// them in a variable.
//
var cache = Object.create(null);
5 changes: 5 additions & 0 deletions thunderbolt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
var Socket = require('ws')
, collection = [];

//
// Get the session document that is used to generate the data.
//
var session = require(process.argv[1]);

process.on('message', function message(task) {
var now = Date.now();

Expand Down

0 comments on commit 36862cc

Please sign in to comment.