-
Notifications
You must be signed in to change notification settings - Fork 56
/
timer.js
41 lines (36 loc) · 1.69 KB
/
timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var Meter = require('./meter');
Histogram = require('./histogram')
ExponentiallyDecayingSample = require('../stats/exponentially_decaying_sample');
/*
* Basically a timer tracks the rate of events and histograms the durations
*/
var Timer = module.exports = function Timer() {
this.meter = new Meter();
this.histogram = new Histogram(new ExponentiallyDecayingSample(1028, 0.015));
this.clear();
this.type = 'timer';
}
Timer.prototype.update = function(duration) {
this.histogram.update(duration);
this.meter.mark();
}
// delegate these to histogram
Timer.prototype.clear = function() { return this.histogram.clear(); }
Timer.prototype.count = function() { return this.histogram.count; }
Timer.prototype.min = function() { return this.histogram.min; }
Timer.prototype.max = function() { return this.histogram.max; }
Timer.prototype.mean = function() { return this.histogram.mean(); }
Timer.prototype.stdDev = function() { return this.histogram.stdDev(); }
Timer.prototype.percentiles = function(percentiles) { return this.histogram.percentiles(percentiles); }
Timer.prototype.values = function() { return this.histogram.values(); }
// delegate these to meter
Timer.prototype.oneMinuteRate = function() { return this.meter.oneMinuteRate(); }
Timer.prototype.fiveMinuteRate = function() { return this.meter.fiveMinuteRate(); }
Timer.prototype.fifteenMinuteRate = function() { return this.meter.fifteenMinuteRate(); }
Timer.prototype.meanRate = function() { return this.meter.meanRate(); }
Timer.prototype.tick = function() { this.meter.tick(); } // primarily for testing
Timer.prototype.printObj = function() {
return {type: 'timer'
, duration: this.histogram.printObj()
, rate: this.meter.printObj()};
}