From e888685bf6f443b7d69601796a7896240a7fc4ee Mon Sep 17 00:00:00 2001 From: Tsu Chiang Chuang Date: Wed, 14 Sep 2011 16:10:12 -0700 Subject: [PATCH] add upload capabilities to data test. Change-Id: I8465e4b97ff7f48de1150193bcd2b520da1adf00 --- core/tests/bandwidthtests/Android.mk | 2 +- .../android/bandwidthtest/BandwidthTest.java | 36 +++++++++++++++++ .../bandwidthtest/util/BandwidthTestUtil.java | 39 ++++++++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/core/tests/bandwidthtests/Android.mk b/core/tests/bandwidthtests/Android.mk index 3d56141afd439..6871efd7dc019 100644 --- a/core/tests/bandwidthtests/Android.mk +++ b/core/tests/bandwidthtests/Android.mk @@ -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)) \ No newline at end of file diff --git a/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java b/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java index be740d3ec529f..9eee2f0f1a99b 100644 --- a/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java +++ b/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java @@ -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. diff --git a/core/tests/bandwidthtests/src/com/android/bandwidthtest/util/BandwidthTestUtil.java b/core/tests/bandwidthtests/src/com/android/bandwidthtest/util/BandwidthTestUtil.java index d850169bbad8b..7dea9e36bfa2e 100644 --- a/core/tests/bandwidthtests/src/com/android/bandwidthtest/util/BandwidthTestUtil.java +++ b/core/tests/bandwidthtests/src/com/android/bandwidthtest/util/BandwidthTestUtil.java @@ -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; + } }