Add resetProfiles method

This commit is contained in:
vakrilov
2017-05-16 17:40:07 +03:00
parent d4e4841b59
commit f23c6c6455
2 changed files with 27 additions and 8 deletions

View File

@ -67,6 +67,11 @@ export declare function profile(name?: string): MethodDecorator;
*/
export declare function dumpProfiles(): void;
/**
* Resets the timers for all methods instrumented with profile decorator.
*/
export function resetProfiles(): void;
/**
* Starts android cpu profiling.
* @param name Name of the cpu profiling session.

View File

@ -12,11 +12,11 @@ interface TimerInfo extends TimerInfoDefinition {
}
// Use object instead of map as it is a bit faster
const timers: { [ index: string ]: TimerInfo } = {};
const timers: { [index: string]: TimerInfo } = {};
const anyGlobal = <any>global;
const profileNames: string[] = [];
let ENABLED = true;
let ENABLED = false;
let nativeTimeFunc: () => number;
export function enable() {
@ -51,7 +51,7 @@ export function start(name: string): void {
return;
}
let info = timers[ name ];
let info = timers[name];
if (info) {
if (info.isRunning) {
@ -66,7 +66,7 @@ export function start(name: string): void {
currentStart: time(),
isRunning: true
};
timers[ name ] = info;
timers[name] = info;
}
}
@ -88,17 +88,17 @@ export function stop(name: string): TimerInfo {
let info = pauseInternal(name);
console.log(`---- [${name}] STOP total: ${info.totalTime} count:${info.count}`);
timers[ name ] = undefined;
timers[name] = undefined;
return info;
}
export function isRunning(name: string): boolean {
const info = timers[ name ];
const info = timers[name];
return !!(info && info.isRunning);
}
function pauseInternal(name: string): TimerInfo {
const info = timers[ name ];
const info = timers[name];
if (!info) {
throw new Error(`No timer started: ${name}`);
@ -156,7 +156,7 @@ export function profile(name?: string): MethodDecorator {
export function dumpProfiles(): void {
profileNames.forEach(function (name) {
const info = timers[ name ];
const info = timers[name];
if (info) {
console.log("---- [" + name + "] STOP total: " + info.totalTime + " count:" + info.count);
@ -167,6 +167,20 @@ export function dumpProfiles(): void {
});
}
export function resetProfiles(): void {
profileNames.forEach(function (name) {
const info = timers[name];
if (info) {
if (!info.isRunning) {
timers[name] = undefined;
} else {
console.log("---- timer with name [" + name + "] is currently running and won't be reset");
}
}
});
}
export function startCPUProfile(name: string) {
if (!ENABLED) {
return;