probe.gl
Version:
JavaScript Console Instrumentation and Benchmarking for Browser and Node
207 lines (179 loc) • 5.11 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _timestamp = require("./utils/timestamp");
var _formatters = require("./utils/formatters");
var Stats = function () {
function Stats(_ref) {
var id = _ref.id;
(0, _classCallCheck2.default)(this, Stats);
this.id = id;
this.time = (0, _timestamp.getTimestamp)();
this.counters = {};
Object.seal(this);
}
(0, _createClass2.default)(Stats, [{
key: "addCounter",
value: function addCounter(name) {
this._getCounter(name);
return this;
}
}, {
key: "bump",
value: function bump(name) {
var counter = this._getCounter(name);
counter.call++;
counter.count++;
return this;
}
}, {
key: "increment",
value: function increment(name, count) {
var counter = this._getCounter(name);
counter.call++;
counter.count += count;
return this;
}
}, {
key: "addTimer",
value: function addTimer(name) {
var timer = this._getCounter(name);
timer.time = 0;
return this;
}
}, {
key: "addTime",
value: function addTime(name, time) {
var timer = this._getCounter(name);
timer.time += time;
timer.count++;
return this;
}
}, {
key: "timeStart",
value: function timeStart(name, subname) {
var timer = this._getCounter(name);
timer._startTime = (0, _timestamp.getTimestamp)();
}
}, {
key: "timeEnd",
value: function timeEnd(name, subname) {
var timer = this._getCounter(name);
this.addTime(name, (0, _timestamp.getTimestamp)() - timer._startTime);
}
}, {
key: "reset",
value: function reset() {
this.time = (0, _timestamp.getTimestamp)();
for (var key in this.counters) {
var counter = this.counters[key];
counter.count = 0;
counter.time = 0;
}
return this;
}
}, {
key: "hasTimeElapsed",
value: function hasTimeElapsed() {
var deltaTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;
return (0, _timestamp.getTimestamp)() - this.time > 1000;
}
}, {
key: "getStats",
value: function getStats() {
var deltaTime = ((0, _timestamp.getTimestamp)() - this.time) / 1000;
var stats = {};
for (var key in this.counters) {
var counter = this.counters[key];
stats[counter.title] = {
total: counter.count,
fps: Math.round(counter.count / deltaTime)
};
if (counter.time) {
stats[counter.title].totalTime = (0, _formatters.formatTime)(counter.time);
stats[counter.title].avgTime = (0, _formatters.formatTime)(counter.time / counter.count);
}
}
return stats;
}
}, {
key: "getStatsTable",
value: function getStatsTable() {
var stats = this.getStats();
for (var key in stats) {
if (stats[key].total === 0) {
delete stats[key];
}
}
return stats;
}
}, {
key: "getStatNames",
value: function getStatNames() {
return Object.keys(this.counters);
}
}, {
key: "get",
value: function get(name) {
var counter = this._getCounter(name);
return counter.count;
}
}, {
key: "getCount",
value: function getCount(name) {
var counter = this._getCounter(name);
return counter.count;
}
}, {
key: "getFPS",
value: function getFPS(name) {
var counter = this._getCounter(name);
var deltaTime = ((0, _timestamp.getTimestamp)() - this.time) / 1000;
return Math.round(counter.count / deltaTime);
}
}, {
key: "getTimeString",
value: function getTimeString() {
return "".concat(this.id, ":").concat((0, _formatters.formatTime)(this.time), "(").concat(this.count, ")");
}
}, {
key: "oneSecondPassed",
value: function oneSecondPassed() {
var deltaTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;
return this.hasTimeElapsed(deltaTime);
}
}, {
key: "_getCounter",
value: function _getCounter(name) {
var counter = this.counters[name];
if (!counter) {
counter = {
title: name,
unit: '',
timer: false,
count: 0,
time: 0,
totalTime: 0,
averageTime: 0
};
this.counters[name] = counter;
}
return counter;
}
}, {
key: "_incrementTimer",
value: function _incrementTimer(counter, time, count) {
counter.count += count;
counter.totalTime += time;
counter.averageTime = counter.totalTime / count;
}
}]);
return Stats;
}();
exports.default = Stats;
//# sourceMappingURL=stats.js.map