Add try catch for profiler decorator

This commit is contained in:
vakrilov
2017-05-13 13:27:13 +03:00
parent 5441e7d1fc
commit b47dafc01a
3 changed files with 100 additions and 38 deletions

View File

@@ -48,6 +48,13 @@ export declare function pause(name: string): TimerInfo;
*/
export declare function stop(name: string): TimerInfo;
/**
* Returns true if a timer is currently running.
* @param name Name of the timer.
* @returns true is the timer is currently running.
*/
export declare function isRunning(name: string): boolean;
/**
* Method decorator factory. It will intercept the method call and start and pause a timer before and after the method call.
* Works only if profiling is enabled.

View File

@@ -89,14 +89,19 @@ export function stop(name: string): TimerInfo {
return info;
}
export function isRunning(name: string): boolean {
const info = timers.get(name);
return !!(info && info.isRunning);
}
function pauseInternal(name: string): TimerInfo {
let info = timers.get(name);
const info = timers.get(name);
if (!info) {
throw new Error(`No timer started: ${name}`);
}
if (info.isRunning) {
info.lastTime = Math.round(time() - info.currentStart);
info.lastTime = time() - info.currentStart;
info.totalTime += info.lastTime;
info.count++;
info.currentStart = 0;
@@ -128,12 +133,11 @@ export function profile(name?: string): MethodDecorator {
//editing the descriptor/value parameter
descriptor.value = function () {
start(name);
var result = originalMethod.apply(this, arguments);
pause(name)
return result;
try {
return originalMethod.apply(this, arguments);
} finally {
pause(name);
}
};
// return edited descriptor as opposed to overwriting the descriptor