google 官方教程学习之 UI 性能优化 (Analyzing UI Performance with Systrace)

本贴最后更新于 2215 天前,其中的信息可能已经事过境迁

更多分享请看 http://www.cherylgood.cn

  • While developing your application, you should check that user interactions are buttery smooth, running at a consistent 60 frames per second. If something goes wrong, and a frame gets dropped, the first step in fixing the problem is understanding what the system is doing.

  • 在开发你的应用时,你应该检查用户交互时时候平滑流畅,界面是否以每秒 60 帧的速度刷新。 如果出现问题,并且帧被丢弃,解决问题的第一步就是了解系统正在做什么。

  • The Systrace tool allows you to collect and inspect timing information across an entire Android device, which is called a trace. It shows where time and CPU cycles are being spent, displaying what each thread and process is doing at any given time. It also inpects the captured tracing information to highlight problems that it observes, from list item recycling to rendering content, and provide recommendations about how to fix them. This document explains how to navigate the trace files produced by the tool, and use them to analyze the performance of an application's user interface (UI).

  • Systrace 工具可以允许你去收集并查看每个时间段内整个 Android 设备的信息,这称为跟踪。 它显示某个点花费的时间和 CPU 执行的周期时间,显示每个线程和进程在任何时间内正在做什么。 它还可以查看捕获到的跟踪信息并且突出显示从列表项目回收到呈现内容的问题,提供有关如何解决这些问题的建议。 本文档介绍如何使用工具生成的跟踪文件,并使用它们来分析应用程序用户界面(UI)的性能。

##Overview

  • Systrace helps you analyze how the execution of your application fits into the many running systems on an Android device. It puts together system and application thread execution on a common timeline. In order to analyze your app with Systrace, you first collect a trace log of your app, and the system activity. The generated trace allows you to view highly detailed, interactive reports showing everything happening in the system for the traced duration.

  • Systrace 可帮助你分析应用程序的执行情况以及如何适配 Android 设备上正在运行的不同版本的系统。 它系统和应用程序线程放在公共时间轴上的一起执行。 为了使用 Systrace 分析你的应用,你首先收集应用程序的跟踪日志和系统活动。 生成的跟踪允能够让你查看到非常详细在系统中跟踪的持续时间内发生的所有事件的交互式报告。

  • shows a trace captured while scrolling an app that is not rendering smoothly. By default, a zoomed out view of the traced duration is shown. The horizontal axis is time, and trace events are grouped by process, and then by thread on the vertical axis.
  • 上图显示在滚动未平滑渲染的应用程序时捕获的跟踪信息。 默认情况下,显示跟踪的持续时间的缩小视图。 水平轴是时间,跟踪事件按进程分组,然后通过垂直轴上的线程进行分组。

  • The groupings are in the order Kernel, SurfaceFlinger (the android compositor process), followed by apps, each labeled by package name. Each app process contains all of the tracing signals from each thread it contains, including a hierarchy of high level tracing events based on the enabled tracing categories.
    分组分别按照内核,SurfaceFlinger(Android 合成器进程)的顺序,后面是应用程序,每个都用程序包名称做标记。 每个应用进程都包含其内部的每个线程的所有跟踪信号,包括一个基于开启的跟踪类别的高级跟踪事件的层次结构。

##Generating a Trace 生成追踪报告

  • In order to create a trace of your application, you must perform a few setup steps. First, you must have a device running Android 4.1 (API 16) or higher. Set up the device for debugging, connect it to your development system, and install your application. Some types of trace information, specifically disk activity and kernel work queues, require that you have root access to the device. However, most Systrace log data only requires that the device be enabled for developer debugging.

  • Systrace traces can be run either from a command line or from a graphical user interface. This guide focuses on using the command line options.

  • 为了创建一份你的应用程序的追踪,你必须执行几个安装步骤。第一,你必须有一个运行 Android4.1(API16)或者更高的设备, 设置调试设备,将它连接到你的开发系统并安装你的应用程序。 某些类型的跟踪信息,特别是磁盘活动和内核工作队列,要求您具有对设备的 root 访问权限。 但是,大多数 Systrace 日志数据只需要开启该设备进行开发调试。

  • Systrace 跟踪可以从命令行或图形用户界面运行。 该指南主要使用命令行选项。


##Tracing on Android 4.3 and higher

  • To run a trace on Android 4.3 and higher devices:

  • 在 Android4.3 和更高的版本的设备上运行一个追踪命令。

  • 1、Make sure the device is connected through a USB cable and is enabled for debugging.

  • 确保设备通过 USB 线连接并且开启了调试模式

    1、在 build.gradle 文件中设置

    android {
    buildTypes {
      debug {
          debuggable true
      }   
    
    

    2、在 AndroidManifest.xml 中在 节点增加

       android:debuggable="true"
    
    

    注意:在应用发布时需一处调试配置。

  • 2、Run the trace with the options you want, for example:

  • 使用你喜欢的形式运行 trace,例如:

    $ cd android-sdk/platform-tools/systrace
    $ python systrace.py --time=10 -o mynewtrace.html sched gfx view wm
    
    
  • 3、On the device, execute any user actions you want be included in the trace.

  • 在设备上,执行你想要包含在跟踪中的任何用户操作。

  • For more information on the available options for running Systrace, see the Systrace help page.

  • 有关运行 Systrace 的可用选项的更多信息,请参阅 Systrace 帮助页面。


##Analyzing a Trace 分析追踪的数据

  • After you have generated a trace, open the output html file using a web browser. This section explains how to analyze and interpret the information that the tool produces to find and fix UI performance problems.
    • 之后你获得一份生成的追踪数据,使用浏览器打开输出的 html 文件。本节将介绍如何分析和解释该工具生成的信息以查找和修复 UI 性能问题。

##Inspecting Frames 分析画面

  • Each app that is rendering frames shows a row of frame circles, which are typically colored green. Circles that are colored yellow or red, exceeding the 16.6 millisecond run time limit required to maintain a stable 60 frames per second. Zoom in using the 'w' key to see the frames of your application, and look for long-running frames getting in the way of smoothness.

  • 渲染帧的每个应用程序都显示一行画面圆,通常颜色为绿色。 颜色为黄色或红色的圆圈,超过维持每秒 60 帧稳定所需的 16.6 毫秒运行时间限制。 使用“w”键放大显示你的应用程序的画面,并寻找长时间运行的画面,采取措施使其运行平滑。

  • Clicking on one such frame highlights it, focusing only on the work done by the system for that frame. On devices running Android 5.0 (API level 21) or higher, this work is split between the UI Thread and RenderThread. On prior versions, all work in creating a frame is done on the UI Thread.

  • 如上图,点击一个圆形图以突出显示它,这样操作后能仅关注系统为该画面完成的工作。在运行 Android 5.0(API 21)或者更高版本的设备中,该工作按 UI 线程和渲染线程分开显示。在之前的版本中,所有画面的创建工作都在 UI 线程完成。

  • Click on individual components of the frame to see how long they took to run. Some events, such as performTraversals, describe what the system is doing in that method when you select it. Selecting a frame displays any alerts present in that frame.

  • 点击画面的各个组件去查看运行耗时多少。一些事件,如 performTraversals 描述当你选择它时,系统正在做什么。选择一个画面将显示在该画面中存在的任何警报。


##Investigating Alerts

  • Systrace does automatic analysis of the events in the trace, and highlights many performance problems as alerts, suggesting what to do next.
  • Systrace 自动分析了跟踪中的事件,并突出显示作为警报的许多性能问题且提示接下来要做什么。

  • After you select a slow frame such as the one shown in Figure 3, an alert may be displayed. In the case above, it calls out that the primary problem with the frame is too much work being done inside ListView recycling and rebinding. There are links to the relevant events in the trace, which can be followed to explain more about what the system is doing during this time.

  • 选择如图 3 所示的慢帧后,可能会显示警报。 在上面的情况下,它提出了该画面的主要问题是在 ListView 循环和重新绑定中做的工作太多。 有跟踪中的相关事件的链接,可以跟踪,以解释更多关于系统在这段时间内正在做什么。

  • If you see too much work being done on the UI thread, as in this case with this ListView work, you can use Traceview, the app code profiling tool, to investigate exactly what is taking so much time.

  • 如果你看到太多工作在 UI 线程上进行,就像在这种情况下使用此 ListView 工作一样,你可以使用应用程序代码分析工具 Traceview 来调查正在花费的时间。

  • Note that you can also find about every alert in the trace by clicking the Alerts tab to the far right of the window. Doing so expands the Alerts panel, where you can see every alert that the tool discovered in your trace, along with an occurrence count.

  • 注意,你也可以通过点击窗口最右侧的 Alerts 选项卡来查找跟踪中的每个警报。 这样做会扩展“警报”面板,您可以在其中看到工具在跟踪中发现的每个警报,以及发生次数。

  • The Alerts panel helps you see which problems occur in the trace, and how often they contribute to jank. Think of the alerts panel as a list of bugs to be fixed, often a tiny change or improvement in one area can eliminate an entire class of alerts from your application!
  • 该警告页面可以帮助你去查看跟踪中出现的问题以及它们对 jank 贡献的频率。 可以将警报页面看作要修复的 bug 列表,通常在一个区域中的微小更改或改进可以消除你应用中的一整类警报!

##Tracing Application Code

  • The tracing signals defined by the framework do not have visibility into everything your application is doing, so you may want to add your own. In Android 4.3 (API level 18) and higher, you can use the methods of the Trace class to add signals to your code. This technique can help you see what work your application's threads are doing at any given time. Tracing begin and end events do add overhead while a trace is being captured, a few microseconds each, but sprinkling in a few per frame, or per worker thread task can go a long way to adding context to a trace of your app.

  • 由框架定义的跟踪信号不具备你的应用所做任务的可见性,因此你可能需要添加自己的信息。 在 Android 4.3(API18)及更高版本中,你可以使用 Trace 类的方法向您的代码添加信号。 这种技术可以让你看到应用的线程在任何时间正在做什么。 跟踪开始和结束事件确实会增加开销,同时捕获跟踪,每个微秒,但每帧插入几个,或每个工作线程任务可以很大的方式为您的应用程序的跟踪添加上下文。

  • The following code example shows how to use the Trace
    class to track execution of an application method, including two nested code blocks within that method.

  • 以下代码示例显示了如何使用 Trace 类跟踪应用程序方法的执行,包括该方法中的两个嵌套代码块。

    public void ProcessPeople() {
    Trace.beginSection("ProcessPeople");
    try {
      Trace.beginSection("Processing Jane");
      try {
          // code for Jane task...
      } finally {
          Trace.endSection(); // ends "Processing Jane"
      }
    
      Trace.beginSection("Processing John");
      try {
          // code for John task...
      } finally {
          Trace.endSection(); // ends "Processing John"
      }
    } finally {
      Trace.endSection(); // ends "ProcessPeople"
    } }
    
    

Note: When you nest trace calls within each other, the endSection() method ends the most recently called beginSection(String) method. This means that a trace started within another trace cannot extend beyond the end of the enclosing trace, so make sure your beginning and ending method calls are properly matched to measure your applications processing.
注意:当你在彼此之间嵌套跟踪调用时,endSection()方法将结束最近称为 beginSection(String)的方法。 这意味着在另一个跟踪中启动的跟踪不能超出封闭跟踪的结尾,因此请确保你的开始和结束方法调用已正确匹配,以测量应用程序处理。

Note: Traces must begin and end on the same thread. Do not call beginSection(String)on one thread of execution and then attempt to end the trace with a call to endSection()on another thread.
跟踪必须在同一个线程上开始和结束。 不要在一个执行线程上调用 beginSection(String),然后尝试在另一个线程上调用 endSection()来结束跟踪。

  • When using application-level tracing with Systrace, you must specify the package name of your application in the user interface or specify the -a or --app= options on the command line. For more information, see the Systrace usage guide

  • 应用程序级中使用 Systrace 进行跟踪时,必须在用户界面中指定应用程序的包名称或者在命令行中指定-a 或--app = options。 有关详细信息,请参阅 Systrace 使用指南

  • You should enable app level tracing when profiling your app, even if you have not added signals yourself. Library code can include very useful tracing signals when you enable application-level tracing. The RecyclerViewclass is a great example of this, providing information about several important stages of work it executes.

  • 在对你的应用进行概要分析时你应该开启应用级跟踪,即使您没有自己添加信号。 当您启用应用程序级跟踪时,库代码可以包含非常有用的跟踪信号。 RecyclerViewclass 是一个很好的例子,它提供了有关它执行的几个重要工作阶段的信息。
    更多分享请看 http://www.cherylgood.cn

  • Android

    Android 是一种以 Linux 为基础的开放源码操作系统,主要使用于便携设备。2005 年由 Google 收购注资,并拉拢多家制造商组成开放手机联盟开发改良,逐渐扩展到到平板电脑及其他领域上。

    334 引用 • 323 回帖 • 4 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...