Android 性能优化(26)*性能工具之「Batterystats,Battery Historian」Batterystats & Battery Historian Walkthrough

Batterystats & Battery Historian Walkthrough

  Working with Batterystats & Battery Historian

  Battery Historian Charts

  You should also read


1.1 简介

  This walkthrough shows the basic usage and workflow for the Batterystats tool and the Battery Historian script.

  Batterystats collects battery data from your device, and Battery Historian converts that data into an HTML visualization that you can view in your Browser. Batterystats is part of the Android framework, and Battery Historian is open-sourced and available on GitHub at

 Batterystats用来收集电池健康数据。它是android 5.0以后内置的一个服务。
 Battery Historian 用来把收集到的数据转换成html,它是个开源项目,请看:

  What it's good for:

  • Showing you where and how processes are drawing current from the battery.
  • Identifying tasks in your app that could be deferred or even removed to improve battery life.

1.2 要求

  rerequisites 使用本工具的前提是5.0以上

1.3 使用步骤

    1. Download the open-source Battery Historion Python script from GitHub (
      下载 Battery Historion Python
    2. Unzip the file to extract the Battery Historian folder. Inside the folder, find the file and move it to the Desktop or another writable directory.
      解压后,拷贝 scripts/ 到一个有写权限的目录,如~/Android/batterystats
    3. Connect your mobile device to your computer.
    4. On your computer, open a Terminal window.
    5. Change to the directory where you've saved
      for example: cd ~/Android/batterystats
      进入到 所在目录。
    6. Shut down your running adb server. 
      停止adb server
      $adb kill-server
    7. Restart adb and check for connected devices. 
      $adb devices
      If you don't see any devices, make sure your phone is connected, and USB Debugging is turned on, and then kill and restart adb.
    8. Reset battery data gathering. 
      $adb shell dumpsys batterystats --reset

      Resetting erases old battery collection data; otherewise, the output will be huge.

    9. Disconnect your device from your computer so that you are only drawing current from the device's battery.
    10. Play with your app for a short time.
    11. Reconnect your phone.
    12. Make sure your phone is recognized: > adb devices
      确保通过$adb devices能看到你的设备。
    13. Dump all battery data. This can take a while: 
      $adb shell dumpsys batterystats > ~/Android/batterystats/batterystats.txt
    14. Create a HTML version of the data dump for Battery Historian: 
      $python batterystats.txt > batterystats.html
    15. Open the batterystats.htm file in your browser.
    16. Continue with Battery Historian Charts.
> cd ~/Android/batterystats
> adb kill-server
> adb devices
> adb shell dumpsys batterystats --reset
<disconnect and play with app>...<reconnect>
> adb devices
>adb shell dumpsys batterystats > ~/Android/batterystats/batterystats.txt
> python batterystats.txt > batterystats.html

2.Battery Historian Charts

2.1 简介 

  The Battery Historian chart graphs power-relevant events over time.

  Each row shows a colored bar segment when a system component is active and thus drawing current from the battery. The chart does not show how much battery was used by the component, only that the app was active. Charts are organized by category.

                Figure 1. Example of Battery Historian output.

2.2 各字段的含义 

  • battery_level: When the battery level was recorded and logged. Reported in percent, where 093 is 93%. Provides an overall measure of how fast the battery is draining.
  • top: The application running at the top; usually, the application that is visible to the user. If you want to measure battery drain while your app is active, make sure it's the top app. If you want to measure battery drain while your app is in the background, make sure it's not the top app.
  • wifi_running: Shows that the Wi-Fi network connection was active.
  • screen: Screen is turned on.
  • phone_in_call: Recorded when the phone is in a call.
  • wake_lock: App wakes up, grabs a lock, does small work, then goes back to sleep. This is one of the most important pieces of information. Waking up the phone is expensive, so if you see lots of short bars here, that might be a problem.
  • running: Shows when the CPU is awake. Check whether it is awake and asleep when you expect it to be.
  • wake_reason: The last thing that caused the kernel to wake up. If it's your app, determine whether it was necessary.
  • mobile_radio: Shows when the radio was on. Starting the radio is battery expensive. Many narrow bars close to each other can indicate opportunities for batching and other optimizations.
  • gps: Indicates when the GPS was on. Make sure this is what you expect.
  • sync: Shows when an app was syncing with a backend. The sync bar also shows which app did the syncing. For users, this can show apps where they might turn syncing off to save battery. Developers should sync as little as possible and only as often as necessary.

  Note: Not every chart will show every category.


2.3 一些在上述html中未显示信息

  You can gather additional information from the batterystats.txt file where you saved the output from the batterystats command.

 batterystats.txt 显示的是全部数据。batterystats.html显示的只是它分析时所需的数据。



          Figure 2.Example of filtered batterystats output.

  Open the file in a text editor and search for:

  • Battery History: A time series of power-relevant events, such as screen, Wi-Fi, and app launch. These are also visible through Battery Historian.
  • Per-PID Stats: How long each process ran.
  • Statistics since last charge: System-wide statistics, such as cell signal levels and screen brightness. Provides an overall picture of what's happening with the device. This information is especially useful to make sure no external events are affecting your experiment.
  • Estimated power use (mAh) by UID and peripheral: This is currently an extremely rough estimate and should not be considered experiment data.
  • Per-app mobile ms per packet: Radio-awake-time divided by packets sent. An efficient app will transfer all its traffic in batches, so the lower this number the better.
  • All partial wake locks: All app-held wakelocks, by aggregate duration and count.



posted @ 2016-05-01 23:29 g3r 阅读(...) 评论(...) 编辑 收藏