Android Apps Performance Testing Using Appium

Abhishek Dhoundiyal
2 min readFeb 26, 2023

--

How to use driver.getPerformanceData method in Appium Android Driver

((AndroidDriver)driver).getPerformanceData("<package>", "<perf type>", <timeout>);

returns the resource usage information of the application. the resource is one of the system states which means CPU, memory, network traffic, and battery.

Params:
packageName — the package name of the application dataType — the type of system state which wants to read. It should be one of the supported performance data types, the return value of the function “getSupportedPerformanceDataTypes” dataReadTimeout — the number of attempts to read

Get Performance Data Types

Returns the information types of the system state which is supported to read as like CPU, memory, network traffic, and battery

List<String> performanceTypes = driver.getSupportedPerformanceDataTypes();

Returns:
cpuinfo
memoryinfo
batteryinfo
networkinfo

Example:

Let’s construct a simple scenario, we want to open up a view in our app, take a snapshot of the memory usage, and then wait. After a while, we take another snapshot. Then we make an assertion that the memory usage outlined in the second snapshot is not significantly more than what we found in the first snapshot. This is a simple test that we don’t have a memory leak.

Assuming we’re using the MMT app, our call now looks like this:

List<List<Object>> data = driver.getPerformanceData("com.makemytrip", "memoryinfo", 10);
private HashMap<String, Integer> getMemoryInfo(AndroidDriver driver) throws Exception {
List<List<Object>> data = driver.getPerformanceData("com.makemytrip", "memoryinfo", 10);
HashMap<String, Integer> readableData = new HashMap<>();
for (int i = 0; i < data.get(0).size(); i++) {
int val;
if (data.get(1).get(i) == null) {
val = 0;
} else {
val = Integer.parseInt((String) data.get(1).get(i));
}
readableData.put((String) data.get(0).get(i), val);
}
return readableData;
}

Essentially, we now have HashMap we can use to query the particular kinds of memory info we retrieved. In our case, we're going to look for the totalPss value:

//totalPss = Proportional Set Size
HashMap<String, Integer> memoryInfo = getMemoryInfo(driver);
int setSize = memoryInfo.get("totalPss");

Complete Code:

@Test
public void testMemoryUsage() throws Exception {
// get the usage at one point in time
int totalPss1 = getMemoryInfo(driver).get(PSS_TYPE);
// then get it again after waiting a while
utility.threadSleep(MEMORY_USAGE_WAIT);
int totalPss2 = getMemoryInfo(driver).get(PSS_TYPE);
// finally, verify that we haven't increased usage more than 5%
Assert.assertThat((double) totalPss2, Matchers.lessThan(totalPss1 * 1.05));
}
/**
* Get Memory Info
*
* @param driver
* @return
* @throws Exception
*/
private HashMap<String, Integer> getMemoryInfo(AndroidDriver driver) throws Exception {
List<List<Object>> data = driver.getPerformanceData(PKG, PERF_TYPE, MEMORY_CAPTURE_WAIT);
HashMap<String, Integer> readableData = new HashMap<>();
for (int i = 0; i < data.get(0).size(); i++) {
int val;
if (data.get(1).get(i) == null) {
val = 0;
} else {
val = Integer.parseInt((String) data.get(1).get(i));
}
readableData.put((String) data.get(0).get(i), val);
}
return readableData;
}

Ref: https://developer.android.com/studio/command-line/dumpsys

https://appium.io/docs/en/commands/device/performance-data/performance-data-types/

--

--