Android 接入微信性能监控框架Matrix实践

本文主要是记录笔者接入微信性能监控框架Matrix的过程,以及简单阐述如何分析Matrix输出的监控数据。

一、接入步骤

1. 项目的根目录下的gradle.properties文件声明接入Matrix的版本

lib_thirds_matrix_version=2.0.8

2. 项目的根目录下的build.gradle文件 声明Matrix Gradle 插件

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
       
        classpath ("com.tencent.matrix:matrix-gradle-plugin:${lib_thirds_matrix_version}") { changing = true }
     
    }
}

3. 在app build.gralde文件声明apply Matrix插件

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"
    }
}

4. 在项目接入Matrix的Module 进行依赖

一般我们是在app module下的build.gradle进行依赖,但是笔者比较喜欢按功能划分module,所以新建一个Module: apmlib, 并且在apmlib的build.gradle下进行依赖。

dependencies {
    //按需依赖需要的canary
    implementation group: "com.tencent.matrix", name: "matrix-android-lib", version: lib_thirds_matrix_version, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-android-commons", version: lib_thirds_matrix_version, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-trace-canary", version: lib_thirds_matrix_version, changing: true
    implementation group: "com.tencent.matrix", name: "matrix-io-canary", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-memory-canary", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-resource-canary-android", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-resource-canary-common", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-sqlite-lint-android-sdk", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-battery-canary", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-hooks", version: lib_thirds_matrix_version, changing: true
//    implementation group: "com.tencent.matrix", name: "matrix-backtrace", version: lib_thirds_matrix_version, changing: true


......
}

5. 自定义一个单例管理器来完成Matrix的初始化工作

package com.mikel.apmlib;

import android.app.Application;

import com.tencent.matrix.Matrix;
import com.tencent.matrix.iocanary.IOCanaryPlugin;
import com.tencent.matrix.iocanary.config.IOConfig;
import com.tencent.matrix.trace.TracePlugin;
import com.tencent.matrix.trace.config.TraceConfig;

public class MatrixManager {

    private static volatile MatrixManager INSTANCE;

    private MatrixManager() {
    }

    public static MatrixManager getInstance() {
        if (INSTANCE == null) {
            synchronized (MatrixManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new MatrixManager();
                }
            }
        }
        return INSTANCE;
    }

    public void doOnCreate(Application application, String splashActivity) {
        Matrix.Builder builder = new Matrix.Builder(application); // build matrix
        builder.pluginListener(new MatrixPluginListener(application)); // add general pluginListener
        MatrixDynamicConfigImpl matrixDynamicConfig = new MatrixDynamicConfigImpl(); // dynamic config
        boolean fpsEnable = matrixDynamicConfig.isFPSEnable();
        boolean traceEnable = matrixDynamicConfig.isTraceEnable();
        //Trace plugin
        TraceConfig traceConfig = new TraceConfig.Builder()
                .dynamicConfig(matrixDynamicConfig)
                .enableFPS(fpsEnable)//帧率
                .enableEvilMethodTrace(traceEnable)//慢方法
                .enableAnrTrace(traceEnable)//anr
                .enableStartup(traceEnable)//启动速度
                .splashActivities(splashActivity)//首页
                //debug模式
                .isDebug(true)
                //dev环境
                .isDevEnv(false)
                .build();

        TracePlugin tracePlugin = new TracePlugin(traceConfig);
        builder.plugin(tracePlugin);

        // io plugin
        IOCanaryPlugin ioCanaryPlugin = new IOCanaryPlugin(new IOConfig.Builder()
                .dynamicConfig(matrixDynamicConfig)
                .build());
        builder.plugin(ioCanaryPlugin);

        //init matrix
        Matrix.init(builder.build());
        tracePlugin.start();
        ioCanaryPlugin.start();
    }
}

本文只使用了Matrix的TraceCanary和IOCanary来做Demo, Matrix的功能远不止这些,还有其他的Canary,可以按需接入。

此外,初始化的时候,可以继承Matrix的DefaultPluginListener进行性能监控的数据监听以及实现IDynamicConfig自定义一些性能监控的阈值。

package com.mikel.apmlib;

import android.content.Context;

import com.tencent.matrix.plugin.DefaultPluginListener;
import com.tencent.matrix.report.Issue;
import com.tencent.matrix.util.MatrixLog;

public class MatrixPluginListener extends DefaultPluginListener {
    public static final String TAG = "MatrixPluginListener";
    public MatrixPluginListener(Context context) {
        super(context);
    }

    @Override
    public void onReportIssue(Issue issue) {
        super.onReportIssue(issue);
        //todo 处理性能监控数据
        MatrixLog.e(TAG, issue.toString());
    }
}
package com.mikel.apmlib;
import com.tencent.mrs.plugin.IDynamicConfig;

public class MatrixDynamicConfigImpl implements IDynamicConfig {
    public MatrixDynamicConfigImpl() {}

    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
        return defStr;
    }

    @Override
    public int get(String key, int defInt) {
        //hook to change default values
        return defInt;
    }

    @Override
    public long get(String key, long defLong) {
        //hook to change default values
//        if (IDynamicConfig.ExptEnum.clicfg_matrix_trace_evil_method_threshold.name().equals(key)) {
//            return 300; // 默认为700
//        }
        return defLong;
    }

    @Override
    public boolean get(String key, boolean defBool) {
        //hook to change default values
        return defBool;
    }

    @Override
    public float get(String key, float defFloat) {
        //hook to change default values
        return defFloat;
    }
}

6. 在Application.onCreate方法调用Matrix初始化

        MatrixManager.getInstance().doOnCreate(getApplication(),"com.mikel.projectdemo.MainActivity");

二、 Matrix 性能数据分析

1. 慢方法监控

Matrix 对堆栈进行了聚合,解决堆栈数据量大,后台很难聚类的问题,详细原理见:

Matrix Android TraceCanary · Tencent/matrix Wiki · GitHub

根据TraceStack 的 methidId,在项目app-build-outputs-mapping-debug-methodMapping.txt 找到对应方法。

2. ANR监控

 

3. 启动速度监控

启动速度监控根据Matrix Trace-Canary 的StartupTracer开头注释很容易理解

application_create:  第一个方法调用时  到 四大组件创建时  对应下图applicationCost

first_activity_create:  第一个方法调用时  到 首个Activity.onWindowFocusChange 对应下图firstScreenCost

冷启动:第一个方法调用时  到 MainActivity.onWindowFocusChange 对应下图coldCost

温启动:首个Activity.onCreate 到 首个Activity.onWindowFocusChange 对应下图warmCost

 

4. 帧率监控

 

过滤TAG :Trace_FPS 可以得到帧率监控数据

关键字段解析:

    scene: 关键页面

    dropLevel: 一个采样周期,根据每60帧的丢帧数落入不同区间的次数。

                       DROPPED_FROZEN 代表采样周期内每60帧丢了42帧以上的次数

                       DROPPED_HIGH 代表采样周期内每60帧丢了24-42帧的次数

                       DROPPED_MIDDLE 代表采样周期内每60帧里丢了9-24帧的次数

                       DROPPED_NONAL 代表采样周期内每60帧里丢了3-9帧的次数

                       DROPPED_BEST 代表采样周期内每60帧丢了0-3帧的次数

      dropSum:  一个采样周期,落入不同区间的累计丢帧数。

                       DROPPED_FROZEN  代表采样周期内每60帧丢了42帧以上的累计丢帧数

                       DROPPED_HIGH  代表采样周期内每60帧丢了24-42帧的累计丢帧数

                       DROPPED_MIDDLE 代表采样周期内每60帧里丢了9-24帧的累计丢帧数

                       DROPPED_NONAL 代表采样周期内每60帧里丢了3-9帧的累计丢帧数

                       DROPPED_BEST 代表采样周期内每60帧丢了0-3帧的累计丢帧数

        fps: 帧率, 监控指标

 

 

 

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Android应用接入微信支付,您需要按照以下步骤进行操作: 1. 在微信支付开放平台注册账号并登录。前往微信支付开放平台(https://pay.weixin.qq.com/)注册一个账号,并进行登录。 2. 创建应用并获取应用ID。在开放平台创建一个应用,并获取到对应的应用ID,这是后续接入过程中的重要标识。 3. 配置应用信息。在应用管理页面填写应用的基本信息,包括应用名称、AppID、支付回调URL等。确保信息填写准确无误。 4. 下载SDK和文档。在开放平台下载对应的微信支付SDK和接入文档,以便后续进行接入操作。 5. 导入SDK到Android项目。将下载的微信支付SDK导入到您的Android项目中,可以使用Gradle或手动导入的方式进行集成。 6. 配置权限和签名。在AndroidManifest.xml文件中添加必要的权限声明,并确保应用的签名信息与在微信支付开放平台注册时填写的一致。 7. 实现支付功能。根据微信支付开放平台提供的接入文档,按照要求实现支付功能的相关代码逻辑。 8. 发起支付请求。在合适的时机,通过调用微信支付SDK提供的API发起支付请求,并传递必要的订单信息和回调参数。 9. 处理支付结果回调。在您的应用中处理微信支付结果的回调通知,根据返回的支付结果状态进行相应的处理操作。 10. 测试和上线。在开发阶段进行测试,确保支付流程和结果正常。待测试通过后,提交应用上线并进行发布。 请注意,以上只是一个大致的流程概述,具体的接入步骤和代码实现会因应用的具体需求和技术栈而有所差异。建议您仔细阅读微信支付开放平台提供的官方文档,并根据文档中的指引进行接入操作。如果遇到问题,可以参考文档中的常见问题解答或联系微信支付开放平台的技术支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值