Windshaft-cartodb/lib/stats/timer.js

35 lines
702 B
JavaScript
Raw Normal View History

'use strict';
2019-10-22 01:07:24 +08:00
function Timer () {
2016-03-19 00:21:43 +08:00
this.times = {};
}
module.exports = Timer;
2019-10-22 01:07:24 +08:00
Timer.prototype.start = function (label) {
2016-03-19 00:21:43 +08:00
this.timeIt(label, 'start');
};
2019-10-22 01:07:24 +08:00
Timer.prototype.end = function (label) {
2016-03-19 00:21:43 +08:00
this.timeIt(label, 'end');
};
2019-10-22 01:07:24 +08:00
Timer.prototype.timeIt = function (label, what) {
2016-03-19 00:21:43 +08:00
this.times[label] = this.times[label] || {};
this.times[label][what] = Date.now();
};
2019-10-22 01:07:24 +08:00
Timer.prototype.getTimes = function () {
2016-03-19 00:21:43 +08:00
var self = this;
var times = {};
2019-10-22 01:07:24 +08:00
Object.keys(this.times).forEach(function (label) {
2016-03-19 00:21:43 +08:00
var stat = self.times[label];
if (stat.start && stat.end) {
times[label] = Math.max(0, stat.end - stat.start);
}
});
return times;
};