Skip to content

Commit 92082bf

Browse files
heeseonSrinivasanTarget
authored andcommitted
add apis to read the performance data (#562)
* add apis to read the performance data * fix the requirements for pullrequest * fix the ci error * fix the ci error * fix the ci error * define interface to read performance data * delete the duplicated code * delete the unused import * delete the private comment
1 parent 72b9e6a commit 92082bf

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class MobileCommand {
4848
protected static final String GET_DEVICE_TIME;
4949
protected static final String GET_SESSION;
5050

51+
protected static final String GET_PERFORMANCE_DATA;
52+
protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES;
53+
54+
5155
protected static final String HIDE_KEYBOARD;
5256
protected static final String LOCK;
5357
//iOS
@@ -92,6 +96,9 @@ public class MobileCommand {
9296
GET_DEVICE_TIME = "getDeviceTime";
9397
GET_SESSION = "getSession";
9498

99+
GET_PERFORMANCE_DATA = "getPerformanceData";
100+
GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes";
101+
95102
HIDE_KEYBOARD = "hideKeyboard";
96103
LOCK = "lock";
97104
SHAKE = "shake";
@@ -136,6 +143,11 @@ public class MobileCommand {
136143
commandRepository.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"));
137144
commandRepository.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time"));
138145
commandRepository.put(GET_SESSION,getC("/session/:sessionId/"));
146+
commandRepository.put(GET_SUPPORTED_PERFORMANCE_DATA_TYPES,
147+
postC("/session/:sessionId/appium/performanceData/types"));
148+
commandRepository.put(GET_PERFORMANCE_DATA,
149+
postC("/session/:sessionId/appium/getPerformanceData"));
150+
139151
//iOS
140152
commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
141153
commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id"));

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
public class AndroidDriver<T extends WebElement>
4848
extends AppiumDriver<T>
4949
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
50-
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings, HasDeviceDetails {
50+
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings, HasDeviceDetails,
51+
HasSupportedPerformanceDataType {
5152

5253
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5354

src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,55 @@ public class AndroidMobileCommandHelper extends MobileCommand {
6363
END_TEST_COVERAGE, prepareArguments(parameters, values));
6464
}
6565

66+
/**
67+
* returns the information type of the system state which is supported to read
68+
* as like cpu, memory, network traffic, and battery.
69+
* @return output - array like below
70+
* [cpuinfo, batteryinfo, networkinfo, memoryinfo]
71+
*
72+
*/
73+
public static Map.Entry<String, Map<String, ?>> getSupportedPerformanceDataTypesCommand() {
74+
return new AbstractMap.SimpleEntry<>(
75+
GET_SUPPORTED_PERFORMANCE_DATA_TYPES, ImmutableMap.<String, Object>of());
76+
}
77+
78+
/**
79+
* returns the resource usage information of the application. the resource is one of the system state
80+
* which means cpu, memory, network traffic, and battery.
81+
*
82+
* @param packageName the package name of the application
83+
* @param dataType the type of system state which wants to read.
84+
* It should be one of the supported performance data types,
85+
* the return value of the function "getSupportedPerformanceDataTypes"
86+
* @param dataReadTimeout the number of attempts to read
87+
* @return table of the performance data, The first line of the table represents the type of data.
88+
* The remaining lines represent the values of the data.
89+
* in case of battery info : [[power], [23]]
90+
* in case of memory info :
91+
* [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty,
92+
* totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize],
93+
* [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]]
94+
* in case of network info :
95+
* [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,],
96+
* [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000],
97+
* [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]]
98+
* in case of network info :
99+
* [[st, activeTime, rb, rp, tb, tp, op, bucketDuration],
100+
* [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600],
101+
* [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600],
102+
* [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600],
103+
* [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]]
104+
* in case of cpu info : [[user, kernel], [0.9, 1.3]]
105+
* @throws Exception if the performance data type is not supported, thows Error
106+
*/
107+
public static Map.Entry<String, Map<String, ?>> getPerformanceDataCommand(
108+
String packageName, String dataType, int dataReadTimeout) throws Exception {
109+
String[] parameters = new String[] {"packageName", "dataType", "dataReadTimeout"};
110+
Object[] values = new Object[] {packageName, dataType, dataReadTimeout};
111+
return new AbstractMap.SimpleEntry<>(
112+
GET_PERFORMANCE_DATA, prepareArguments(parameters, values));
113+
}
114+
66115
/**
67116
* This method forms a {@link java.util.Map} of parameters to
68117
* Retrieve the display density of the Android device.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.appium.java_client.android;
2+
3+
import static io.appium.java_client.android.AndroidMobileCommandHelper.getPerformanceDataCommand;
4+
import static io.appium.java_client.android.AndroidMobileCommandHelper.getSupportedPerformanceDataTypesCommand;
5+
6+
import io.appium.java_client.CommandExecutionHelper;
7+
import io.appium.java_client.ExecutesMethod;
8+
9+
import java.util.List;
10+
11+
/**
12+
*
13+
*/
14+
public interface HasSupportedPerformanceDataType extends ExecutesMethod {
15+
16+
/**
17+
* returns the information type of the system state which is supported to read
18+
* as like cpu, memory, network traffic, and battery.
19+
* @return output - array like below
20+
* [cpuinfo, batteryinfo, networkinfo, memoryinfo]
21+
*
22+
*/
23+
default List<String> getSupportedPerformanceDataTypes() {
24+
return CommandExecutionHelper.execute(this, getSupportedPerformanceDataTypesCommand());
25+
}
26+
27+
/**
28+
* returns the resource usage information of the application. the resource is one of the system state
29+
* which means cpu, memory, network traffic, and battery.
30+
*
31+
* @param packageName the package name of the application
32+
* @param dataType the type of system state which wants to read.
33+
* It should be one of the supported performance data types,
34+
* the return value of the function "getSupportedPerformanceDataTypes"
35+
* @param dataReadTimeout the number of attempts to read
36+
* @return table of the performance data, The first line of the table represents the type of data.
37+
* The remaining lines represent the values of the data.
38+
* in case of battery info : [[power], [23]]
39+
* in case of memory info :
40+
* [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty,
41+
* totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize],
42+
* [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]]
43+
* in case of network info :
44+
* [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,],
45+
* [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000],
46+
* [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]]
47+
* in case of network info :
48+
* [[st, activeTime, rb, rp, tb, tp, op, bucketDuration],
49+
* [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600],
50+
* [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600],
51+
* [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600],
52+
* [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]]
53+
* in case of cpu info : [[user, kernel], [0.9, 1.3]]
54+
* @throws Exception if the performance data type is not supported, thows Error
55+
*/
56+
default List<List<Object>> getPerformanceData(String packageName, String dataType, int dataReadTimeout)
57+
throws Exception {
58+
return CommandExecutionHelper.execute(this,
59+
getPerformanceDataCommand(packageName, dataType, dataReadTimeout));
60+
}
61+
}

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
import org.openqa.selenium.html5.Location;
3030

3131
import java.io.File;
32+
import java.util.ArrayList;
33+
import java.util.List;
3234
import java.util.Map;
3335

36+
3437
public class AndroidDriverTest extends BaseAndroidTest {
3538

3639
@Test public void getDeviceTimeTest() {
@@ -140,4 +143,43 @@ public class AndroidDriverTest extends BaseAndroidTest {
140143
assertNotNull(driver.getDisplayDensity());
141144
assertNotEquals(0, driver.getSystemBars().size());
142145
}
146+
147+
@Test public void getSupportedPerformanceDataTypesTest() {
148+
driver.startActivity("io.appium.android.apis", ".ApiDemos");
149+
150+
List<String> dataTypes = new ArrayList<String>();
151+
dataTypes.add("cpuinfo");
152+
dataTypes.add("memoryinfo");
153+
dataTypes.add("batteryinfo");
154+
dataTypes.add("networkinfo");
155+
156+
List<String> supportedPerformanceDataTypes = driver.getSupportedPerformanceDataTypes();
157+
158+
assertEquals(4, supportedPerformanceDataTypes.size());
159+
160+
for ( int i = 0 ; i < supportedPerformanceDataTypes.size() ; ++i) {
161+
assertEquals(dataTypes.get(i), supportedPerformanceDataTypes.get(i));
162+
}
163+
164+
165+
}
166+
167+
@Test public void getPerformanceDataTest() throws Exception {
168+
driver.startActivity("io.appium.android.apis", ".ApiDemos");
169+
170+
List<String> supportedPerformanceDataTypes = driver.getSupportedPerformanceDataTypes();
171+
172+
for (int i = 0 ; i < supportedPerformanceDataTypes.size() ; ++ i) {
173+
174+
String dataType = supportedPerformanceDataTypes.get(i);
175+
176+
List<List<Object>> valueTable = driver.getPerformanceData("com.example.android.apis", dataType, 60000);
177+
178+
for ( int j = 1 ; j < valueTable.size() ; ++ j) {
179+
assertEquals(valueTable.subList(0,0).size(), valueTable.subList(j, j).size());
180+
}
181+
}
182+
183+
}
184+
143185
}

0 commit comments

Comments
 (0)