From 1aae8c0c866a459b1e336e91d93382aec50181d9 Mon Sep 17 00:00:00 2001 From: 3rd-Eden Date: Sat, 6 Apr 2013 12:17:08 +0200 Subject: [PATCH] [metric] Output some metrics, do some stats stuff mek it pruddy --- bin/thor | 6 +++++- metrics.js | 31 +++++++++++++++++++++++++++++-- package.json | 6 ++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/bin/thor b/bin/thor index c38b680..7822880 100755 --- a/bin/thor +++ b/bin/thor @@ -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)); +}); diff --git a/metrics.js b/metrics.js index ea55b87..e94acc0 100644 --- a/metrics.js +++ b/metrics.js @@ -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 @@ -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 @@ -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; @@ -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() + }; }; // diff --git a/package.json b/package.json index e1b9023..0aa2fa2 100644 --- a/package.json +++ b/package.json @@ -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"