forked from wechat-miniprogram/miniprogram-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps_helper.ts
79 lines (70 loc) · 1.83 KB
/
fps_helper.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
/**
* 统计开始到现在/结束的fps
*/
class FpsHelper {
startTime = 0;
frameCount = 0;
endTime = 0;
tempFrameCount = 0;
tempStartTime = 0;
/** 更新fps, 回调1秒内的平均fps */
updateFPS(onTempFPS: (fps: number) => any) {
const now = Date.now();
//更新全局fps记录
if (this.frameCount == 0) {
this.startTime = now;
}
this.frameCount++;
this.endTime = now;
//更新临时fps记录
this.tempFrameCount++;
if (this.tempStartTime > 0) {
const interval = now - this.tempStartTime;
if (interval > 1000) {
const fps = Math.round(1000 / (interval / this.tempFrameCount));
this.tempFrameCount = 0;
this.tempStartTime = now;
onTempFPS && onTempFPS(fps);
}
} else {
this.tempStartTime = now;
onTempFPS && onTempFPS(0);
}
}
/** 获得开始到当前的平均fps */
getAverageFps() {
const duration = this.endTime - this.startTime;
if (duration > 1000) {
return Math.round((1000 * this.frameCount) / duration);
}
return 0;
}
/** 重置所有的计算 */
reset() {
this.startTime = 0;
this.frameCount = 0;
this.endTime = 0;
this.tempFrameCount = 0;
this.tempStartTime = 0;
}
/** 执行上报fps */
doReport(record: boolean = false, face: boolean = false) {
const duration = this.endTime - this.startTime;
if (duration > 5000) {
const fps = Math.round((1000 * this.frameCount) / duration);
const info = wx.getSystemInfoSync();
wx.report('skip_fps', {
fps,
duration,
record: record ? 1 : 0,
face: face ? 1 : 0,
up_platform: info.platform,
up_brand: info.brand,
up_model: info.model,
up_benchmark: info.benchmarkLevel
});
this.reset();
}
}
}
export { FpsHelper };