2014-04-01 07:33:36 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/performance.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: global
|
|
|
|
|
|
|
|
Performance measurement.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function Performance(enabled) {
|
|
|
|
this.enabled = !!enabled;
|
|
|
|
this.measures = {}; // Hashmap of current values of measurements
|
|
|
|
this.logger = new $tw.utils.Logger("performance");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wrap performance reporting around a top level function
|
|
|
|
*/
|
|
|
|
Performance.prototype.report = function(name,fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.enabled) {
|
|
|
|
return function() {
|
|
|
|
self.measures = {};
|
|
|
|
var startTime = $tw.utils.timer(),
|
|
|
|
result = fn.apply(this,arguments);
|
2015-10-14 09:59:46 +00:00
|
|
|
self.logger.log(name + ": " + $tw.utils.timer(startTime).toFixed(2) + "ms");
|
2014-04-01 07:33:36 +00:00
|
|
|
for(var m in self.measures) {
|
2015-10-14 09:59:46 +00:00
|
|
|
self.logger.log("+" + m + ": " + self.measures[m].toFixed(2) + "ms");
|
2014-04-01 07:33:36 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wrap performance measurements around a subfunction
|
|
|
|
*/
|
|
|
|
Performance.prototype.measure = function(name,fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.enabled) {
|
|
|
|
return function() {
|
|
|
|
var startTime = $tw.utils.timer(),
|
|
|
|
result = fn.apply(this,arguments),
|
|
|
|
value = self.measures[name] || 0;
|
|
|
|
self.measures[name] = value + $tw.utils.timer(startTime);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.Performance = Performance;
|
|
|
|
|
|
|
|
})();
|