美文网首页
腾讯性能监控Matrix

腾讯性能监控Matrix

作者: 放羊娃华振 | 来源:发表于2021-09-25 13:07 被阅读0次

    Matrix-android 当前监控范围包括:应用安装包大小,帧率变化,启动耗时,卡顿,慢方法,SQLite 操作优化,文件读写,内存泄漏等等。

    • APK Checker: 针对 APK 安装包的分析检测工具,根据一系列设定好的规则,检测 APK 是否存在特定的问题,并输出较为详细的检测结果报告,用于分析排查问题以及版本追踪
    • Resource Canary: 基于 WeakReference 的特性和 Square Haha 库开发的 Activity 泄漏和 Bitmap 重复创建检测工具
    • Trace Canary: 监控ANR、界面流畅性、启动耗时、页面切换耗时、慢函数及卡顿等问题
    • SQLite Lint: 按官方最佳实践自动化检测 SQLite 语句的使用质量
    • IO Canary: 检测文件 IO 问题,包括:文件 IO 监控和 Closeable Leak 监控
    • Battery Canary: 监控 App 活跃线程(待机状态 & 前台 Loop 监控)、ASM 调用 (WakeLock/Alarm/Gps/Wifi/Bluetooth 等传感器)、 后台流量 (Wifi/移动网络)等 Battery Historian 统计 App 耗电的数据

    特性

    与常规的 APM 工具相比,Matrix 拥有以下特点:

    APK Checker

    • 具有更好的可用性:JAR 包方式提供,更方便应用到持续集成系统中,从而追踪和对比每个 APK 版本之间的变化
    • 更多的检查分析功能:除具备 APKAnalyzer 的功能外,还支持统计 APK 中包含的 R 类、检查是否有多个动态库静态链接了 STL 、搜索 APK 中包含的无用资源,以及支持自定义检查规则等
    • 输出的检查结果更加详实:支持可视化的 HTML 格式,便于分析处理的 JSON ,自定义输出等等

    Resource Canary

    • 分离了检测和分析部分,便于在不打断自动化测试的前提下持续输出分析后的检测结果
    • 对检测部分生成的 Hprof 文件进行了裁剪,移除了大部分无用数据,降低了传输 Hprof 文件的开销
    • 增加了重复 Bitmap 对象检测,方便通过减少冗余 Bitmap 数量,降低内存消耗

    Trace Canary

    • 编译期动态修改字节码, 高性能记录执行耗时与调用堆栈
    • 准确的定位到发生卡顿的函数,提供执行堆栈、执行耗时、执行次数等信息,帮助快速解决卡顿问题
    • 自动涵盖卡顿、启动耗时、页面切换、慢函数检测等多个流畅性指标
    • 准确监控ANR,并且能够高兼容性和稳定性地保存系统产生的ANR Trace文件

    SQLite Lint

    • 接入简单,代码无侵入
    • 数据量无关,开发、测试阶段即可发现SQLite性能隐患
    • 检测算法基于最佳实践,高标准把控SQLite质量*
    • 底层是 C++ 实现,支持多平台扩展

    IO Canary

    • 接入简单,代码无侵入
    • 性能、泄漏全面监控,对 IO 质量心中有数
    • 兼容到 Android P

    Battery Canary

    • 接入简单,开箱即用
    • 预留 Base 类和 Utility 工具以便扩展监控特性

    Memory Hook

    • 一个检测 Android native 内存泄漏的工具
    • 无侵入,基于 PLT-hook(iqiyi/xHook),无需重编 native 库
    • 高性能,基于 Wechat-Backtrace 进行快速 unwind 堆栈,支持 aarch64 和 armeabi-v7a 架构

    Pthread Hook

    • 一个检测 Android Java 和 native 线程泄漏及缩减 native 线程栈空间的工具
    • 无侵入,基于 PLT-hook(iqiyi/xHook),无需重编 native 库
    • 通过对 native 线程的默认栈大小进行减半降低线程带来的虚拟内存开销,在 32 位环境下可缓解虚拟内存不足导致的崩溃问题

    WVPreAllocHook

    • 一个用于安全释放 WebView 预分配内存以在不加载 WebView 时节省虚拟内存的工具,在 32 位环境下可缓解虚拟内存不足导致的崩溃问题
    • 无侵入,基于 PLT-hook(iqiyi/xHook),无需重编 native 库
    • 使用该工具后 WebView 仍可正常工作

    Backtrace Component

    • 基于 DWARF 以及 ARM 异常处理数据进行简化并生成全新的 quicken unwind tables 数据,用于实现可快速回溯 native 调用栈的 backtrace 组件。回溯速度约是 libunwindstack 的 15x ~ 30x 左右。

    使用方法

    由于 JCenter 服务将于 2022 年 2 月 1 日下线,我们已将 Matrix 新版本(>= 0.8.0) maven repo 发布至 MavenCentral。

    1. 在你项目根目录下的 gradle.properties 中配置要依赖的 Matrix 版本号,如:
      MATRIX_VERSION=2.0.1
    
    1. 在你项目根目录下的 build.gradle 文件添加 Matrix 依赖,如:
      dependencies {
          classpath ("com.tencent.matrix:matrix-gradle-plugin:${MATRIX_VERSION}") { changing = true }
      }
    
    1. 接着,在 app/build.gradle 文件中添加 Matrix 各模块的依赖,如:
      dependencies {
        implementation group: "com.tencent.matrix", name: "matrix-android-lib", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-android-commons", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-trace-canary", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-resource-canary-android", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-resource-canary-common", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-io-canary", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-sqlite-lint-android-sdk", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-battery-canary", version: MATRIX_VERSION, changing: true
        implementation group: "com.tencent.matrix", name: "matrix-hooks", version: MATRIX_VERSION, changing: true
      }
    
      apply plugin: 'com.tencent.matrix-plugin'
      matrix {
        trace {
            enable = true   //if you don't want to use trace canary, set false
            baseMethodMapFile = "${project.buildDir}/matrix_output/Debug.methodmap"
            blackListFile = "${project.projectDir}/matrixTrace/blackMethodList.txt"
        }
      }
    

    目前 Matrix gradle plugin 支持 Android Gradle Plugin 3.5.0/4.0.0/4.1.0。

    1. 实现 PluginListener,接收 Matrix 处理后的数据, 如:
      public class TestPluginListener extends DefaultPluginListener {
        public static final String TAG = "Matrix.TestPluginListener";
        public TestPluginListener(Context context) {
            super(context);
    
        }
    
        @Override
        public void onReportIssue(Issue issue) {
            super.onReportIssue(issue);
            MatrixLog.e(TAG, issue.toString());
    
            //add your code to process data
        }
    }
    
    1. 实现动态配置接口, 可修改 Matrix 内部参数. 在 sample-android 中 我们有个简单的动态接口实例DynamicConfigImplDemo.java, 其中参数对应的 key 位于文件 MatrixEnum中, 摘抄部分示例如下:
      public class DynamicConfigImplDemo implements IDynamicConfig {
        public DynamicConfigImplDemo() {}
    
        public boolean isFPSEnable() { return true;}
        public boolean isTraceEnable() { return true; }
        public boolean isMatrixEnable() { return true; }
        public boolean isDumpHprof() {  return false;}
    
        @Override
        public String get(String key, String defStr) {
            //hook to change default values
        }
    
        @Override
        public int get(String key, int defInt) {
             //hook to change default values
        }
    
        @Override
        public long get(String key, long defLong) {
            //hook to change default values
        }
    
        @Override
        public boolean get(String key, boolean defBool) {
            //hook to change default values
        }
    
        @Override
        public float get(String key, float defFloat) {
            //hook to change default values
        }
    }
    
    
    1. 选择程序启动的位置对 Matrix 进行初始化,如在 Application 的继承类中, Init 核心逻辑如下:
      Matrix.Builder builder = new Matrix.Builder(application); // build matrix
      builder.pluginListener(new TestPluginListener(this)); // add general pluginListener
      DynamicConfigImplDemo dynamicConfig = new DynamicConfigImplDemo(); // dynamic config
    
      // init plugin 
      IOCanaryPlugin ioCanaryPlugin = new IOCanaryPlugin(new IOConfig.Builder()
                        .dynamicConfig(dynamicConfig)
                        .build());
      //add to matrix               
      builder.plugin(ioCanaryPlugin);
    
      //init matrix
      Matrix.init(builder.build());
    
      // start plugin 
      ioCanaryPlugin.start();
    

    至此,Matrix就已成功集成到你的项目中,并且开始收集和分析性能相关异常数据,如仍有疑问,请查看 示例.

    PS:

    1. 从 v0.9.0 开始,Matrix for Android 迁移到了 AndroidX. 你可能需要添加 'android.useAndroidX=true' 标志到 gradle.properties 文件里。
    2. Matrix 分析后的输出字段的含义请查看 Matrix 输出内容的含义解析

    Battery Canary Usage

    相关初始化代码如下:

    BatteryMonitorConfig config = new BatteryMonitorConfig.Builder()
            .enable(JiffiesMonitorFeature.class)
            .enableStatPidProc(true)
            .greyJiffiesTime(30 * 1000L)
            .setCallback(new BatteryMonitorCallback.BatteryPrinter())
            .build();
    
    BatteryMonitorPlugin plugin = new BatteryMonitorPlugin(config);
    

    具体使用方式,请参考单元测试里相关用例的代码: com.tencent.matrix.batterycanary.ApisTestsample.tencent.matrix.battery.BatteryCanaryInitHelper.

    Backtrace Component Usage

    如何初始化 backtrace 组件:

    WeChatBacktrace.instance().configure(getApplicationContext()).commit();
    

    初始化后其他 Matrix 组件就可以使用 Quicken Backtrace 进行回溯。更多参数的配置请查看 WeChatBacktrace.Configuration 的接口注释。

    APK Checker

    APK Check 以独立的 jar 包提供 (matrix-apk-canary-2.0.1.jar),你可以运行:

    java -jar matrix-apk-canary-2.0.1.jar
    

    查看 Usages 来使用它。

    Usages: 
        --config CONFIG-FILE-PATH
    or
        [--input INPUT-DIR-PATH] [--apk APK-FILE-PATH] [--unzip APK-UNZIP-PATH] [--mappingTxt MAPPING-FILE-PATH] [--resMappingTxt RESGUARD-MAPPING-FILE-PATH] [--output OUTPUT-PATH] [--format OUTPUT-FORMAT] [--formatJar OUTPUT-FORMAT-JAR] [--formatConfig OUTPUT-FORMAT-CONFIG (json-array format)] [Options]
    
    Options:
    -manifest
         Read package info from the AndroidManifest.xml.
    -fileSize [--min DOWN-LIMIT-SIZE (KB)] [--order ORDER-BY ('asc'|'desc')] [--suffix FILTER-SUFFIX-LIST (split by ',')]
         Show files whose size exceed limit size in order.
    -countMethod [--group GROUP-BY ('class'|'package')]
         Count methods in dex file, output results group by class name or package name.
    -checkResProguard
         Check if the resguard was applied.
    -findNonAlphaPng [--min DOWN-LIMIT-SIZE (KB)]
         Find out the non-alpha png-format files whose size exceed limit size in desc order.
    -checkMultiLibrary
         Check if there are more than one library dir in the 'lib'.
    -uncompressedFile [--suffix FILTER-SUFFIX-LIST (split by ',')]
         Show uncompressed file types.
    -countR
         Count the R class.
    -duplicatedFile
         Find out the duplicated resource files in desc order.
    -checkMultiSTL  --toolnm TOOL-NM-PATH
         Check if there are more than one shared library statically linked the STL.
    -unusedResources --rTxt R-TXT-FILE-PATH [--ignoreResources IGNORE-RESOURCES-LIST (split by ',')]
         Find out the unused resources.
    -unusedAssets [--ignoreAssets IGNORE-ASSETS-LIST (split by ',')]
         Find out the unused assets file.
    -unstrippedSo  --toolnm TOOL-NM-PATH
         Find out the unstripped shared library file.
    
    

    由于篇幅影响,此次不再赘述,我们在 Matrix-APKChecker 中进行了详细说明。

    参加文章:
    https://www.jianshu.com/p/0db4a02d6ca7

    相关文章

      网友评论

          本文标题:腾讯性能监控Matrix

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