Test Android App Battery Consumption with Battery Historian Tool
Mobile devices comes with a limited battery life. Due to this, users would not want an app which consumes a lot of their device battery. Therefore, it’s important for testers to keep tab on their test app’s battery consumption. While the native battery analyzer in Android does a great job at this by showing apps which consumes the most battery, it still doesn’t show how much battery does an individual app consume. A specialized tool is needed to get this info. Battery Historian is one such tool. It’s a great little utility which shows Android app battery consumption details in a graphical manner.
In this article, we will see how we can use it to measure an Android app battery consumption. But before that, let’s see what is Battery Historian first.
Introducing Battery Historian
A part of Google Project Volta, it is basically an open source Python script by Google. Its sole purpose is to profile battery related events in Android. And after profiling, it provides a nice breakdown of battery performance and stats by app. You get all these details in an easy to understand, graphical manner. Apart from this, it also helps in identifying most battery hungry events in Android app.
Check out the cool features Battery Historian has to offer below.
- It works on Android device running Android 5.0 Lollipop (API level 21) and later.
- It allows an A/B comparison of two bugreports. Therefore highlighting differences in key battery related metrics.
- It works on different platforms such as Linux, Mac OS and Windows.
- Battery Historian can also track down issues related to wake locks, GPS and other battery intensive features.
Looking at the features that it provides, it’s without a doubt a great tool for testing android app battery consumption.
Now that we know a bit about Battery Historian, let’s see how we can use it. But before that, we need to ensure our system meets its prerequisites.
Prerequisites
- ADB: We will need adb to issue commands to Android devices. As you may know, it comes as a part of Android SDK.
- Python: Battery Historian is a Python script. So, we need Python installed in our system.
- On Android device: We need to enable USB debugging on our Android device. We can enable it by going to Settings–>System–>Developer Options.
Since we have all the requirements in place now, we can start using Battery Historian.
Testing Android app battery consumption
- Step 1 – Clear existing battery stats: Since we want to capture battery consumption of our app only, we need to clear existing battery stats from our device. This is because we don’t want stats other than our app’s to be included in the report. So to clear battery stats, connect your device and run below adb command on your system.
1adb shell dumpsys batterystats --reset - Step 2 – Disconnect your device from system: After clearing the battery stats, you can disconnect the mobile device from the system.
- Step 3 – Test your app: Now you can start performing the steps for which you want to test battery consumption. Try to perform scenarios which you suspect are eating a lot of battery. Also, use your application like a user would. This way, you will get a better idea of how your app affects user experience in terms of battery life.
- Step 4 – Generate a bugreport: Once done with capturing some scenarios, connect your device with your system. We will now generate a bugreport. A bugreport is basically a log file which contains lot of details about apps and system related stuff. Battery Historian uses it to get the device’s battery stats. We can capture bugreport with below adb command.
1adb bugreport > batterydata.txt - Step 5 – Generate HTML report: Now that we have bugreport with us, it’s time to put it to good use. We will use it to get Battery Historian report. Download Battery Historian and navigate to its ‘scripts’ directory. Use below command to generate HTML report from Android bugreport. You should be able to see HTML report in the directory.
1python historian.py batterydata.txt > batteryinfo.html
Checking Battery Historian report
The Battery Historian report provides a detailed information about the app. As you can see in picture below, it shows nice graphs to easily understand app states. You can also customize the graph to add/remove any metrics.
Going further down the report, you can check statistics app wise. Here you can select your app and check how much resources your app is using. It is good idea to take a look here and try to figure out possible causes of battery drain. Check out the screenshot below which shows different stats available for app.
While this tool is capable of locating your app’s battery issues, it falls short on some points. Mainly, below are some of its shortcomings.
Battery Historian limitations
- It doesn’t support Android version less than 5.0. So you are out of luck if you want to test Android app battery consumption for Android versions lower than 5.0.
- There isn’t much documentation available for this tool. So you are on your own to figure out how to make use of it.
- It isn’t much helpful in tracking down battery events over larger period of time. So it may be harder for you to use it for such scenario.
However, discounting the above limitations, it surely is a nice tool to measure Android app battery consumption. We encourage you to give it a try and share your experience with us in comments.
Happy battery testing! 🙂
Great information
I wanted to know, by using this tool what we are achieving? For the particular app if i want to check the battery consumption and to predict is it taking more battery or not, will this tool be helpful for that? Please help. Not able to figure out how this helps.
Didn’t know this tool. Thanks for sharing.
Thanks for sharing this article .It help me a lot to know about the tools.
This is an outstanding post that’s filled with so many useful nuggets. Thank you.