Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API)

这篇具有很好参考价值的文章主要介绍了Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.adb logcat 查看冷启动时间和Activity显示时间:

过滤Displayed关键字,可看到Activity的显示时间
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
那上面display后面的是时间是指包含哪些过程的时间呢?

模拟在Application中沉睡1秒操作,冷启动情况下:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
从上可知:在冷启动情况下,第一个activity的display是冷启动的总时间=application+Activity的时间。

接着,界面上点击按钮,随意启动一个Activity:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
从上可知:app进程在线情况下,启动activity的display 时间才是activity的显示时间(onCreate()->onStart()->onResume())

2.adb shell 启动activity的时间:

格式:

adb shell am start -W package名/包名路径的activity

例如,执行打开指定的mainActivity,如下图所示:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
解读以上数据

  • TotalTime: 是Activity的启动时间 ,与adb logcat 中Display 时间规则一致(通常以这个时间为准)
  • WaitTime: 是AMS启动Activity的总耗时(它会TotalTime更大,存在跨进程通讯回执时间)

通过adb shell 启动的activity 必须具备以下条件之一:

设置程序的主入口:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
或者设置exported为true
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb

3.AndroidStudio的cpu Profile:

3.1适用于app冷启动时的trace捕捉

创建一个新的运行configurations, 在profiling中配置:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
一般是选择 java method sample,即Profiler的sample 模式,对性能影响小。系统会间隔us周期性记录java方法的调用栈,记录较为耗时,耗时较少不会显示在trace中。

  • Java method trace: 适用于精确记录方法执行时间,即profiler的instrument模式,因一直记录方法的调用栈和cpu 使用情况,对性能影响很大,可能会造成黑屏,卡顿等情况。
  • CallStack sample 适用于记录natvie+java的方法分析,Profiler的sample 模式。
  • System trace: 记录同一个过程中系统各个进程的trace,适合更复杂的分析耗时。

使用方式: 先使用java method sample 记录,找出耗时的所在;接着,再用 java method trace 精确分析真正的耗时时间。

点击profiler 按钮后,等待安装后,启动后,会自动开始捕捉,等执行完需要观察的流程后,点击stop 。

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
打开录制的trace文件,选择main 主线程,打开如下所示:

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
从上面,可以清楚看到app 主线程中每个环节的方法调用栈和耗时时间等。 带颜色的方块越长,耗时越多,绿色一般是app中的代码。

选择Application的onCreate()查看,双击该方法,出现如下所示:

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
通过上图,可知左边菜单可知该方法耗时1s ,右边菜单top down 可知该方法的调用栈,真正耗时所在,沉睡一秒导致。

3.2 app 运行时,记录trace

有些界面是在app 特殊情况下触发,捕获该流程的trace 。可通过usb 连接手机,其次,在profiler 界面中选择可debug的进程,如下图所示:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
选择cpu ,双击,如下所示:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
也可以自由配置记录条件,选择不同缓存大小和周期时间us。

选择需要记录的trace类型,然后在app中使用流程,点击stop 。比如,打开一个视频页面,想了解其播放过程的耗时,如下所示:

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
从top down 中可清楚看到,视频页面中播放视频testPlay()的调用时间。

4.插桩使用Debug#trace api 精准记录方法执行时间

插桩方式,在release和debug 包下都可以使用,比较灵活方便。

由于存在 8MB 的缓冲区空间限制,因此 Debug API 中的 startMethodTracing(String tracePath) 方法专为时间间隔较短或难以手动启动/停止记录的场景而设计。如需设置持续时间更长的记录,请使用 Android Studio 中的性能剖析器界面。

简而言之,这种方式适合app冷启动,或者短时间内的方法记录trace。

public class TraceMethodUtils {
    /**
     * 适合于非常精确的记录,某个具体函数的耗时分析
     *
     * profiler cpu 中instrument模式,会从开始一直记录方法/cpu 使用情况,对性能影响大。
     */
    public static void  startTrack(String tracFileName){
        Debug.startMethodTracing(fixTraceFileName(tracFileName));
    }

    /**
     *  在android 5.0 以后使用,间隔时间微妙 
     *
     *  profiler cpu 中sample模式,系统间隔us 周期性记录调用栈,对性能影响小
     * @param tracFileName
     */
    public static void startSampleTrack(String tracFileName){
        //1000us=1ms 周期性记录一次
        Debug.startMethodTracingSampling(fixTraceFileName(tracFileName),0,1000);
    }
    private static String fixTraceFileName(String tracFileName){
        if (!TextUtils.isEmpty(tracFileName)){
            tracFileName=tracFileName.endsWith(".trace")?tracFileName:tracFileName+".trace";
            tracFileName=new StringBuilder().append(System.currentTimeMillis()).append("_").append(tracFileName).toString();
        }else{
            tracFileName=new StringBuilder().append(System.currentTimeMillis()).append(".trace").toString();
        }
        return tracFileName;
    }
    /**
     * 结束跟踪
     */
    public static void stopTrack(){
        Debug.stopMethodTracing();
    }
}

冷启动/界面启动的分析,推荐使用startSampleTrack()。

这里,具体分析某个方法testPlay(), 执行需要记录的方法,记录如下所示:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
以上文件在在DeviceFileExplorer 窗口中sdcard/android/data/package名目录,双击打开该trace文件,如下所示:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
使用方式:首先选择需要查看的线程,进行双击进入。因testPlay()是在主线程中执行,因此在这里选择主线程:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
Bottom UP窗口

用于观察某个方法中,调用其他若干方法的耗时占比,可更进一步筛选出耗时的具体位置,选择查看比例排前的几个方法。
比如,选中testPlay(),查看其内部的耗时方法占比:创建VideoView对象,耗时最大,3002us微妙=3ms毫秒。

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb

Flame chat 火焰图窗口

用于倒置查看某个方法中调用若干方法的查看,很直观的看到方法调用情况与耗时情况。淡黄色的往往是需要关注的方法点,可优化点也是在这里。

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
Top Down窗口

可用于查看,该方法向下调用栈,也可以清楚看到接下来代码逻辑走向。也是按耗时排列。

Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb
使用总结: 打开trace文件后–>双击选择线程–>选择观察的方法–>Top Down窗口细化查看耗时方法–>Bottom UP窗口找到耗时所在点。

Trace 存储路径和sdcard 权限问题

简单来看下trace的保存路径逻辑:
Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API),Android性能优化,android,adb

发现,trace 是默认保存在sdcard/android/data/package名目录或者sdcard根目录中,因此必须要有sdcard读写权限。trace会自动补全.trace后缀名。

官方资料:https://developer.android.com/studio/profile/record-traces?hl=zh-cn#debug-api

资料参考文章来源地址https://www.toymoban.com/news/detail-609246.html

  • Android Studio 中 CPU Profiler 系统性能分析工具的使用:https://juejin.cn/post/7098548053136637983
  • https://juejin.cn/post/7024387231104106503
  • 基于AOP思想的Android方法耗时开源分析工具TimeTracer:https://www.paincker.com/time-tracer/
  • https://jishuin.proginn.com/p/763bfbd4c428
  • https://zhuanlan.zhihu.com/p/508526020
  • https://juejin.cn/post/7163522788781719559
  • 性能优化:https://www.jianshu.com/p/837ad55f3049

到了这里,关于Android 耗时分析(adb shell/Studio CPU Profiler/插桩Trace API)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android使用adb命令查看CPU信息

    Android使用adb命令查看CPU信息 在开发和调试Android应用程序的过程中,了解设备的硬件信息是非常重要的。而其中一个关键信息就是设备的CPU信息。通过使用adb命令,我们可以轻松地查看Android设备的CPU信息。本文将介绍如何使用adb命令来查看CPU信息,并附上相应的源代码。 首

    2024年02月07日
    浏览(32)
  • android 通过adb shell命令旋转Android屏幕朝向

    注意: 默认0有的为横向,有的为纵向 纵向返回结果: cur 的值 宽 短 x 高 长 init=1080x1920 420dpi cur=1080x1920 app=1080x1794 rng=1080x1017-1794x1731 横向返回结果: cur 的值 宽 长 x 高 短 init=1080x1920 420dpi cur=1920x1080 app=1794x1080 rng=1080x1017-1794x1731

    2024年02月11日
    浏览(68)
  • Android - adb shell (查找文件 find )

    默认adb shell find . -name 文件名这样查找文件位置无法使用,如下。 记录下两种方法。 1、adb shell 进入 效果如下: 2、通过 busybox 查找文件 1)首先下载 BusyBox,下载地址为 Index of /downloads/binaries,记得选择对应设备架构的版本。adb shell cat /proc/cpuinfo 2)adb push 本地路径/busybox-

    2024年01月16日
    浏览(37)
  • Android adb shell svc 知识详解

    这个命令可以用来控制电源管理,wifi开关,数据开关(就是上网流量),注意需要root权限。 有兴趣的可以一起学习一下。 一、svc 常用命令: 其实还有其他svc 命令,如果想深入了解可以往后看看。 查看系统是否安装了svc 路径 /system/bin/svc 如果存在这个 svc 文件,那就就可以执

    2024年02月09日
    浏览(34)
  • Android adb shell命令捕获systemtrace

    Android adb shell命令捕获systemtrace   (1)抓取trace文件: -t    时长,20s,20秒的trace文件。 -o   保存文件路径。     (2)把trace文件从手机中pull拉取到电脑桌面:       (3)在谷歌官网: Perfetto UI 打开trace文件:       Android ADB(Andorid Debug Bridge)调试真机设备_adb在线执行器_zhang

    2024年02月09日
    浏览(44)
  • android 通过adb shell命令旋转Android屏幕朝向方向

    注意: 默认0有的为横向,有的为纵向 纵向返回结果: cur 的值 宽 短 x 高 长 init=1080x1920 420dpi cur=1080x1920 app=1080x1794 rng=1080x1017-1794x1731 横向返回结果: cur 的值 宽 长 x 高 短 init=1080x1920 420dpi cur=1920x1080 app=1794x1080 rng=1080x1017-1794x1731

    2024年02月06日
    浏览(48)
  • Android系统 adb shell auth授权使用

    adb shell是Android开发者常用的一个工具,它可以让我们在电脑上通过USB或网络连接到Android设备,并执行一些命令或操作。但是,有时候我们可能不想让任何人都能随意使用adb shell,而是需要一些授权或验证的机制,以保护我们的设备和数据。本文将介绍如何在基于rockchip rk35

    2024年02月07日
    浏览(32)
  • 通过adb shell 设置android时间(时区)同步

    设置时间同步,时区设置为上海 进入adb shell环境。在计算机上打开终端或命令提示符,并运行以下命令: adb shell 获取root权限(如果设备已经root)。在adb shell环境中运行以下命令:su 启用自动时间同步和自动时区同步。运行以下命令以启用设备的自动时间和自动时区同步:

    2024年02月07日
    浏览(40)
  • adb修改android系统时间 adb shell date必须要root权限

    以下是一个示例代码,展示如何实现这个格式化: 请注意, GetSystemTime 函数获取的是 GMT 时间,所以如果你需要的是本地时间的毫秒数,应该使用 GetLocalTime 函数替换 GetSystemTime 。同时,这种方法假定系统时区设置是正确的。如果系统时区设置不正确,计算出的时间可能会有

    2024年02月02日
    浏览(40)
  • adb shell date 命令修改Android系统时间

    1.关掉自动时间同步 settings put global auto_time 0 2.关掉自动时区同步 settings put global auto_time_zone 0 3.挂载 adb remount 4.adb shell date “时间格式”    日期格式为 yyyymmdd.hhmmss 或者是 yyyy-mm-dd hh:mm:ss adb shell date \\\"2023-12-05 10:00:00\\\" 5.查看时间是否设置成功 adb shell date 6.设置时区。先将自动

    2024年02月01日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包