安卓性能专项之FPS测试篇

FPS测试

FPS是每秒传输帧数(Frames Per Second),每秒钟帧数越多,则画面看起来越流畅,FPS测试也是手机游戏性能测试的一个重点专项。

测试工具

APP性能测试工具

第三方的APP性能测试工具,主流代表,GT,Wetest,Emmagee等。

使用方式:各APP的帮助文件已经详细说明了~不再重复~

adb方式

开发者选项–>GPU呈现模式分析–>使用adb shell dumpsys gfxinfo

1
adb shell dumpsys gfxinfo "your app package name" >fps.txt

查看fps.txt,在Profile data in ms下方,查看刷新时间。

Unity手游并不适用这个方式

使用WeTest查看FPS

鹅厂出品WeTest性能测试工具,可以实时计算出手游FPS,实测挺准的。推荐使用。

Unity自制FPS显示工具

我们也可以制作一个Unity专用的FPS显示工具,与Wetest相比,我们可以有更多的定制内容,比如加入帧数低于指定数字时,显示数字变成红色。

工具原理:

OnGUI方法是每帧调用的,使用一个变量,在每帧调用的时候自增,每隔一定时间统计帧数总量,总量除以累计的时间,就为每秒传输帧数FPS了,在Unity官网就有实例代码。

使用方式:保存为ShowFPS.cs,直接挂在主场景的Main Camera即可。

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
using UnityEngine;
using System.Collections;
public class ShowFPS : MonoBehaviour {
public GUIStyle FPSStyle;
public float updateInterval = 0.5F; //统计间隔
private float lastInterval;
private int frames = 0;
private float fps;
private int warnFPS = 40; //FPS警告临界值
void Start() {
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}
void OnGUI() {
if (fps < warnFPS) {
FPSStyle.normal.textColor = Color.red;
} else {
FPSStyle.normal.textColor = Color.white;
}
GUI.Label(new Rect(5, 200, 80, 30), "FPS:" + fps.ToString("f2"), FPSStyle);
}
void Update() {
++frames;
if (Time.realtimeSinceStartup > lastInterval + updateInterval) {
fps = frames / (Time.realtimeSinceStartup - lastInterval);
frames = 0;
lastInterval = Time.realtimeSinceStartup;
}
}
}