diff --git a/bin/thor b/bin/thor index ff8b17a..19c0b21 100755 --- a/bin/thor +++ b/bin/thor @@ -28,4 +28,35 @@ var cluster = require('cluster') cluster.setupMaster({ exec: '../thunderbolt.js', silent: true }); while (workers--) cluster.fork(); +Object.keys(cluster.workers).forEach(function each(id) { + var worker = cluster.workers[id]; + worker.on('message', function message(data) { + switch (data.type) { + case 'open': + case 'close': + case 'error': + case 'message': + } + }); +}); + +// +// Start. +// +var url = (cli.secure ? 'wss' : 'ws') +'://'+ cli.host +':'+ cli.port; +[ + '' + , 'Thor: version: '+ cli._version + , '' + , 'God of Thunder, son of Odin and smasher of WebSockets!' + , '' + , 'Thou shall:' + , '- Spawn '+ cli.workers +' workers.' + , '- Create '+ cli.concurrent + ' concurrent connections.' + , '- Smash '+ cli.amount +' connections with the mighty Mjölnir.' + , '' + , 'Prepare to suffer, '+ url + ' thy infidel.' +].forEach(function stdout(line) { + console.log(line); +}); diff --git a/thunderbolt.js b/thunderbolt.js index 456431f..806707b 100644 --- a/thunderbolt.js +++ b/thunderbolt.js @@ -4,19 +4,44 @@ var Socket = require('ws') , collection = []; process.on('message', function message(task) { + var now = Date.now(); + // // Write a new message to the socket. The message should have a size of x // if ('write' in task) collection.forEach(function write(socket) { - var start = Date.now(); + var start = socket.last = now; socket.send(task.message, function sending(err) { - var duration = Date.now() - start; - + if (err) process.send({ type: 'error', message: err.message }); }); }); if (task.shutdown) collection.forEach(function shutdown(socket) { socket.close(); }); + + // End of the line, we are gonna start generating new connections. + if (!task.url) return; + + var socket = new Socket(task.url); + + socket.on('open', function open() { + process.send({ type: 'open', duration: Date.now() - now }); + }); + + socket.on('message', function message(data) { + process.send({ type: 'message', latency: Date.now() - socket.last }); + }); + + socket.on('close', function close() { + process.send({ type: 'close' }); + }); + + socket.on('error', function error(err) { + process.send({ type: 'error', message: err.message }); + }); + + // Adding a new socket to our socket collection. + collection.push(socket); });