美文网首页
Runloop - PFS监控

Runloop - PFS监控

作者: xxttw | 来源:发表于2023-06-25 22:04 被阅读0次
  • 通过实例化CADisplayLink添加到相当runloop中,并添加到当前应用程序窗口, 监控当前屏幕刷新率, 如果低于阈值, 可以高亮显示
  • 监控思路是获取当前屏幕刷新率,和1秒内的刷新次数, 如果刷新间隔 >=1秒 则计算其帧率比例, 如当前屏幕刷新次数 为55次, 时间间隔正好是1秒.则可以计算当前屏幕刷新率这1秒是55帧
  • FPS监控多用于开发测试环境中, 只管的用于快速发现问题
@interface ViewController ()
@property (nonatomic, strong) CADisplayLink *link;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSTimeInterval lastTime;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _lastTime = 0;
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(fpsTest:)];
    [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
}

// 这个方法调用的频次和屏幕的刷新率一致,正常来说是每秒60次
- (void)fpsTest:(CADisplayLink *)sender
{
    if (_lastTime == 0) {
        _lastTime = sender.timestamp;
    }
    _count++;// 记录刷新次数
    double dif = sender.timestamp - _lastTime;// 记录每一帧的时间间隔
    if (dif >= 1) { // 当时间间隔超过1秒时候, 看下FPS还有多少帧, 正常来说(60帧,) count/1 = 多少FPS  50/1 = 50帧
        NSInteger FPS = _count/dif;
        _lastTime = sender.timestamp;
        _count = 0;
    }
    NSLog(@"FPS: %ld- %fd", _count, dif);
}
2022-12-30 17:00:00.240023+0800 FPSMonitor[7305:607921] FPS: 1- 0.000000d
2022-12-30 17:00:00.256695+0800 FPSMonitor[7305:607921] FPS: 2- 0.016667d
2022-12-30 17:00:00.273200+0800 FPSMonitor[7305:607921] FPS: 3- 0.033333d
2022-12-30 17:00:00.290061+0800 FPSMonitor[7305:607921] FPS: 4- 0.050000d
2022-12-30 17:00:00.306698+0800 FPSMonitor[7305:607921] FPS: 5- 0.066667d
2022-12-30 17:00:00.323369+0800 FPSMonitor[7305:607921] FPS: 6- 0.083333d
2022-12-30 17:00:00.340047+0800 FPSMonitor[7305:607921] FPS: 7- 0.100000d
2022-12-30 17:00:00.356710+0800 FPSMonitor[7305:607921] FPS: 8- 0.116667d
2022-12-30 17:00:00.373377+0800 FPSMonitor[7305:607921] FPS: 9- 0.133333d
--------------------------------------------------------------------------------------------n
2022-12-30 17:00:01.157236+0800 FPSMonitor[7305:607921] FPS: 56- 0.916667d
2022-12-30 17:00:01.173904+0800 FPSMonitor[7305:607921] FPS: 57- 0.933333d
2022-12-30 17:00:01.190076+0800 FPSMonitor[7305:607921] FPS: 58- 0.950000d
2022-12-30 17:00:01.206733+0800 FPSMonitor[7305:607921] FPS: 59- 0.966667d
2022-12-30 17:00:01.223425+0800 FPSMonitor[7305:607921] FPS: 60- 0.983333d
2022-12-30 17:00:01.240077+0800 FPSMonitor[7305:607921] FPS: 0- 1.000000d
2022-12-30 17:00:01.257274+0800 FPSMonitor[7305:607921] FPS: 1- 0.016667d
2022-12-30 17:00:01.273922+0800 FPSMonitor[7305:607921] FPS: 2- 0.033333d

相关文章

  • iOS常规的优化技巧

    原文地址 卡顿优化 添加Observer到主线程RunLoop中,通过监听RunLoop状态切换的耗时,以达到监控...

  • iOS 性能监控(二)—— 主线程卡顿监控

    级别:★★☆☆☆标签:「iOS」「性能监控」「工具」「RunLoop」作者: 647 审校: QiShare团队...

  • iOS面试题:bugly的卡顿监控原理

    原文:iOS面试题大全 Runloop的两次source的监控渲染界面的频率来监控帧率

  • 常规优化技巧

    卡顿优化 添加Observer到主线程RunLoop中,通过监听RunLoop状态切换的耗时,以达到监控卡顿的目的...

  • iOS通过runloop监控卡顿

    质量监控-卡顿检测iOS实时卡顿监控基于Runloop简单监测iOS卡顿的demo微信iOS卡顿监控系统iOS-R...

  • iOS开发中的卡顿分析

    市面上的iOS卡顿分析方案有三种:监控FPS、监控RunLoop、ping主线程。 方案一:监控FPS 一般来说,...

  • runloop 监控UI活动

    当UI线程很忙时,通过监控runloop的活动,把布局或计算的操作放在用户交互后进行,比如tableview滑动停止时。

  • 监控卡顿-Runloop

    1 对主线程Runloop注册一个回调函数runLoopObserverCallBack。在每次runloop的C...

  • RunLoop下的卡顿监控

    在开发中,我们可以使用Xcode自带的Instruments工具的Core Animation来对APP运行流畅度...

  • Runloop监控卡顿

    一、监控卡顿的原理 1.1、原理 我们通过监听 NSRunLoop 的状态,就能够发现调用方法是否执行 时间过长,...

网友评论

      本文标题:Runloop - PFS监控

      本文链接:https://www.haomeiwen.com/subject/pjvtcdtx.html