DevOps工具链之 IOS监控编程(二)

Posted by Marco Liu on July 26, 2018

“Hope for the best. ”

下面分别介绍性能指标数据

APP基本信息不在这累述

性能指标

1,PFS获取

TDFPSMonitor这个类检测主线程帧率变化,这里面用到了与系统帧率一样的CADisplayLink类检测主线程帧率变化,原理也很简单


#pragma mark - Public
- (void)startMonitoring {
    if (_isMonitoring) { return; }
    _isMonitoring = YES;
    self.displayLink = [CADisplayLink displayLinkWithTarget: [[TDWeakProxy alloc]initWithTarget:self] selector: @selector(monitor:)];
    [self.displayLink addToRunLoop: [NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
    self.lastTime = self.displayLink.timestamp;
    if ([self.displayLink respondsToSelector: @selector(setPreferredFramesPerSecond:)]) {
        if (@available(iOS 10.0, *)) {
            self.displayLink.preferredFramesPerSecond = 60;
        } else {
            // Fallback on earlier versions
        }
    } else {
        self.displayLink.frameInterval = 1;
    }
}
#pragma mark - DisplayLink
- (void)monitor: (CADisplayLink *)link {
  //记录每一次帧率回调时间
    if (self.delegate && [self.delegate respondsToSelector:@selector(fpsFrameCurrentTime:)]){
        NSString *currenT = [self getCurrntTime];
       // NSLog(@"fps====%@",currenT);
        [self.delegate fpsFrameCurrentTime: currenT];
    }
    if (_lastTime == 0) {
        _lastTime = link.timestamp;
        return;
    }
    _count++;
    NSTimeInterval delta = link.timestamp - _lastTime;
    if (delta < 1) return;
    _lastTime = link.timestamp;
    float fps = _count / delta;
    _fps = fps;
    _count = 0;   
}
//获取帧率
- (double)getFPS {
    return _fps;
}
2,内存占用率

内存占用率有两个版本一个是OC版的,系统整体占用率TDSystemMemoryUsage,一个是APP在设备占用率TDApplicationMemory

还有一个是swift版的第三方获取内存占用率Memory

原理代码都是一样的,网上也会对这个有所介绍,这里不做累述

//获取当前App Memory的使用情况
- (NSUInteger)getResidentMemory
{
    struct mach_task_basic_info info;
    mach_msg_type_number_t count =MACH_TASK_BASIC_INFO_COUNT;
    int r = task_info(mach_task_self(),MACH_TASK_BASIC_INFO, (task_info_t)& info, & count);
    if (r == KERN_SUCCESS)
    {
        //info.resident_size >> 10;//10-KB   20-MB  
        return info.resident_size;
    }
    else
    {
        return -1;
    }
}
//获取设备的物理内存
- (NSUInteger)getPhysicalMemory {
    NSUInteger memory =  [NSProcessInfo processInfo].physicalMemory;
    return memory;
}
3,CPU占用率

CPU占用率也有两个版本OC版的,应用CPU占用TDApplicationCPU,整个设备占用CPU情况 TDSystemCPU

swift版是CPU类

代码原理是一样的,可以进去

- (double)currentUsage {
    kern_return_t           kr = { 0 };
    task_info_data_t        tinfo = { 0 };
    mach_msg_type_number_t  task_info_count = TASK_INFO_MAX;
    kr = task_info( mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count );
    if ( KERN_SUCCESS != kr )
        return 0.0f;
    task_basic_info_t       basic_info = { 0 };
    thread_array_t          thread_list = { 0 };
    mach_msg_type_number_t  thread_count = { 0 };
    thread_info_data_t      thinfo = { 0 };
    thread_basic_info_t     basic_info_th = { 0 };
    basic_info = (task_basic_info_t)tinfo;
    // get threads in the task
    kr = task_threads( mach_task_self(), &thread_list, &thread_count );
    if ( KERN_SUCCESS != kr )
        return 0.0f;
    long    tot_sec = 0;
    long    tot_usec = 0;
    float   tot_cpu = 0;
    for ( int i = 0; i < thread_count; i++ )
    {
        mach_msg_type_number_t thread_info_count = THREAD_INFO_MAX;
        kr = thread_info( thread_list[i], THREAD_BASIC_INFO, (thread_info_t)thinfo, &thread_info_count );
        if ( KERN_SUCCESS != kr )
            return 0.0f;
        basic_info_th = (thread_basic_info_t)thinfo;
        if ( 0 == (basic_info_th->flags & TH_FLAGS_IDLE) )
        {
            tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
            tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
            tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE;
        }
    }
    kr = vm_deallocate( mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t) );
    if ( KERN_SUCCESS != kr )
        return 0.0f;
    return tot_cpu * 100.; // CPU 占用百分比
}
4,电量消耗

电量多少通过TDPerformanceMonitor来获取的

//获取电量
- (void)getElectricity {
    /*
     UIDeviceBatteryStateUnknown,
     UIDeviceBatteryStateUnplugged, // on battery, discharging
     UIDeviceBatteryStateCharging, // plugged in, less than100%
     UIDeviceBatteryStateFull, // plugged in, at 100%
     */
    [UIDevice currentDevice].batteryMonitoringEnabled = YES;
    [[NSNotificationCenter defaultCenter]
     addObserverForName:UIDeviceBatteryLevelDidChangeNotification
     object:nil queue:[NSOperationQueue mainQueue]
     usingBlock:^(NSNotification *notification) {

         // Level has changed
         NSLog(@"Battery Level Change");
         NSLog(@"电池电量:%f%%", [UIDevice currentDevice].batteryLevel * 100);
     }];
}
5,网络流量监控

针对网络监控

对于成功率、状态码、流量,以及网络的响应时间之类的,我们可以主要可以通过两种方式来做

针对URLConnection、CFNetwork、NSURLSession三种网络做Hook,hook的具体技术可以是method swizzle 也可以是Proxy、Fishhook之类的

也可以使用 NSURLProtocol对网络请求的拦截,进而得到流量、响应时间等信息,但是NSURLProtocol有自己的局限,比如NSURLProtocol只能拦截NSURLSession,NSURLConnection以及UIWebView,但是对于CFNetwork则无能为力

对于第一种方式可以Hook哪些方法的,可以参考这个图

对于 HTTP与HTTPS 的 DNS 解析、TCP握手、SSL握手(HTTP除外)、首包时间等时间的统计,稍有难度

但是,因为我们所使用的URLConnection、CFNetwork、NSURLSession底层都是 BSDSocket,所以可以尝试在socket上动手脚来实现效果,类似于通过ViewController的生命周期方法来统计页面加载时间的做法,我们Hook socket相关的方法来做,比如通过hooksocket连接时的 connect方法,拿到tcp握手的起始时间,通过hookSSLHandshake方法,在SSLHandshake执行的时候拿到 SSL握手的起始时间等。目前听云已经提供了 HTTP的分段时间查询功能,大家去体验下

但是对于 iOS 9 Apple 加入 ATS 新特性,并要求开发者使用 HTTPS,我在 iOS9、10上对 HTTPS 网络请求Hook socket方法时候,有一些方法hook失效,猜想应该是Apple 进行了加固、加密,导致一些系统方法没办法hook,所以在 iOS9、10 上无法通过socket来取得HTTPS网络的分段时间(纠正:fishhook 无法 hook socket 的原因:https://github.com/facebook/fishhook/issues/40)

不过apple在 iOS 10 推出一个API,可以在 iOS10 版本以上进行网络信息的收集

  • (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics

以上结合网上摘要

以下本项目是使用第三方NetworkEye来检测网络

NetworkEye是一个网络调试库,可以监控App内HTTP请求并显示请求相关的详细信息,方便App开发的网络调试。

但是后面研究NetworkEye是有缺陷的,已经废除,1,只能获取下行流量 2,下行流量也不准确

可以检测到包括网页,NSURLConnection,NSURLSession,AFNetworking,第三方库,第三方SDK等的HTTP请求,非常方便实用。并且可以统计App内流量

现在监控网络流量是自己写的一套网络监控框架TDNetworkTrafficManager

—— Marco 后记于 2018.07