If you haven’t already read my article about battery optimisation, battery-historian is another useful tool for analyzing Android power consumption.

Before Android5.0, testing Battery consumption in an app was cumbersome and inaccurate, but since then an API has been introduced to retrieve information about Battery consumption on your device :Battery Historian. Battery Historian is an Android Battery Historian provided by Google. It is a graphical data analysis tool that visualises the Battery consumption process of your mobile phone, as well as providing useful information on how to optimize Battery life by inputting a Battery Historian as an application.

Data preparation

Battery-historian requires battery History in Bugreport

  • Adb kill-server this step is important because we open a lot of things that can cause conflicts when we do battery logging during development. Let’s restart ADB just to be on the safe side. Adb Devices will automatically connect to find the phone. Adb start-server is also available

  • 2. Reset battery data collection data. At the beginning, we need to open the battery data acquisition and reset by the following command:

    adbshelldumpsysbatterystats--enable full-wake-history
    
    adbshelldumpsysbatterystats--reset
    Copy the code

The execution effect is as follows:


image.png



test

adb bugreport> bugreport.txt
Copy the code

Or you can use the following command:

adb shell dumpsysbatterystats> batterystats.txt
adb shell dumpsysbatterystats>com.example.android.sunshine.app> batterystats.txt
Copy the code

Adding the package name limits the output data that we want to detect. But this TXT data is not very readable. The next thing we need is the battery-historian.

Here we have two ways of analyzing this file :(historian- previous version V1 and historian-V2 latest version)

Valid -V1 previous version analysis method:

To convert TXT files into HTML files, run the following command:

python historian.py -a bugreport.txt> battery.html
Copy the code

The script above is written in Python, so you need a Python environment, and download it from Github. The file is located in the scripts directory on Github. You need to download it to the directory where the command line resides


image.png

After the above two commands are successfully executed, two files bugreport. TXT and battery.




image.png

Meaning of each parameter

First, we’ll find something in the Battery History data bar in bugreport. TXT that looks like the following:

  -------------------------------------------------------------------------------

  DUMPOFSERVICEbatterystats:

  BatteryHistory (2% used, 5980 used of256KB, 45 strings using 2592):

  0 (9) RESET:TIME: 2015-03-05-15-21-56

  0 (2) 100 c0900422 status=discharging health=good plug=none temp=200       volt=4167 +running +wake_lock +sensor +screen data_conn=edge phone_signal_strength=great brightness=medium proc=u0a15:"android.process.acore"

  0 (2) 100 c0900422 proc=u0a7:"com.android.cellbroadcastreceiver"

  0 (2) 100 c0900422 proc=u0a53:"com.android.gallery3d"
Copy the code

All information in HTML can be found in bugreport.txt.

Now let’s analyze the meaning of each indicator:

  • abscissa

    image.png

The number 10 and 20 above is the second, which is a one-minute cycle that goes to zero at 60 seconds. The horizontal coordinate is a time range. In our example, the statistical data starts from reset and ends at the time when the bugreport content is obtained. How long have we collected the data? There is also information under the chart. (According to feedback from others, this coordinate interval can change over time, so it depends on your actual situation. The zoom level can be adjusted as shown below 🙂


image.png

  • Battery_level indicates the change in battery charge. For example, the data above shows that the battery starts at 100% and drops to 99% sometime between 11 and 12 seconds.


image.png

  • Plugged Charging status, which shows whether the battery has been plugged in and the time range for charging. For example, the figure above reflects that we inserted the data line at 22s, and then continued until the end of data collection.

  • Whether the screen is on or not can take into account the sleep state and the power usage information under the lit state.

  • The column of Top displays which app is at the top at the current moment, which is the app currently running on the mobile phone. It is used to judge the influence of an app on the mobile phone power, and thus the power consumption information of the app can also be determined. This column records when an application was started and how long it was running, which is a great help in comparing the performance impact of different applications.

  • Wake_lock wake_lock This property records the working time of the wake_lock module. Whether there is a time to stop etc

  • Running Indicates whether the interface is in idle state. It is used to judge the power consumption under no operation state.

  • Wake_lock_in Wake_lock has different components. This is the place that records what components are working at a certain time and for what time.

  • GPS Indicates whether GPS is enabled

  • Phone_in_call Specifies whether to make a call

  • Sync Indicates whether to synchronize data with the background. You can hover your mouse over an item. You can see when sync is started and how long the Duration is. The battery capacity doesn’t show how much power is consumed by a single action, it shows how often and for how long the battery is used, and you can look at the amount of power left over a period of time to see how much power is consumed.

    image.png

  • Job Background work, such as running a service. Qihoo’s AppStore and Master Lu are both running background services in the image below.

    image.png

  • Data_conn Data connection mode change, the above edge is to illustrate the use of GPRS mode to connect to the network. The data shows whether the phone is using 2G, 3G, 4G or wifi for data exchange. This column shows the impact of different connection methods on power usage.

  • Status Indicates the battery status, including charging, discharging, uncharged, full, and unknown. This column records information about battery status changes.

  • Phone_signal_strength Change of cell phone signal status. This column records the strength and weakness of mobile phone signals to judge the influence of mobile phone signals on electric quantity in turn.

  • Health Indicates the health status of the battery. This information partly reflects how long the battery has been used. This column records when the battery state changes. In the diagram above, the battery state is always good.

  • Plug charging method, USB or socket, and display the time of connection. This column shows how different charging methods affect power usage.

Valid -V2 latest version of the way:

The bugreport.txt file will be generated in [http://localhost:9999](javascript:goto();) To upload files and generate reports (as long as you have configured the battery-historian environment locally or on a server)


image.png

There are two versions of version analysis:


image.png

Well, battery-historian as an analysis tool, if you’re interested, please check it out!

I do Android development for many years, later will be updated on android advanced UI,NDK development, performance optimization and other articles, more please pay attention to my wechat public number: thank you!


The Android thing. JPG