Permalink
Browse files

Merge pull request #277 from dynmeth/master

Added median calculation to timer_data
  • Loading branch information...
mrtazz committed Mar 23, 2013
2 parents a11359d + 76c13f0 commit 8bc1bb3f5d9c642b86ade0a73d41edb8472af691
Showing with 16 additions and 2 deletions.
  1. +4 −0 lib/process_metrics.js
  2. +12 −2 test/process_metrics_tests.js
View
@@ -64,6 +64,9 @@ var process_metrics = function (metrics, flushInterval, ts, flushCallback) {
sumOfDiffs += (values[i] - mean) * (values[i] - mean);
}
+ var mid = Math.floor(count/2);
+ var median = (count % 2) ? values[mid] : (values[mid-1] + values[mid])/2;
+
var stddev = Math.sqrt(sumOfDiffs / count);
current_timer_data["std"] = stddev;
current_timer_data["upper"] = max;
@@ -72,6 +75,7 @@ var process_metrics = function (metrics, flushInterval, ts, flushCallback) {
current_timer_data["count_ps"] = timer_counters[key] / (flushInterval / 1000);
current_timer_data["sum"] = sum;
current_timer_data["mean"] = mean;
+ current_timer_data["median"] = median;
// note: values bigger than the upper limit of the last bin are ignored, by design
conf = histogram || [];
@@ -46,7 +46,7 @@ module.exports = {
test.done();
},
timers_single_time: function(test) {
- test.expect(7);
+ test.expect(8);
this.metrics.timers['a'] = [100];
this.metrics.timer_counters['a'] = 1;
pm.process_metrics(this.metrics, 100, this.time_stamp, function(){});
@@ -58,10 +58,11 @@ module.exports = {
test.equal(10, timer_data.count_ps);
test.equal(100, timer_data.sum);
test.equal(100, timer_data.mean);
+ test.equal(100, timer_data.median);
test.done();
},
timers_multiple_times: function(test) {
- test.expect(7);
+ test.expect(8);
this.metrics.timers['a'] = [100, 200, 300];
this.metrics.timer_counters['a'] = 3;
pm.process_metrics(this.metrics, 100, this.time_stamp, function(){});
@@ -73,6 +74,7 @@ module.exports = {
test.equal(30, timer_data.count_ps);
test.equal(600, timer_data.sum);
test.equal(200, timer_data.mean);
+ test.equal(200, timer_data.median);
test.done();
},
timers_single_time_single_percentile: function(test) {
@@ -190,5 +192,13 @@ module.exports = {
statsd_metrics = this.metrics.statsd_metrics;
test.notEqual(undefined, statsd_metrics["processing_time"]);
test.done();
+ },
+ timers_multiple_times_even: function(test) {
+ test.expect(1);
+ this.metrics.timers['a'] = [300, 200, 400, 100];
+ pm.process_metrics(this.metrics, 100, this.time_stamp, function(){});
+ timer_data = this.metrics.timer_data['a'];
+ test.equal(250, timer_data.median);
+ test.done();
}
}

0 comments on commit 8bc1bb3

Please sign in to comment.