add upload capabilities to data test.

Change-Id: I8465e4b97ff7f48de1150193bcd2b520da1adf00
This commit is contained in:
Tsu Chiang Chuang
2011-09-14 16:10:12 -07:00
parent 7a685e8911
commit e888685bf6
3 changed files with 75 additions and 2 deletions

View File

@@ -27,4 +27,4 @@ LOCAL_PACKAGE_NAME := BandwidthTests
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))
include $(call all-makefiles-under,$(LOCAL_PATH))

View File

@@ -119,6 +119,42 @@ public class BandwidthTest extends InstrumentationTestCase {
assertTrue(cleanUpFile(tmpSaveFile));
}
/**
* Ensure that downloading on wifi reports reasonable stats.
*/
@LargeTest
public void testWifiUpload() {
assertTrue(setDeviceWifiAndAirplaneMode(mSsid));
// Download a file from the server.
String ts = Long.toString(System.currentTimeMillis());
String targetUrl = BandwidthTestUtil.buildDownloadUrl(
mTestServer, FILE_SIZE, mDeviceId, ts);
File tmpSaveFile = new File(BASE_DIR + File.separator + TMP_FILENAME);
assertTrue(BandwidthTestUtil.DownloadFromUrl(targetUrl, tmpSaveFile));
ts = Long.toString(System.currentTimeMillis());
NetworkStats pre_test_stats = fetchDataFromProc(mUid);
TrafficStats.startDataProfiling(mContext);
assertTrue(BandwidthTestUtil.postFileToServer(mTestServer, mDeviceId, ts, tmpSaveFile));
NetworkStats prof_stats = TrafficStats.stopDataProfiling(mContext);
Log.d(LOG_TAG, prof_stats.toString());
NetworkStats post_test_stats = fetchDataFromProc(mUid);
NetworkStats proc_stats = post_test_stats.subtract(pre_test_stats);
// Output measurements to instrumentation out, so that it can be compared to that of
// the server.
Bundle results = new Bundle();
results.putString("device_id", mDeviceId);
results.putString("timestamp", ts);
results.putInt("size", FILE_SIZE);
AddStatsToResults(PROF_LABEL, prof_stats, results);
AddStatsToResults(PROC_LABEL, proc_stats, results);
getInstrumentation().sendStatus(INSTRUMENTATION_IN_PROGRESS, results);
// Clean up.
assertTrue(cleanUpFile(tmpSaveFile));
}
/**
* We want to make sure that if we use the Download Manager to download stuff,
* accounting still goes to the app making the call and that the numbers still make sense.

View File

@@ -18,6 +18,15 @@ package com.android.bandwidthtest.util;
import android.util.Log;
import com.android.internal.http.multipart.FilePart;
import com.android.internal.http.multipart.MultipartEntity;
import com.android.internal.http.multipart.Part;
import com.android.internal.http.multipart.StringPart;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer;
import java.io.BufferedInputStream;
@@ -80,7 +89,7 @@ public class BandwidthTestUtil {
* Download a given file from a target url to a given destination file.
* @param targetUrl the url to download
* @param file the {@link File} location where to save to
* @return true if it succeeded.
* @return true if it succeeded
*/
public static boolean DownloadFromUrl(String targetUrl, File file) {
try {
@@ -106,4 +115,32 @@ public class BandwidthTestUtil {
return true;
}
/**
* Post a given file for a given device and timestamp to the server.
* @param postUrl {@link String} url used to upload files
* @param deviceId {@link String} device id that is uploading
* @param timestamp {@link String} timestamp
* @param file {@link File} to upload
* @return true if it succeeded
*/
public static boolean postFileToServer(String postUrl, String deviceId, String timestamp,
File file) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(postUrl);
Part[] parts = {
new StringPart("device_id", deviceId),
new StringPart("timestamp", timestamp),
new FilePart("file", file)
};
MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
postRequest.setEntity(reqEntity);
HttpResponse res = httpClient.execute(postRequest);
res.getEntity().getContent().close();
} catch (IOException e) {
Log.e(LOG_TAG, "Could not upload file with error: " + e);
return false;
}
return true;
}
}