添加性能日志

更新时间: 2022-04-24 17:37:27

性能日志用于统计应用的启动速度、卡顿与卡死等情况。您可以在移动分析控制台的 基础分析 页面查看启动速度指标;在 性能分析 页面中查看卡顿与卡死报告。

支持基于 mPaaS 框架和原生工程进行日志埋点。

基于 mPaaS 框架

  1. 卡顿监控默认对 10% 的设备开启,可通过下面这个接口设置卡顿开启率。

    [MPAnalysisHelper setLagMonitorPercent: 100]; // 100% 监控,需要在 startPerformanceMonitor 调用之前设置
    卡顿监控只有在真机上并且非 Xcode 调试状态下是打开的。
  2. 在启动时调用 [MPAnalysisHelper startPerformanceMonitor],推荐在 -(void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用。

    启动性能监控
  3. 为了记录启动时间,您只需在 App 启动完成(比如在首页出现时)后记录启动时间,并发出启动完成的通知即可。

    // globalMonitorStartUpTime 是已定义的变量,导入 <mPaas/MPaaS+MonitorStartUpTime.h> 头文件后即可使用
    double time = CFAbsoluteTimeGetCurrent() - globalMonitorStartUpTime;
    [ [NSNotificationCenter defaultCenter] postNotificationName:@"APMonitor_Startup_Cost_Time" object:nil userInfo:@{@"CostTimeOnUserFeel": [NSString stringWithFormat:@"%f", time]}];

基于原生工程

SDK 封装了性能监控接口,推荐您在 AppDelegate 的 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions 方法中调用 [PerformanceHelper performanceMonitor]

    #import "PerformanceHelper.h"
    #import <MPAnalysis/MPAnalysisHelper.h>

    static NSTimeInterval __start_timestamp = 0;

    @implementation PerformanceHelper

    + (void)load
    {
        __start_timestamp = CFAbsoluteTimeGetCurrent();
    }

    + (void)performanceMonitor
    {
        //start performance monitor

       [MPAnalysisHelper setLagMonitorPercent: 100]; // 100% 监控,需要在 startPerformanceMonitor 调用之前设置
        [MPAnalysisHelper startPerformanceMonitor];

        //record the time interval used for the app startup
        NSTimeInterval time = CFAbsoluteTimeGetCurrent() - __start_timestamp;
        [[MPAnalysisHelper sharedInstance] writeLogForStartupWithTime:time];

    }

    @end
卡顿监控只有在真机上并且非 Xcode 调试状态下是打开的。
阿里云首页 移动开发平台 mPaaS 相关技术圈