Profile battery usage with Batterystats and Battery Historian

    Batterystats is a tool included in the Android framework that collects battery data on your device. You can use to dump the collected battery data to your development machine and create a report you can analyze using Battery Historian. Battery Historian converts the report from Batterystats into an HTML visualization that you can view in your browser.

    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.
      Note: To use Batterystats and Battery Historian, you need a mobile device running Android 5.0 or higher with USB Debugging enabled.

    The easiest way to install Battery Historian is to use Docker. For alternate installation methods, including building from source, see on the project's GitHub page. To install using Docker, do the following:

    • Install Docker Community Edition following the instructions on the Docker website.
    • To confirm that Docker is correctly installed, open the command line and enter the following command:

    If Docker is correctly installed, you will see output like this:

    1. Unable to find image 'hello-world:latest' locally
    2. latest: Pulling from library/hello-world
    3. 78445dd45222: Pull complete
    4. Digest:
    5. sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
    6. Status: Downloaded newer image for hello-world:latest
    7.  
    8. Hello from Docker!
    9. This message shows that your installation appears to be working correctly.
    10.  
    11. 1. The Docker client contacted the Docker daemon.
    12. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    13. 3. The Docker daemon created a new container from that image which runs the
    14. executable that produces the output you are currently reading.
    15. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
    • Run the Battery Historian image using the following command:
    1. docker run -p port_number:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999

    Battery Historian will use the port of your choice, as specified using .

    • Navigate to Battery Historian in your browser to confirm that it is running. The address varies depending on your operating system:
      • For Linux and Mac
      • Battery Historian is available at :port_number.
      • For Windows
      • Once you start Docker, it tells you the IP address of the machine it is using. So, for example, if the IP address is 123.456.78.90, Battery Historian is available at http://123.456.78.90:port_number
        You will see the Battery Historian start page, where you can upload and view battery statistics.

        Figure 1. The start page for Battery Historian

    To collect data from your device using Batterystats and open it in Battery Historian, do the following:

    • Connect your mobile device to your computer.
    • From a Terminal window, shut down your running adb server.
    • Restart adb and check for connected devices.
    1. adb devices

    You should see your device listed, similar to the example output below.
    Profile battery usage - 图1
    Figure 2. The output of , showing one connected device

    If you don't see any devices, make sure your phone is connected, and is turned on, and then kill and restart adb.

    • Reset battery data gathering.
    1. adb shell dumpsys batterystats --reset

    The device is always collecting batterystats and other debugging information in the background. Resetting erases old battery collection data. If you do not reset, the output will be huge.

    • Play with your app and perform actions for which you would like data; for example, disconnect from WiFi and send data to the cloud.
    • Reconnect your phone.
    • Make sure your phone is recognized:
    • Dump all battery data. This can take a while:
    1. adb shell dumpsys batterystats [path/]batterystats.txt

    The batterystats.txt file is created in the directory you specify using the optional path argument. If you do not specify a path, the file is created in your home directory.

    • Create a report from raw data.
      • For devices running Android 7.0 and higher:
    1. adb bugreport > [path/]bugreport.zip
    • For devices running Android 6.0 and lower:

    As with batterystats.txt above, these files are created in the directory you specify using the optional path argument. If you do not specify a path, they are created in your home directory.

    • If it's not already running, run Battery Historian using the following command:
    1. docker --run -p port_number:9999 gcr.io/android-battery-historian:2.1 --port 9999
    • To view your data in Battery Historian, open the Battery Historian in your browser. (For Mac and Linux, Battery Historian runs at . For Windows, Battery Historian runs at http://your_IP_address/port_number.)
    • Click Browse and then choose the bugreport file you created above.
    • Click Submit. Battery Historian will open a chart created from your batterystats data.

    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, showing a bar for each category over time, as displayed on the X-axis of the chart.

    Figure 3. Example of a Battery Historian chart

    • Add additional metrics from the dropdown list.
    • Hover over the information icons to see more information about each metric, including a key for the colors used in the chart.

    You can view additional information from the batterystats.txt file in the stats section below the Battery Historian chart.
    Profile battery usage - 图2
    Figure 4. The stats section of Battery Historian

    The 1System Stats tab includes system-wide statistics, such as cell signal levels and screen brightness. This information provides an overall picture of what's happening with the device. This is especially useful to make sure no external events are affecting your test.

    For more information about battery performance, see Optimizing Battery Life.