Battery and CPU utilization testing for Android
single,single-post,postid-17300,single-format-standard,ajax_fade,page_not_loaded,,qode-title-hidden,qode-content-sidebar-responsive,qode-theme-ver-9.5,wpb-js-composer js-comp-ver-4.11.1,vc_responsive

Battery and CPU utilization testing for Android

Why is it important to measure Battery and CPU utilization of your mobile app?

Everyday many applications are launched and everyone is using applications like shopping, games, utilities. People are using apps that are not compatible with their devices so it is important to measure the battery utilization of the application while testing it. When you get worried about more battery consumption, it’s fair to find out what is causing the drainage of the battery. So we always have to test the battery consumption when we develop the application.

CPU consumption of apps also plays a vital role in the performance of the application. If an application consumes more CPU then it degrades the performance of the phone while using the app. More the CPU consumed by the application the more the battery is consumed. CPU consumption simply tells you how much memory is used by your application.

How to measure Battery and CPU consumption of an Android application?

CPU consumption of Android application
It is much more important to find how much CPU your application is consuming as the more the CPU consumption the more the Battery is used by the application. Let’s start monitoring CPU consumption.
Setup required for this:

  • Windows 10 OS
  • Android debugger bridge (adb)
  • Android mobile

Configuring the CPU monitoring Environment
These are the steps below to monitor the CPU consumption of Android applications.

Download Android Debugger Bridge(adb)

  1. Download Android Debugger Bridge(adb) zip from the link below:
  2. Extract the zip folder into any folder. I Extracted it in “C:\adb\”
    Battery and CPU utilization testing for Android
  3. Add this folder path in Environment variable.
    Battery and CPU utilization testing for Android-adb

Set up your mobile phone to monitor CPU consumption.

  1. Open developer option of your phone by clicking 5 times on build number.
    Go to settings → About phone → Build number.
  2. Now go to developer option
    Enable the developer option.
    Enable USB debugging.
    Click on Revoke USB debugging and press OK.
    Developers option

Let’s start monitoring the CPU consumption of Android application
As you are all set so we can start the monitoring of CPU consumption. It’s easy to monitor CPU consumption. You just need to connect your phone to the desktop or laptop and start to process.

  1. Connect your phone to a desktop or laptop.
  2. Open mobile application for which you want to monitor the battery.
  3. Run command
    adb shell “top -n 1”
    Then you will see a pop up on your mobile phone, Click Allow.
  4. Find your application with its appPackage name from the list of running applications and note down the PID of your application we will use it in the next step.
    developer options
  5. Run the command below to calculate the CPU consumption of your application.
    adb shell “top -d 1 -p ‘PID of your app’ > /sdcard/performance.txt”
    Example :- adb shell “top -d 1 -p 19598 > /sdcard/performance.txt”
  6. The above command will create the performance.txt file in your device which consists of entries per second CPU consumption of the application.
  7. Run this command for 2 or 3 hours and calculate the average of CPU consumption from performance.txt file.

Let’s start with the battery consumption of the Android application
This is the main part of the application to test as if an application consumes more battery then it’s performance is low and it causes the drainage of battery so we have to test the battery consumption before releasing the app for the end-user.
Steps to test the battery consumption of Android application

  1. Charge your phone to 100% and let’s find out how much battery it is consuming.
  2. Open your application on your mobile and let it run for 2 hours in the background.
  3. After 2 hours check the percentage of battery consumption in Phone settings
  4. Settings → Battery → View detailed usage
    Battery Usages

Here you can find the percentage of battery used by your applications.

Thanks for reading this blog, if you have any doubts or query you can comment below.

Shivam Chaudhary
[email protected]

Shivam is a tenacious problem solver and student of new technologies, he has the ability to dive into unfamiliar tech and tools and achieve results. He has a clear understanding of the V Model And Agile Methodologies and skilled in Automated Software Testing.

  • Arjun bm
    Posted at 00:04h, 15 February Reply

    Hey Shivam, thanks for this awesome article. After generating the performance.txt how do i analyze the same? i mean what parameters i need to look into in that log file?

    • Shivam Chaudhary
      Posted at 11:30h, 16 February Reply

      There is a column name CPU in performance.txt file, you should calculate the average of all the values in that column. so that will be your average CPU utilization.

  • Maher Noraiz
    Posted at 16:08h, 06 June Reply

    adb shell “top -d 1 -p 19598 > /sdcard/performance.txt”

    where can I get the file performance.txt

    • Shivam Chaudhary
      Posted at 11:09h, 07 June Reply

      You will get the performance.txt at this path /sdcard/performance.txt or you can provide any folder path of mobile device to save the performance.txt

Post A Comment