Skip to content

Commit f23c6c6

Browse files
author
vakrilov
committed
Add resetProfiles method
1 parent d4e4841 commit f23c6c6

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

tns-core-modules/profiling/profiling.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export declare function profile(name?: string): MethodDecorator;
6767
*/
6868
export declare function dumpProfiles(): void;
6969

70+
/**
71+
* Resets the timers for all methods instrumented with profile decorator.
72+
*/
73+
export function resetProfiles(): void;
74+
7075
/**
7176
* Starts android cpu profiling.
7277
* @param name Name of the cpu profiling session.

tns-core-modules/profiling/profiling.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface TimerInfo extends TimerInfoDefinition {
1212
}
1313

1414
// Use object instead of map as it is a bit faster
15-
const timers: { [ index: string ]: TimerInfo } = {};
15+
const timers: { [index: string]: TimerInfo } = {};
1616
const anyGlobal = <any>global;
1717
const profileNames: string[] = [];
1818

19-
let ENABLED = true;
19+
let ENABLED = false;
2020
let nativeTimeFunc: () => number;
2121

2222
export function enable() {
@@ -51,7 +51,7 @@ export function start(name: string): void {
5151
return;
5252
}
5353

54-
let info = timers[ name ];
54+
let info = timers[name];
5555

5656
if (info) {
5757
if (info.isRunning) {
@@ -66,7 +66,7 @@ export function start(name: string): void {
6666
currentStart: time(),
6767
isRunning: true
6868
};
69-
timers[ name ] = info;
69+
timers[name] = info;
7070
}
7171
}
7272

@@ -88,17 +88,17 @@ export function stop(name: string): TimerInfo {
8888
let info = pauseInternal(name);
8989
console.log(`---- [${name}] STOP total: ${info.totalTime} count:${info.count}`);
9090

91-
timers[ name ] = undefined;
91+
timers[name] = undefined;
9292
return info;
9393
}
9494

9595
export function isRunning(name: string): boolean {
96-
const info = timers[ name ];
96+
const info = timers[name];
9797
return !!(info && info.isRunning);
9898
}
9999

100100
function pauseInternal(name: string): TimerInfo {
101-
const info = timers[ name ];
101+
const info = timers[name];
102102

103103
if (!info) {
104104
throw new Error(`No timer started: ${name}`);
@@ -156,7 +156,7 @@ export function profile(name?: string): MethodDecorator {
156156

157157
export function dumpProfiles(): void {
158158
profileNames.forEach(function (name) {
159-
const info = timers[ name ];
159+
const info = timers[name];
160160

161161
if (info) {
162162
console.log("---- [" + name + "] STOP total: " + info.totalTime + " count:" + info.count);
@@ -167,6 +167,20 @@ export function dumpProfiles(): void {
167167
});
168168
}
169169

170+
export function resetProfiles(): void {
171+
profileNames.forEach(function (name) {
172+
const info = timers[name];
173+
174+
if (info) {
175+
if (!info.isRunning) {
176+
timers[name] = undefined;
177+
} else {
178+
console.log("---- timer with name [" + name + "] is currently running and won't be reset");
179+
}
180+
}
181+
});
182+
}
183+
170184
export function startCPUProfile(name: string) {
171185
if (!ENABLED) {
172186
return;

0 commit comments

Comments
 (0)