Minetrack/lib/profiler.js
2015-11-03 01:41:27 -06:00

31 lines
654 B
JavaScript

var logger = require('./logger');
var timestamps = {};
function getCurrentTimeMs() {
return (new Date).getTime();
};
exports.track = function(name) {
if (timestamps[name]) {
throw new Error(name + ' is already being profiled!');
}
timestamps[name] = getCurrentTimeMs();
};
exports.untrack = function(name) {
if (!timestamps[name]) {
throw new Error(name + ' isn\'t being profiled!');
}
var timestamp = getCurrentTimeMs() - timestamps[name];
delete timestamps[name];
logger.log('debug', name + ' took ' + timestamp + 'ms');
return timestamp;
};
exports.getCurrentTimeMs = getCurrentTimeMs;