Skip to content

Commit

Permalink
[metric] Output some metrics, do some stats stuff mek it pruddy
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Apr 6, 2013
1 parent 1517ffb commit 1aae8c0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 5 additions & 1 deletion bin/thor
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,8 @@ async.forEach(cli.args, function forEach(url, done) {
});

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

console.log(JSON.stringify(metrics.summary(), null, 2));
});
31 changes: 29 additions & 2 deletions metrics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
'use strict';

var Stats = require('fast-stats').Stats
, sugar = require('sugar');

/**
* Metrics collection and generation.
*
* @constructor
* @param {Number} requests The total amount of requests scheduled to be send
*/
function Metrics(requests) {
this.requests = requests; // The total amount of requests send

Expand All @@ -10,8 +19,8 @@ function Metrics(requests) {
this.errors = Object.create(null); // Collection of different errors
this.timing = Object.create(null); // Different timings

this.latency = []; // Latencies of the echo'd messages
this.handshaking = []; // Handshake duration
this.latency = new Stats(); // Latencies of the echo'd messages
this.handshaking = new Stats(); // Handshake duration

this.read = 0; // Bytes read
this.send = 0; // Bytes send
Expand All @@ -33,6 +42,8 @@ Metrics.prototype.start = function start() {
* @api public
*/
Metrics.prototype.stop = function stop() {
if (this.timing.stop) return this;

this.timing.stop = Date.now();
this.timing.duration = this.timing.stop - this.timing.start;
return this;
Expand Down Expand Up @@ -106,9 +117,25 @@ Metrics.prototype.close = function close(data) {
/**
* Generate a summary of the metrics.
*
* @returns {Object} The summary
* @api public
*/
Metrics.prototype.summary = function summary() {
var errorRate = 0;

// Calculate the total amount of errors
Object.keys(this.errors).forEach(function forEach(err) {
errorRate = errorRate + this.errors[err];
}, this);

return {
'Total received': this.read.bytes(),
'Total transfered': this.send.bytes(),
'Time taken for tests': this.timing.duration.duration(),
'Connections created': this.connections,
'Handshake duration (median)': this.handshaking.median().duration(),
'Message latency (median)': this.latency.median().duration()
};
};

//
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
"license": "MIT",
"dependencies": {
"commander": "1.1.x",
"ws": "0.4.x",
"async": "~0.2.6"
"async": "0.2.x",
"ws": "git://github.com/3rd-Eden/ws.git",
"sugar": "git://github.com/bluesmoon/node-faststats.git",
"tab": "0.1.x"
},
"bin": {
"thor": "./bin/thor"
Expand Down

0 comments on commit 1aae8c0

Please sign in to comment.