diff options
author | Tsu Chiang Chuang <tsu@google.com> | 2011-09-14 16:10:12 -0700 |
---|---|---|
committer | Tsu Chiang Chuang <tsu@google.com> | 2011-09-15 12:01:29 -0700 |
commit | e888685bf6f443b7d69601796a7896240a7fc4ee (patch) | |
tree | 0de7db89dd9d224378e778450a243919562de41e /core/tests/bandwidthtests/src | |
parent | 7a685e89114ddfe35f87075dfe66a480c91c9de2 (diff) | |
download | frameworks_base-e888685bf6f443b7d69601796a7896240a7fc4ee.zip frameworks_base-e888685bf6f443b7d69601796a7896240a7fc4ee.tar.gz frameworks_base-e888685bf6f443b7d69601796a7896240a7fc4ee.tar.bz2 |
add upload capabilities to data test.
Change-Id: I8465e4b97ff7f48de1150193bcd2b520da1adf00
Diffstat (limited to 'core/tests/bandwidthtests/src')
-rw-r--r-- | core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java | 36 | ||||
-rw-r--r-- | core/tests/bandwidthtests/src/com/android/bandwidthtest/util/BandwidthTestUtil.java | 39 |
2 files changed, 74 insertions, 1 deletions
diff --git a/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java b/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java index be740d3..9eee2f0 100644 --- a/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java +++ b/core/tests/bandwidthtests/src/com/android/bandwidthtest/BandwidthTest.java @@ -120,6 +120,42 @@ public class BandwidthTest extends InstrumentationTestCase { } /** + * 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 d850169..7dea9e3 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; + } } |