使用 Batterystats 和 Battery Historian 剖析電池用量

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

本逐步操作說明 Batterystats 工具與 Battery Historian 令碼的基本設定步驟和工作流程。如要瞭解如何使用 Battery Historian 檢查電池消耗模式,請參閱「使用 Battery Historian 分析電力使用情形」一文。

Batterystats 是 Android 架構中的一種工具,可收集裝置中的電池相關資料。您可以使用 adb,將收集到的電池資料傾印至開發機器,然後建立可使用 Battery Historian 分析的報表。Battery Historian 會將報表從 Batterystats 轉換成 HTML 視覺效果,方便您在瀏覽器中查看。

適用場合:

  • 顯示程序在何處及如何從電池提取電力。
  • 找出應用程式中可延遲或甚至移除的工作,藉以提升電池續航力。

安裝 Battery Historian

安裝 Battery Historian 最簡單的方法,就是使用 Docker。如需其他安裝方式 (包括從原始碼開始建構),請參閱專案 GitHub 頁面中的「Read Me」。如要使用 Docker 進行安裝,請執行下列步驟:

  1. 按照 Docker 網站上的操作說明安裝 Docker Community Edition。

  2. 如要確認 Docker 已正確安裝,請開啟指令列並輸入下列指令:

    docker run hello-world
    

    如果已正確安裝 Docker,就可以看到如下所示的輸出內容:

    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    78445dd45222: Pull complete
    Digest:
    sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    
    To generate this message, Docker took the following steps:
    1. The Docker client contacted the Docker daemon.
    2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
    4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal
    
  3. 使用下列指令執行 Battery Historian 圖片:

    docker run -p port_number:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999
    

    Battery Historian 會使用您選擇的通訊埠,如使用 port_number 所指定。

  4. 在瀏覽器中前往 Battery Historian 頁面,確認裝置正在執行。 位址會因您的作業系統而異:

    Linux 和 Mac

    Battery Historian 位於 http://localhost:port_number

    For Windows

    啟動 Docker 後,系統會指出電腦使用的 IP 位址。舉例來說,如果 IP 位址是 123.456.78.90,代表 Battery Historian 位於 http://123.456.78.90:port_number
    您可以上傳及查看電池統計資料。

    Battery Historian 的起始網頁
    圖 1. Battery Historian 的起始網頁

使用 Batterystats 收集資料

如要使用 Batterystats 從裝置收集資料,然後在 Battery Historian 中開啟,請執行以下操作:

  1. 連接行動裝置至電腦。

  2. 在終端機視窗中,關閉執行中的 adb 伺服器。

    adb kill-server
    
  3. 重新啟動 adb,然後檢查是否有已連接的裝置。

    adb devices
    

    畫面中應會列出您的裝置,與以下範例的輸出內容類似。

    adb 裝置的輸出內容
    圖 2. adb devices 的輸出內容,顯示有一部連接的裝置

    如果沒有看到任何裝置,請確認手機已經連接,且「USB 偵錯」已經開啟,然後清除並重新啟動 adb。

  4. 重設電池資料收集。

    adb shell dumpsys batterystats --reset
    

    裝置一律會在背景中收集 Batterystats 和其他偵錯資訊。重設會清除舊的電池收集資料。如果沒有重設,輸出內容會非常龐大。

  5. 中斷裝置與電腦的連線,這樣裝置就只會從裝置的電池中取得電力。

  6. 使用應用程式並執行所需的資料操作,例如中斷 Wi-Fi 連線,並傳送資料至雲端。

  7. 重新連接手機。

  8. 確認手機已經辨識:

    adb devices
    
  9. 捨棄所有電池資料。這可能需要一段時間:

    adb shell dumpsys batterystats > [path/]batterystats.txt
    

    batterystats.txt 檔案會使用選用路徑引數,在您指定的目錄中建立。如果沒有指定路徑,檔案就會在主目錄中建立。

  10. 使用原始資料建立報表。

    執行 Android 7.0 以上版本的裝置:

    adb bugreport [path/]bugreport.zip

    執行 Android 6.0 以上版本的裝置:

    adb bugreport [path/]bugreport.txt

    Bugreport 可能需要幾分鐘才能完成。完成之前請勿中斷裝置的連線或取消程序。

    和上述的 batterystats.txt 相同,這些檔案是使用選用的 path 引數,在您指定的目錄中建立。如果沒有指定路徑,則路徑會在主目錄中建立。

    如果還沒有執行 Battery Historian,請使用以下指令執行 Battery Historian:

    docker --run -p port_number:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999
    
  11. 如要在 Battery Historian 中檢視資料,請在瀏覽器中開啟 Battery Historian。(如果是 Mac 和 Linux,Battery Historian 會在 http://localhost:port_number 執行。如果是 Windows,Battery Historian 會在 http://your_IP_address:port_number 執行。)

  12. 按一下「Browse」(瀏覽),然後選擇您在上述步驟中建立的 bugreport 檔案。

  13. 按一下「Submit」(提交)。Battery Historian 會開啟使用 Batterystats 資料建立的圖表。

使用 Battery Historian 圖表檢視資料

Battery Historian 會以圖表方式顯示一段時間內的電力相關事件。

當系統元件處於啟動狀態並因此從電池提取電力時,每一列都會顯示色條的區隔。此圖表「不會」顯示元件已使用多少電力,只會顯示應用程式處於啟用狀態。圖表會按照類別分類,並隨時間顯示每個類別的長條,如圖表的 X 軸所示。

Battery Historian 圖表範例
圖 3. Battery Historian 圖表範例

  1. 在下拉式清單中選擇新增其他指標。
  2. 將滑鼠游標懸停在資訊圖示上,即可查看每個指標的詳細資訊,包括圖表中使用的顏色索引鍵。
  3. 將游標懸停在長條上,即可查看該指標在時間軸上特定時間點的詳細資料,以及和電池統計資料。

其他 Batterystats 輸出內容

您可以在 Battery Historian 圖表下方的統計資料部分中查看 batterystats.txt 檔案的其他相關資訊。

Battery Historian 的統計資料部分
圖 4. Battery Historian 的統計資料部分

1「System Stats」(系統統計資料) 分頁包含整個系統的統計資料,例如:儲存格信號等級和螢幕亮度。此資訊可協助您全面掌握裝置的運作情況。這對確保不會有其他事件影響測試特別有幫助。

2「App Stats」(應用程式統計資料) 分頁包含特定應用程式的相關資訊。使用左側「App Selection」(應用程式選項) 窗格中的 3「Sort apps by」(應用程式排序依據) 下拉式選單,排序應用程式的清單。您可以使用 4 下方的應用程式下拉式選單,選取特定應用程式以檢視統計資料。