项目开发都会做一些调试,比如看看PFS的情况。
网上有不少工具,自己就参考做了一个比较简单的工具WHDebugTool,可以监测内存,CPU和FPS。
GitHub地址:https://github.com/remember17/WHDebugTool
WHDebugTool
1、快速使用方法
1.1 导入头文件
1 |
|
1.2 调用开关方法
一行代码开启或关闭监测。
1 2 |
|
1.3 可选:也可以通过如下方式初始化和关闭
1 2 3 4 |
|
2. 参数说明
初始化方法中带有一个枚举参数
这个参数可以让三种监测随意组合。例如只想要监测FPS,就传入DebugToolTypeFPS
1 2 3 4 5 6 7 |
|
3. 实现方法
3.1 FPS实现方法(参考了YYKit中的检测工具)
首先简单介绍一下FPS:
FPS的意思是:每秒传输帧数(刷新率)。
值越高,画面越流畅,值越低越卡顿。
下面来看一下iOS实现检测FPS的原理:
主要用的是CADisplayLink:一个和屏幕刷新率相同定时器。
创建CADisplayLink对象的时候会指定一个selector,把创建的CADisplayLink对象加入runloop,所以就实现了以屏幕刷新的频率调用某个方法。
在调用的方法中计算执行的次数,用次数除以时间,就算出了FPS。
注:iOS正常刷新率为每秒60次。
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 |
|
3.2 内存监测实现方法
1 2 3 4 5 6 7 8 9 10 |
|
3.3 CUP检测实现方法
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 |
|
后记
内存监测的值与Xcode给的有出入,所以这些工具给出的值仅供参考。
CUP和FPS的检测结果相对来说比较接近。
我的GitHub:https://github.com/remember17
作者:remember17
链接:https://www.jianshu.com/p/0d94a81a31db