支持基于 mPaaS 框架和原生工程进行日志埋点。
在iOS开发中,性能监控是确保应用流畅运行的关键。除了本文介绍的手动配置方法,开发者还可以借助工具如KeyMob来实现更全面的性能监控。KeyMob是一款专为iOS开发设计的综合工具,提供性能监控、文件管理、日志分析等功能,能够实时监控CPU、GPU、内存、FPS、网络和能耗等指标,帮助开发者优化应用性能和提升开发效率。
[MPAnalysisHelper setLagMonitorPercent: 100]; // 100% 监控,需要在 startPerformanceMonitor 调用之前设置
卡顿监控只有在真机上并且非 Xcode 调试状态下是打开的。
[MPAnalysisHelper startPerformanceMonitor],推荐在
-(void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用。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 调试状态下是打开的。
当默认的性能监控阈值无法满足您的需求时,可以自定义设置相关阈值。
#引入头文件 #import <MPMasAdapter/MPAnalysisHelper.h> /** 设置主线程卡顿监控的阈值,单位秒,可不设置,默认值为2秒 */ + (void)setLagTimeThreshold:(NSUInteger)threshold; /** 设置卡顿检测的间隔时长, 建议 lagTimeThreshold / lagCheckInterval 等于整数 */ + (void)setLagCheckInterval:(NSTimeInterval)interval;
#import <MPMasAdapter/MPMasSettings.h>
#创建 MPMasSettings 分类,重写下面方法做自定义
/**
获取卡死时长阈值,需自定义时在Category中重写,建议 anrTimeThreshold / anrCheckInterval 等于整数
*/
- (NSUInteger)anrTimeThreshold
{
return 自定义的时长;
}
/**
获取卡死检测间隔时长,需自定义时在Category中重写,建议 anrTimeThreshold / anrCheckInterval 等于整数
*/
- (NSTimeInterval)anrCheckInterval
{
return 自定义的检测间隔时长;
}
#import <MPMasAdapter/MPMasSettings.h>
#创建 MPMasSettings 分类,重写下面方法做自定义
/**
获取启动卡死时间阈值,需自定义时在Category中重写
*/
- (NSUInteger)startupAnrTimeThreshold
{
return 自定义的时长;
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70044672/viewspace-3116624/ ,如需转载,请注明出处,否则将追究法律责任。