2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let Profiler;
|
|
|
|
const Settings = require('settings-sharelatex');
|
|
|
|
const logger = require('logger-sharelatex');
|
2017-05-18 06:00:07 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
const deltaMs = function(ta, tb) {
|
|
|
|
const nanoSeconds = ((ta[0]-tb[0])*1e9) + (ta[1]-tb[1]);
|
|
|
|
const milliSeconds = Math.floor(nanoSeconds*1e-6);
|
|
|
|
return milliSeconds;
|
|
|
|
};
|
2017-05-18 06:00:07 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
module.exports = (Profiler = (function() {
|
|
|
|
Profiler = class Profiler {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.LOG_CUTOFF_TIME = 1000;
|
|
|
|
}
|
2017-05-18 06:00:07 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
constructor(name, args) {
|
|
|
|
this.name = name;
|
|
|
|
this.args = args;
|
|
|
|
this.t0 = (this.t = process.hrtime());
|
|
|
|
this.start = new Date();
|
|
|
|
this.updateTimes = [];
|
|
|
|
}
|
2017-05-18 06:00:07 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
log(label) {
|
|
|
|
const t1 = process.hrtime();
|
|
|
|
const dtMilliSec = deltaMs(t1, this.t);
|
|
|
|
this.t = t1;
|
|
|
|
this.updateTimes.push([label, dtMilliSec]); // timings in ms
|
|
|
|
return this; // make it chainable
|
|
|
|
}
|
2017-05-18 06:00:07 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
end(message) {
|
|
|
|
const totalTime = deltaMs(this.t, this.t0);
|
|
|
|
if (totalTime > this.LOG_CUTOFF_TIME) { // log anything greater than cutoff
|
|
|
|
const args = {};
|
|
|
|
for (let k in this.args) {
|
|
|
|
const v = this.args[k];
|
|
|
|
args[k] = v;
|
|
|
|
}
|
|
|
|
args.updateTimes = this.updateTimes;
|
|
|
|
args.start = this.start;
|
|
|
|
args.end = new Date();
|
|
|
|
logger.log(args, this.name);
|
|
|
|
}
|
|
|
|
return totalTime;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Profiler.initClass();
|
|
|
|
return Profiler;
|
|
|
|
})());
|