forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling.ts
103 lines (85 loc) · 2.12 KB
/
profiling.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
export var ENABLE_PROFILING = true;
var log = function(text) {
var line = document.createElement('p');
line.innerHTML = text;
var logContainer = document.getElementById('log');
logContainer.appendChild(line);
}
export function time(): number {
if (!ENABLE_PROFILING) {
return;
}
return performance.now();
}
interface TimerInfo {
totalTime: number;
lastTime?: number;
count: number;
currentStart: number;
}
var timers = new Map<string, TimerInfo>();
export function start(name: string): void {
if (!ENABLE_PROFILING) {
return;
}
console.time(name);
var info: TimerInfo;
if (timers.has(name)) {
info = timers.get(name);
if (info.currentStart != 0) {
throw new Error(`Timer already started: ${name}`);
}
info.currentStart = time();
}
else {
info = {
totalTime: 0,
count: 0,
currentStart: time()
};
timers.set(name, info);
}
}
export function pause(name: string) {
if (!ENABLE_PROFILING) {
return;
}
var info = pauseInternal(name);
log(`---- [${name}] PAUSE last: ${info.lastTime} total: ${info.totalTime} count: ${info.count}`);
}
export function stop(name: string) {
if (!ENABLE_PROFILING) {
return;
}
console.timeEnd(name);
var info = pauseInternal(name);
log(`---- [${name}] STOP total: ${info.totalTime} count:${info.count}`);
timers.delete(name);
}
function pauseInternal(name: string): TimerInfo {
var info = timers.get(name);
if (!info) {
throw new Error(`No timer started: ${name}`);
}
info.lastTime = Math.round(time() - info.currentStart);
info.totalTime += info.lastTime;
info.count++;
info.currentStart = 0;
return info;
}
export function startCPUProfile(name: string) {
if (!ENABLE_PROFILING) {
return;
}
if (global.android) {
__startCPUProfiler(name);
}
}
export function stopCPUProfile(name: string) {
if (!ENABLE_PROFILING) {
return;
}
if (global.android) {
__stopCPUProfiler(name);
}
}