-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperfCounter.js
More file actions
89 lines (73 loc) · 2.11 KB
/
perfCounter.js
File metadata and controls
89 lines (73 loc) · 2.11 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var metrics = require('metrics');
var startPushTask = require('./pushTask').startPushTask;
var PerfCounter = function () {
this.tags = {};
this.counters = {};
this.meters = {};
this.hists = {};
this.timers = {};
};
PerfCounter.prototype.addTags = function (name ,tags) {
var t = this.tags[name] = this.tags[name] || {};
for (var k in tags) {
if (tags.hasOwnProperty(k)) {
t[k] = tags[k];
}
}
};
PerfCounter.prototype.getTag = function (name) {
return this.tags[name] || {};
};
PerfCounter.prototype.getCounter = function (name) {
return this.counters[name] = this.counters[name] || new metrics.Counter();
};
PerfCounter.prototype.incCounter = function (name, n) {
this.getCounter(name).inc(n);
};
PerfCounter.prototype.getMeter = function (name) {
return this.meters[name] = this.meters[name] || new metrics.Meter();
};
PerfCounter.prototype.markMeter = function (name, n) {
this.getMeter(name).mark(n);
};
PerfCounter.prototype.getHistogram = function (name) {
return this.hists[name] = this.hists[name] || new metrics.Histogram();
};
PerfCounter.prototype.updateHistogram = function (name, n) {
this.getHistogram(name).update(n);
};
PerfCounter.prototype.getTimer = function (name) {
return this.timers[name] = this.timers[name] || new metrics.Timer();
};
PerfCounter.prototype.updateTimer = function (name, n) {
this.getTimer(name).update(n);
};
PerfCounter.prototype.count = function (name, count) {
this.markMeter(name, count);
};
PerfCounter.prototype.duration = function (name, duration) {
this.updateTimer(name, duration);
};
PerfCounter.prototype.printObj = function () {
var metricObj = {};
var types = ['counters', 'meters', 'hists', 'timers'];
for (var i in types) {
var type = types[i];
metricObj[type] = {};
for (var name in this[type]) {
metricObj[type][name] = this[type][name].printObj();
}
}
return metricObj;
};
var getInstance = function () {
var instance;
return function() {
if (!instance) {
instance = new PerfCounter();
startPushTask(instance);
}
return instance;
}
}();
exports.getInstance = getInstance;