iOS matrix
matrix介紹
Matrix 是一款微信研發並日常使用的應用性能接入框架,支持iOS, macOS和Android。 Matrix 通過接入各種性能監控方案,對性能監控項的異常數據進行采集和分析,輸出相應的問題分析、定位與優化建議,從而幫助開發者開發出更高質量的應用。
matrix內部實現
當前工具監控范圍包括:崩潰、卡頓和爆內存,包含以下兩款插件:
1.WCCrashBlockMonitorPlugin: 基於 KSCrash 框架開發,具有業界領先的卡頓堆棧捕獲能力,同時兼備崩潰捕獲能力。
2.WCMemoryStatPlugin: 一款性能優化到極致的爆內存監控工具,能夠全面捕獲應用爆內存時的內存分配以及調用堆棧情況。
特性
WCCrashBlockMonitorPlugin
1.接入簡單,代碼無侵入
2.通過檢查 Runloop 運行狀態判斷應用是否卡頓,同時支持 iOS/macOS 平台
3.增加耗時堆棧提取,卡頓線程快照日志中附加最近時間最耗時的主線程堆棧
WCMemoryStatPlugin
1.在應用運行期間獲取對象存活以及相應的堆棧信息,在檢測到應用爆內存時進行上報
2.使用平衡二叉樹存儲存活對象,使用 Hash Table 存儲堆棧,將性能優化到極致
matrix安裝
1.通過 Cocoapods 安裝
- 先安裝 CocoaPods;
- 通過 pod repo update 更新 matrix 的 Cocoapods 版本;
- 在 Podfile 對應的 target 中,添加 pod 'matrix-wechat',並執行 pod install;
- 在項目中使用 Cocoapods 生成的 .xcworkspace運行工程;
- 在你的代碼文件頭引入頭文件 #import <Matrix/Matrix.h>,就可以接入微信的性能探針工具了!
2.通過靜態庫安裝
-
獲取 Matrix 源碼;
-
打開命令行,在 matrix/matrix-iOS 代碼目錄下執行 make 進行編譯生成靜態庫;
編譯完成后,iOS 平台的庫在 matrix/matrix-iOS/build_ios 目錄下,macOS 平台的庫在 matrix/matrix-iOS/build_macos 目錄下;
⚠️ 在make時如果遇到錯誤:
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
```
請用終端工具輸入以下內容然后繼續即可
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
- 工程引入靜態庫:
iOS 平台:使用 matrix/matrix-iOS/build_ios 路徑下的 Matrix.framework,將 Matrix.framework 以靜態庫的方式引入工程;
macOS 平台:使用 matrix/matrix-iOS/build_macos 路徑下的 Matrix.framework,將 Matrix.framework 以靜態庫的方式引入工程。
- 添加頭文件 #import <Matrix/Matrix.h>,就可以接入微信的性能探針工具了!
⚠️ 如果導入成功后編譯發現報了很多std錯誤 將項目文件.m 改為.mm 即可。(注:.m是object c語言文件 .mm是object c++語言文件)
## matrix使用
- 1.matrix初始化
Matrix *matrix = [Matrix sharedInstance];
MatrixBuilder *curBuilder = [[MatrixBuilder alloc] init];
curBuilder.pluginListener = self; // pluginListener 回調 plugin 的相關事件
WCCrashBlockMonitorPlugin *crashBlockPlugin = [[WCCrashBlockMonitorPlugin alloc] init];
[curBuilder addPlugin:crashBlockPlugin]; // 添加卡頓和崩潰監控
WCMemoryStatPlugin *memoryStatPlugin = [[WCMemoryStatPlugin alloc] init];
[curBuilder addPlugin:memoryStatPlugin]; // 添加內存監控功能
[matrix addMatrixBuilder:curBuilder];
// [crashBlockPlugin start]; // 開啟卡頓和崩潰監控
[memoryStatPlugin start];
// 開啟內存監控,注意 memoryStatPlugin 開啟之后對性能損耗較大,建議按需開啟
- 2.遵循MatrixPluginListenerDelegate
- (void)onInit:(id
)plugin{
}
- (void)onStart:(id
)plugin{
}
- (void)onStop:(id
)plugin{
}
- (void)onDestroy:(id
)plugin{
}
/**
- @brief The issue that has been triggered to report
- The listener has to help to report the issue to the right place
- @param issue The issue which be triggered to be reported
*/
-(void)onReportIssue:(MatrixIssue *)issue{
}