Geek Culture
Published in

Geek Culture

Android Apps Performance Testing Using Appium

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

Returns:
table of the performance data, The first line of the table represents the type of data. The remaining lines represent the values of the data. in case of battery info : [[power], [23]] in case of memory info : [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty, totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize], [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]] in case of network info : [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,], [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000], [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]] in case of network info : [[st, activeTime, rb, rp, tb, tp, op, bucketDuration], [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600], [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600], [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600], [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]] in case of cpu info : [[user, kernel], [0.9, 1.3]]

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();

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/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store