Skip to content

Commit 287353a

Browse files
author
Tsu Chiang Chuang
committed
Adding mobile microbenchmark tests.
Change-Id: Ifcd67b0265045778965aeff8e213c3929da02c3d
1 parent e562287 commit 287353a

File tree

2 files changed

+117
-19
lines changed

2 files changed

+117
-19
lines changed

core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,27 @@ protected void tearDown() throws Exception {
8989
* Ensure that downloading on wifi reports reasonable stats.
9090
*/
9191
@LargeTest
92-
public void testWifiDownload() {
93-
assertTrue(setDeviceWifiAndAirplaneMode(mSsid));
92+
public void testWifiDownload() throws Exception {
93+
assertTrue("Could not connect to wifi!", setDeviceWifiAndAirplaneMode(mSsid));
94+
downloadFile();
95+
}
96+
97+
/**
98+
* Ensure that downloading on mobile reports reasonable stats.
99+
*/
100+
@LargeTest
101+
public void testMobileDownload() throws Exception {
102+
// As part of the setup we disconnected from wifi; make sure we are connected to mobile and
103+
// that we have data.
104+
assertTrue("Do not have mobile data!", hasMobileData());
105+
downloadFile();
106+
}
107+
108+
/**
109+
* Helper method that downloads a file using http connection from a test server and reports the
110+
* data usage stats to instrumentation out.
111+
*/
112+
protected void downloadFile() throws Exception {
94113
NetworkStats pre_test_stats = fetchDataFromProc(mUid);
95114
String ts = Long.toString(System.currentTimeMillis());
96115

@@ -120,11 +139,28 @@ public void testWifiDownload() {
120139
}
121140

122141
/**
123-
* Ensure that downloading on wifi reports reasonable stats.
142+
* Ensure that uploading on wifi reports reasonable stats.
124143
*/
125144
@LargeTest
126145
public void testWifiUpload() {
127146
assertTrue(setDeviceWifiAndAirplaneMode(mSsid));
147+
uploadFile();
148+
}
149+
150+
/**
151+
* Ensure that uploading on wifi reports reasonable stats.
152+
*/
153+
@LargeTest
154+
public void testMobileUpload() throws Exception {
155+
assertTrue(hasMobileData());
156+
uploadFile();
157+
}
158+
159+
/**
160+
* Helper method that downloads a test file to upload. The stats reported to instrumentation out
161+
* only include upload stats.
162+
*/
163+
protected void uploadFile() throws Exception {
128164
// Download a file from the server.
129165
String ts = Long.toString(System.currentTimeMillis());
130166
String targetUrl = BandwidthTestUtil.buildDownloadUrl(
@@ -156,12 +192,30 @@ public void testWifiUpload() {
156192
}
157193

158194
/**
159-
* We want to make sure that if we use the Download Manager to download stuff,
195+
* We want to make sure that if we use wifi and the Download Manager to download stuff,
160196
* accounting still goes to the app making the call and that the numbers still make sense.
161197
*/
162198
@LargeTest
163199
public void testWifiDownloadWithDownloadManager() {
164200
assertTrue(setDeviceWifiAndAirplaneMode(mSsid));
201+
downloadFileUsingDownloadManager();
202+
}
203+
204+
/**
205+
* We want to make sure that if we use mobile data and the Download Manager to download stuff,
206+
* accounting still goes to the app making the call and that the numbers still make sense.
207+
*/
208+
@LargeTest
209+
public void testMobileDownloadWithDownloadManager() throws Exception {
210+
assertTrue(hasMobileData());
211+
downloadFileUsingDownloadManager();
212+
}
213+
214+
/**
215+
* Helper method that downloads a file from a test server using the download manager and reports
216+
* the stats to instrumentation out.
217+
*/
218+
protected void downloadFileUsingDownloadManager() throws Exception {
165219
// If we are using the download manager, then the data that is written to /proc/uid_stat/
166220
// is accounted against download manager's uid, since it uses pre-ICS API.
167221
int downloadManagerUid = mConnectionUtil.downloadManagerUid();
@@ -195,6 +249,7 @@ public void testWifiDownloadWithDownloadManager() {
195249

196250
/**
197251
* Fetch network data from /proc/uid_stat/uid
252+
*
198253
* @return populated {@link NetworkStats}
199254
*/
200255
public NetworkStats fetchDataFromProc(int uid) {
@@ -210,7 +265,8 @@ public NetworkStats fetchDataFromProc(int uid) {
210265
}
211266

212267
/**
213-
* Turn on Airplane mode and connect to the wifi
268+
* Turn on Airplane mode and connect to the wifi.
269+
*
214270
* @param ssid of the wifi to connect to
215271
* @return true if we successfully connected to a given network.
216272
*/
@@ -219,12 +275,25 @@ public boolean setDeviceWifiAndAirplaneMode(String ssid) {
219275
assertTrue(mConnectionUtil.connectToWifi(ssid));
220276
assertTrue(mConnectionUtil.waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
221277
ConnectionUtil.LONG_TIMEOUT));
222-
return mConnectionUtil.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
223-
ConnectionUtil.LONG_TIMEOUT);
278+
assertTrue(mConnectionUtil.waitForNetworkState(ConnectivityManager.TYPE_WIFI,
279+
State.CONNECTED, ConnectionUtil.LONG_TIMEOUT));
280+
return mConnectionUtil.hasData();
281+
}
282+
283+
/**
284+
* Helper method to make sure we are connected to mobile data.
285+
*
286+
* @return true if we successfully connect to mobile data.
287+
*/
288+
public boolean hasMobileData() {
289+
assertTrue("Not connected to mobile", mConnectionUtil.isConnectedToMobile());
290+
assertFalse("Still connected to wifi.", mConnectionUtil.isConnectedToWifi());
291+
return mConnectionUtil.hasData();
224292
}
225293

226294
/**
227295
* Output the {@link NetworkStats} to Instrumentation out.
296+
*
228297
* @param label to attach to this given stats.
229298
* @param stats {@link NetworkStats} to add.
230299
* @param results {@link Bundle} to be added to.
@@ -281,4 +350,4 @@ private boolean cleanUpFile(File file) {
281350
}
282351
return true;
283352
}
284-
}
353+
}

core/tests/bandwidthtests/src/com/android/bandwidthtest/util/ConnectionUtil.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.android.bandwidthtest.NetworkState.StateTransitionDirection;
4545
import com.android.internal.util.AsyncChannel;
4646

47+
import java.io.IOException;
48+
import java.net.UnknownHostException;
4749
import java.util.List;
4850

4951
/*
@@ -257,14 +259,14 @@ public void recordNetworkState(int networkType, State networkState) {
257259
mConnectivityState[networkType].recordState(networkState);
258260
}
259261

260-
/**
261-
* Set the state transition criteria
262-
*
263-
* @param networkType
264-
* @param initState
265-
* @param transitionDir
266-
* @param targetState
267-
*/
262+
/**
263+
* Set the state transition criteria
264+
*
265+
* @param networkType
266+
* @param initState
267+
* @param transitionDir
268+
* @param targetState
269+
*/
268270
public void setStateTransitionCriteria(int networkType, State initState,
269271
StateTransitionDirection transitionDir, State targetState) {
270272
mConnectivityState[networkType].setStateTransitionCriteria(
@@ -495,18 +497,19 @@ public boolean waitForWifiState(int expectedState, long timeout) {
495497
* @return true if connected to a mobile network, false otherwise.
496498
*/
497499
public boolean isConnectedToMobile() {
498-
return (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE);
500+
NetworkInfo networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
501+
return networkInfo.isConnected();
499502
}
500503

501504
/**
502505
* Convenience method to determine if we are connected to wifi.
503506
* @return true if connected to wifi, false otherwise.
504507
*/
505508
public boolean isConnectedToWifi() {
506-
return (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI);
509+
NetworkInfo networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
510+
return networkInfo.isConnected();
507511
}
508512

509-
510513
/**
511514
* Associate the device to given SSID
512515
* If the device is already associated with a WiFi, disconnect and forget it,
@@ -681,4 +684,30 @@ public void cleanUp() {
681684
}
682685
Log.v(LOG_TAG, "onDestroy, inst=" + Integer.toHexString(hashCode()));
683686
}
687+
688+
/**
689+
* Helper method used to test data connectivity by pinging a series of popular sites.
690+
* @return true if device has data connectivity, false otherwise.
691+
*/
692+
public boolean hasData() {
693+
String[] hostList = {"www.google.com", "www.yahoo.com",
694+
"www.bing.com", "www.facebook.com", "www.ask.com"};
695+
try {
696+
for (int i = 0; i < hostList.length; ++i) {
697+
String host = hostList[i];
698+
Process p = Runtime.getRuntime().exec("ping -c 10 -w 100 " + host);
699+
int status = p.waitFor();
700+
if (status == 0) {
701+
return true;
702+
}
703+
}
704+
} catch (UnknownHostException e) {
705+
Log.e(LOG_TAG, "Ping test Failed: Unknown Host");
706+
} catch (IOException e) {
707+
Log.e(LOG_TAG, "Ping test Failed: IOException");
708+
} catch (InterruptedException e) {
709+
Log.e(LOG_TAG, "Ping test Failed: InterruptedException");
710+
}
711+
return false;
712+
}
684713
}

0 commit comments

Comments
 (0)