google 官方教程學(xué)習(xí)之UI性能優(yōu)化( Analyzing UI Performance with Systrace)

更多分享請看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.

  • 在開發(fā)你的應(yīng)用時,你應(yīng)該檢查用戶交互時時候平滑流暢,界面是否以每秒60幀的速度刷新。 如果出現(xiàn)問題,并且?guī)粊G棄,解決問題的第一步就是了解系統(tǒng)正在做什么。

  • 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工具可以允許你去收集并查看每個時間段內(nèi)整個Android設(shè)備的信息,這稱為跟蹤。 它顯示某個點(diǎn)花費(fèi)的時間和CPU執(zhí)行的周期時間,顯示每個線程和進(jìn)程在任何時間內(nèi)正在做什么。 它還可以查看捕獲到的跟蹤信息并且突出顯示從列表項目回收到呈現(xiàn)內(nèi)容的問題,提供有關(guān)如何解決這些問題的建議。 本文檔介紹如何使用工具生成的跟蹤文件,并使用它們來分析應(yīng)用程序用戶界面(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可幫助你分析應(yīng)用程序的執(zhí)行情況以及如何適配Android設(shè)備上正在運(yùn)行的不同版本的系統(tǒng)。 它系統(tǒng)和應(yīng)用程序線程放在公共時間軸上的一起執(zhí)行。 為了使用Systrace分析你的應(yīng)用,你首先收集應(yīng)用程序的跟蹤日志和系統(tǒng)活動。 生成的跟蹤允能夠讓你查看到非常詳細(xì)在系統(tǒng)中跟蹤的持續(xù)時間內(nèi)發(fā)生的所有事件的交互式報告。

![Uploading System Trace_138631.png . . .]](http://upload-images.jianshu.io/upload_images/1811893-60c7a5586a4decba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

  • 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.
  • 上圖顯示在滾動未平滑渲染的應(yīng)用程序時捕獲的跟蹤信息。 默認(rèn)情況下,顯示跟蹤的持續(xù)時間的縮小視圖。 水平軸是時間,跟蹤事件按進(jìn)程分組,然后通過垂直軸上的線程進(jìn)行分組。

  • 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.
    分組分別按照內(nèi)核,SurfaceFlinger(Android合成器進(jìn)程)的順序,后面是應(yīng)用程序,每個都用程序包名稱做標(biāo)記。 每個應(yīng)用進(jìn)程都包含其內(nèi)部的每個線程的所有跟蹤信號,包括一個基于開啟的跟蹤類別的高級跟蹤事件的層次結(jié)構(gòu)。

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.

  • 為了創(chuàng)建一份你的應(yīng)用程序的追蹤,你必須執(zhí)行幾個安裝步驟。第一,你必須有一個運(yùn)行Android4.1(API16)或者更高的設(shè)備, 設(shè)置調(diào)試設(shè)備,將它連接到你的開發(fā)系統(tǒng)并安裝你的應(yīng)用程序。 某些類型的跟蹤信息,特別是磁盤活動和內(nèi)核工作隊列,要求您具有對設(shè)備的root訪問權(quán)限。 但是,大多數(shù)Systrace日志數(shù)據(jù)只需要開啟該設(shè)備進(jìn)行開發(fā)調(diào)試。

  • Systrace跟蹤可以從命令行或圖形用戶界面運(yùn)行。 該指南主要使用命令行選項。


Tracing on Android 4.3 and higher


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

  • 在Android4.3和更高的版本的設(shè)備上運(yùn)行一個追蹤命令。

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

  • 確保設(shè)備通過USB線連接并且開啟了調(diào)試模式

1、在build.gradle文件中設(shè)置
android {
buildTypes {
debug {
debuggable true
}
2、在AndroidManifest.xml中在<application>節(jié)點(diǎn)增加
android:debuggable="true"
注意:在應(yīng)用發(fā)布時需一處調(diào)試配置。

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

  • 使用你喜歡的形式運(yùn)行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.

  • 在設(shè)備上,執(zhí)行你想要包含在跟蹤中的任何用戶操作。

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

  • 有關(guān)運(yùn)行Systrace的可用選項的更多信息,請參閱Systrace幫助頁面。


Analyzing a Trace 分析追蹤的數(shù)據(jù)

  • 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.
  • 之后你獲得一份生成的追蹤數(shù)據(jù),使用瀏覽器打開輸出的html文件。本節(jié)將介紹如何分析和解釋該工具生成的信息以查找和修復(fù)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.

  • 渲染幀的每個應(yīng)用程序都顯示一行畫面圓,通常顏色為綠色。 顏色為黃色或紅色的圓圈,超過維持每秒60幀穩(wěn)定所需的16.6毫秒運(yùn)行時間限制。 使用“w”鍵放大顯示你的應(yīng)用程序的畫面,并尋找長時間運(yùn)行的畫面,采取措施使其運(yùn)行平滑。

System Trace.png
  • 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.

  • 如上圖,點(diǎn)擊一個圓形圖以突出顯示它,這樣操作后能僅關(guān)注系統(tǒng)為該畫面完成的工作。在運(yùn)行Android 5.0(API 21)或者更高版本的設(shè)備中,該工作按UI 線程和渲染線程分開顯示。在之前的版本中,所有畫面的創(chuàng)建工作都在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.

  • 點(diǎn)擊畫面的各個組件去查看運(yùn)行耗時多少。一些事件,如performTraversals描述當(dāng)你選擇它時,系統(tǒng)正在做什么。選擇一個畫面將顯示在該畫面中存在的任何警報。


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自動分析了跟蹤中的事件,并突出顯示作為警報的許多性能問題且提示接下來要做什么。
圖3.png
  • 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循環(huán)和重新綁定中做的工作太多。 有跟蹤中的相關(guān)事件的鏈接,可以跟蹤,以解釋更多關(guān)于系統(tǒng)在這段時間內(nèi)正在做什么。

  • 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線程上進(jìn)行,就像在這種情況下使用此ListView工作一樣,你可以使用應(yīng)用程序代碼分析工具Traceview來調(diào)查正在花費(fèi)的時間。

  • 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.

  • 注意,你也可以通過點(diǎn)擊窗口最右側(cè)的Alerts 選項卡來查找跟蹤中的每個警報。 這樣做會擴(kuò)展“警報”面板,您可以在其中看到工具在跟蹤中發(fā)現(xiàn)的每個警報,以及發(fā)生次數(shù)。

System Trace.png
  • 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!
  • 該警告頁面可以幫助你去查看跟蹤中出現(xiàn)的問題以及它們對jank貢獻(xiàn)的頻率。 可以將警報頁面看作要修復(fù)的bug列表,通常在一個區(qū)域中的微小更改或改進(jìn)可以消除你應(yīng)用中的一整類警報!

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.

  • 由框架定義的跟蹤信號不具備你的應(yīng)用所做任務(wù)的可見性,因此你可能需要添加自己的信息。 在Android 4.3(API18)及更高版本中,你可以使用Trace類的方法向您的代碼添加信號。 這種技術(shù)可以讓你看到應(yīng)用的線程在任何時間正在做什么。 跟蹤開始和結(jié)束事件確實(shí)會增加開銷,同時捕獲跟蹤,每個微秒,但每幀插入幾個,或每個工作線程任務(wù)可以很大的方式為您的應(yīng)用程序的跟蹤添加上下文。

  • 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類跟蹤應(yīng)用程序方法的執(zhí)行,包括該方法中的兩個嵌套代碼塊。

    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.
注意:當(dāng)你在彼此之間嵌套跟蹤調(diào)用時,endSection()方法將結(jié)束最近稱為beginSection(String)的方法。 這意味著在另一個跟蹤中啟動的跟蹤不能超出封閉跟蹤的結(jié)尾,因此請確保你的開始和結(jié)束方法調(diào)用已正確匹配,以測量應(yīng)用程序處理。

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.
跟蹤必須在同一個線程上開始和結(jié)束。 不要在一個執(zhí)行線程上調(diào)用beginSection(String),然后嘗試在另一個線程上調(diào)用endSection()來結(jié)束跟蹤。

  • 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

  • 應(yīng)用程序級中使用Systrace進(jìn)行跟蹤時,必須在用戶界面中指定應(yīng)用程序的包名稱或者在命令行中指定-a或--app = options。 有關(guān)詳細(xì)信息,請參閱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.

  • 在對你的應(yīng)用進(jìn)行概要分析時你應(yīng)該開啟應(yīng)用級跟蹤,即使您沒有自己添加信號。 當(dāng)您啟用應(yīng)用程序級跟蹤時,庫代碼可以包含非常有用的跟蹤信號。 RecyclerViewclass是一個很好的例子,它提供了有關(guān)它執(zhí)行的幾個重要工作階段的信息。
    更多分享請看http://www.cherylgood.cn

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,702評論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,615評論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,606評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,044評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,826評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,227評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,307評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,447評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,992評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,807評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,001評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,550評論 5 361
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,243評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,667評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,930評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,709評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,996評論 2 374

推薦閱讀更多精彩內(nèi)容