Testing Android App – How to find out the CPU & Memory utilisation of the device?

While automating Android app with appium, it might be a good practise to find the CPU & memory utilisation of the Android device after different features of the app are put to test.

So, how do we automate this process so that we can have all the data to analyse in the logs.

Before we get onto the actual script, let’s write a pseudo code.

1> Find the CPU & Memory utilised in the Android device – Should be normal.

2> Open the app under test.

3> Perform task 1 with the app.

4> Find the CPU & Memory utilised in the device.

5> Perform task2 with the app.

6>  Find the CPU & Memory utilised in the device.

7> Perform task3 with the app.

8>  Find the CPU & Memory utilised in the device.

In the above script, finding the CPU & Memory utilised is called multiple times. So, makes sense to write a function for it & call it in the script.

Writing a python script for it. Here I’m assuming the scripts are invoked from the appium server, if not you would need an extra function to login to appium server & call these functions.

More details on setting up appium with robot framework can be found here.

demo_library.py

 
import pexpect
import subprocess
import os
import sys
import pprint
import time
import pxssh
import re

def find_cpu_consumed(app_name):
    print "Find the CPU consumption in the mobile device"
    cpu = subprocess.check_output("adb shell top -t -n 2 -d 5 | grep %s &" %(app_name), stderr=subprocess.STDOUT, shell = True)
    print cpu


def find_memory_used():

    print " Find the memory used in the mobile device"
    memory = subprocess.check_output("adb shell cat /proc/meminfo", shell = True)
    print memory

Now for the Robot Framework script.

Notice the library has the demo_library.py file from which we can invoke the functions present in that file which are “Find CPU consumed” & “Find memory used”.

*** Settings ***

Library            AppiumLibrary
Library         demo_library.py
Documentation    demo for appium library
Force Tags       demo
#Resource         demo_resoure.txt

*** Variables ***

${app}     com.enfini.remindus

*** Test Cases ***

test_demo

    [Tags] 
    Find CPU consumed     ${app}
    Find Memory used
    Open Application  http://localhost:4723/wd/hub  platformName=Android  platformVersion=5.1.1  deviceName=fd1bee6e  app=${app}  automationName=appium  appPackage=${app}  appActivity=com.enfini.remindus.dashboard.DashboardSummary
    Click Element      xpath=//android.view.View[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[3]/android.widget.ImageView[1]
    Find CPU consumed     ${app}
    Find Memory used
    Close All Applications
    Find CPU consumed     ${app}
    Find Memory used

 

Robot Framework report for the above run can be found here.

 

report

 

Hi there. I'm Shivaram. Security & Network Professional with around 10+ years of experience. Here on this blog to share all my testing related experience. I'm a problem solver & key to that is to come up with the right questions. Of course we then have Google for all the answers.

Leave a Reply