-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathprofiling.js
85 lines (85 loc) · 2.09 KB
/
profiling.js
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
exports.ENABLE_PROFILING = true;
function time() {
if (!exports.ENABLE_PROFILING) {
return;
}
if (global.android) {
return java.lang.System.nanoTime() / 1000000; // 1 ms = 1000000 ns
}
else {
return CACurrentMediaTime() * 1000;
}
}
exports.time = time;
if (!global.timers) {
global.timers = new Map();
}
function start(name) {
if (!exports.ENABLE_PROFILING) {
return;
}
var info;
if (global.timers.has(name)) {
info = global.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()
};
global.timers.set(name, info);
}
}
exports.start = start;
function pause(name) {
if (!exports.ENABLE_PROFILING) {
return;
}
var info = pauseInternal(name);
console.log("---- [" + name + "] PAUSE last: " + info.lastTime + " total: " + info.totalTime + " count: " + info.count);
}
exports.pause = pause;
function stop(name) {
if (!exports.ENABLE_PROFILING) {
return;
}
var info = pauseInternal(name);
console.log("---- [" + name + "] STOP total: " + info.totalTime + " count:" + info.count);
global.timers.delete(name);
}
exports.stop = stop;
function pauseInternal(name) {
var info = global.timers.get(name);
if (!info) {
throw new Error("No timer started: " + name);
}
console.dump(info);
info.lastTime = Math.round(time() - info.currentStart);
info.totalTime += info.lastTime;
info.count++;
info.currentStart = 0;
return info;
}
function startCPUProfile(name) {
if (!exports.ENABLE_PROFILING) {
return;
}
if (global.android) {
__startCPUProfiler(name);
}
}
exports.startCPUProfile = startCPUProfile;
function stopCPUProfile(name) {
if (!exports.ENABLE_PROFILING) {
return;
}
if (global.android) {
__stopCPUProfiler(name);
}
}
exports.stopCPUProfile = stopCPUProfile;