| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.app.Activity; | 8 import android.app.Activity; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
| 11 import android.os.Bundle; | 11 import android.os.Bundle; |
| 12 import android.os.Debug; | 12 import android.os.Debug; |
| 13 | 13 |
| 14 import org.chromium.base.PathUtils; |
| 15 |
| 14 import org.json.JSONException; | 16 import org.json.JSONException; |
| 15 import org.json.JSONObject; | 17 import org.json.JSONObject; |
| 16 | 18 |
| 17 import java.io.File; | 19 import java.io.File; |
| 18 import java.io.FileOutputStream; | 20 import java.io.FileOutputStream; |
| 19 import java.io.IOException; | 21 import java.io.IOException; |
| 20 import java.io.InputStream; | 22 import java.io.InputStream; |
| 21 import java.io.OutputStream; | 23 import java.io.OutputStream; |
| 22 import java.net.HttpURLConnection; | 24 import java.net.HttpURLConnection; |
| 23 import java.net.MalformedURLException; | 25 import java.net.MalformedURLException; |
| 24 import java.net.URL; | 26 import java.net.URL; |
| 25 import java.net.URLConnection; | 27 import java.net.URLConnection; |
| 26 import java.nio.ByteBuffer; | 28 import java.nio.ByteBuffer; |
| 27 import java.util.ArrayList; | 29 import java.util.ArrayList; |
| 28 import java.util.List; | 30 import java.util.List; |
| 29 import java.util.concurrent.Callable; | 31 import java.util.concurrent.Callable; |
| 30 import java.util.concurrent.Executor; | 32 import java.util.concurrent.Executor; |
| 31 import java.util.concurrent.ExecutorService; | 33 import java.util.concurrent.ExecutorService; |
| 32 import java.util.concurrent.Executors; | 34 import java.util.concurrent.Executors; |
| 33 import java.util.concurrent.Future; | 35 import java.util.concurrent.Future; |
| 34 import java.util.concurrent.LinkedBlockingQueue; | 36 import java.util.concurrent.LinkedBlockingQueue; |
| 35 import java.util.concurrent.TimeUnit; | 37 import java.util.concurrent.TimeUnit; |
| 36 | 38 |
| 37 /** | 39 /** |
| 38 * Runs networking benchmarks and saves results to a file. | 40 * Runs networking benchmarks and saves results to a file. |
| 39 */ | 41 */ |
| 40 public class CronetPerfTestActivity extends Activity { | 42 public class CronetPerfTestActivity extends Activity { |
| 43 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_perf_tes
t"; |
| 41 // Benchmark configuration passed down from host via Intent data. | 44 // Benchmark configuration passed down from host via Intent data. |
| 42 // Call getConfig*(key) to extract individual configuration values. | 45 // Call getConfig*(key) to extract individual configuration values. |
| 43 private Uri mConfig; | 46 private Uri mConfig; |
| 44 // Functions that retrieve individual benchmark configuration values. | 47 // Functions that retrieve individual benchmark configuration values. |
| 45 private String getConfigString(String key) { | 48 private String getConfigString(String key) { |
| 46 return mConfig.getQueryParameter(key); | 49 return mConfig.getQueryParameter(key); |
| 47 } | 50 } |
| 48 private int getConfigInt(String key) { | 51 private int getConfigInt(String key) { |
| 49 return Integer.parseInt(mConfig.getQueryParameter(key)); | 52 return Integer.parseInt(mConfig.getQueryParameter(key)); |
| 50 } | 53 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // When measuring a large upload, only download a small amou
nt so download time | 156 // When measuring a large upload, only download a small amou
nt so download time |
| 154 // isn't significant. | 157 // isn't significant. |
| 155 resource = getConfigString( | 158 resource = getConfigString( |
| 156 direction == Direction.UP ? "SMALL_RESOURCE" : "LARG
E_RESOURCE"); | 159 direction == Direction.UP ? "SMALL_RESOURCE" : "LARG
E_RESOURCE"); |
| 157 mIterations = getConfigInt("LARGE_ITERATIONS"); | 160 mIterations = getConfigInt("LARGE_ITERATIONS"); |
| 158 mLength = getConfigInt("LARGE_RESOURCE_SIZE"); | 161 mLength = getConfigInt("LARGE_RESOURCE_SIZE"); |
| 159 break; | 162 break; |
| 160 default: | 163 default: |
| 161 throw new IllegalArgumentException("Unknown size: " + size); | 164 throw new IllegalArgumentException("Unknown size: " + size); |
| 162 } | 165 } |
| 166 final String scheme; |
| 167 final String host; |
| 163 final int port; | 168 final int port; |
| 164 switch (protocol) { | 169 switch (protocol) { |
| 165 case HTTP: | 170 case HTTP: |
| 171 scheme = "http"; |
| 172 host = getConfigString("HOST_IP"); |
| 166 port = getConfigInt("HTTP_PORT"); | 173 port = getConfigInt("HTTP_PORT"); |
| 167 break; | 174 break; |
| 168 case QUIC: | 175 case QUIC: |
| 176 scheme = "https"; |
| 177 host = getConfigString("HOST"); |
| 169 port = getConfigInt("QUIC_PORT"); | 178 port = getConfigInt("QUIC_PORT"); |
| 170 break; | 179 break; |
| 171 default: | 180 default: |
| 172 throw new IllegalArgumentException("Unknown protocol: " + pr
otocol); | 181 throw new IllegalArgumentException("Unknown protocol: " + pr
otocol); |
| 173 } | 182 } |
| 174 try { | 183 try { |
| 175 mUrl = new URL("http", getConfigString("HOST"), port, resource); | 184 mUrl = new URL(scheme, host, port, resource); |
| 176 } catch (MalformedURLException e) { | 185 } catch (MalformedURLException e) { |
| 177 throw new IllegalArgumentException("Bad URL: " + getConfigString
("HOST") + ":" | 186 throw new IllegalArgumentException( |
| 178 + port + "/" + resource); | 187 "Bad URL: " + host + ":" + port + "/" + resource); |
| 179 } | 188 } |
| 180 final CronetEngine.Builder cronetEngineBuilder = | 189 final CronetEngine.Builder cronetEngineBuilder = |
| 181 new CronetEngine.Builder(CronetPerfTestActivity.this); | 190 new CronetEngine.Builder(CronetPerfTestActivity.this); |
| 191 cronetEngineBuilder.setLibraryName("cronet_tests"); |
| 182 if (mProtocol == Protocol.QUIC) { | 192 if (mProtocol == Protocol.QUIC) { |
| 183 cronetEngineBuilder.enableQUIC(true); | 193 cronetEngineBuilder.enableQUIC(true); |
| 184 cronetEngineBuilder.addQuicHint(getConfigString("HOST"), getConf
igInt("QUIC_PORT"), | 194 cronetEngineBuilder.addQuicHint(host, port, port); |
| 185 getConfigInt("QUIC_PORT")); | 195 cronetEngineBuilder.setMockCertVerifierForTesting( |
| 196 MockCertVerifier.createMockCertVerifier( |
| 197 new String[] {getConfigString("QUIC_CERT_FILE")}
)); |
| 198 } |
| 199 |
| 200 try { |
| 201 JSONObject quicParams = new JSONObject().put("host_whitelist", h
ost); |
| 202 JSONObject experimentalOptions = new JSONObject().put("QUIC", qu
icParams); |
| 203 cronetEngineBuilder.setExperimentalOptions(experimentalOptions.t
oString()); |
| 204 } catch (JSONException e) { |
| 205 throw new IllegalStateException("JSON failed: " + e); |
| 186 } | 206 } |
| 187 mCronetEngine = cronetEngineBuilder.build(); | 207 mCronetEngine = cronetEngineBuilder.build(); |
| 208 CronetTestUtil.registerHostResolverProc(mCronetEngine, getConfigStri
ng("HOST_IP")); |
| 188 mName = buildBenchmarkName(mode, direction, protocol, concurrency, m
Iterations); | 209 mName = buildBenchmarkName(mode, direction, protocol, concurrency, m
Iterations); |
| 189 mConcurrency = concurrency; | 210 mConcurrency = concurrency; |
| 190 mResults = results; | 211 mResults = results; |
| 191 mBufferSize = mLength > getConfigInt("MAX_BUFFER_SIZE") | 212 mBufferSize = mLength > getConfigInt("MAX_BUFFER_SIZE") |
| 192 ? getConfigInt("MAX_BUFFER_SIZE") | 213 ? getConfigInt("MAX_BUFFER_SIZE") |
| 193 : mLength; | 214 : mLength; |
| 194 mUseNetworkThread = getConfigBoolean("CRONET_ASYNC_USE_NETWORK_THREA
D"); | 215 mUseNetworkThread = getConfigBoolean("CRONET_ASYNC_USE_NETWORK_THREA
D"); |
| 195 } | 216 } |
| 196 | 217 |
| 197 private void startTimer() { | 218 private void startTimer() { |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 397 } |
| 377 | 398 |
| 378 public void rewind(UploadDataSink uploadDataSink) { | 399 public void rewind(UploadDataSink uploadDataSink) { |
| 379 uploadDataSink.onRewindError(new Exception("no rewinding")); | 400 uploadDataSink.onRewindError(new Exception("no rewinding")); |
| 380 } | 401 } |
| 381 } | 402 } |
| 382 | 403 |
| 383 private class Callback extends UrlRequest.Callback { | 404 private class Callback extends UrlRequest.Callback { |
| 384 private final ByteBuffer mBuffer; | 405 private final ByteBuffer mBuffer; |
| 385 private final Runnable mCompletionCallback; | 406 private final Runnable mCompletionCallback; |
| 407 private int mBytesReceived; |
| 386 | 408 |
| 387 Callback(ByteBuffer buffer, Runnable completionCallback) { | 409 Callback(ByteBuffer buffer, Runnable completionCallback) { |
| 388 mBuffer = buffer; | 410 mBuffer = buffer; |
| 389 mCompletionCallback = completionCallback; | 411 mCompletionCallback = completionCallback; |
| 390 } | 412 } |
| 391 | 413 |
| 392 @Override | 414 @Override |
| 393 public void onResponseStarted(UrlRequest request, UrlResponseInf
o info) { | 415 public void onResponseStarted(UrlRequest request, UrlResponseInf
o info) { |
| 394 mBuffer.clear(); | 416 mBuffer.clear(); |
| 395 request.readNew(mBuffer); | 417 request.readNew(mBuffer); |
| 396 } | 418 } |
| 397 | 419 |
| 398 @Override | 420 @Override |
| 399 public void onRedirectReceived( | 421 public void onRedirectReceived( |
| 400 UrlRequest request, UrlResponseInfo info, String newLoca
tionUrl) { | 422 UrlRequest request, UrlResponseInfo info, String newLoca
tionUrl) { |
| 401 request.followRedirect(); | 423 request.followRedirect(); |
| 402 } | 424 } |
| 403 | 425 |
| 404 @Override | 426 @Override |
| 405 public void onReadCompleted( | 427 public void onReadCompleted( |
| 406 UrlRequest request, UrlResponseInfo info, ByteBuffer byt
eBuffer) { | 428 UrlRequest request, UrlResponseInfo info, ByteBuffer byt
eBuffer) { |
| 429 mBytesReceived += byteBuffer.position(); |
| 407 mBuffer.clear(); | 430 mBuffer.clear(); |
| 408 request.readNew(mBuffer); | 431 request.readNew(mBuffer); |
| 409 } | 432 } |
| 410 | 433 |
| 411 @Override | 434 @Override |
| 412 public void onSucceeded(UrlRequest request, UrlResponseInfo info
) { | 435 public void onSucceeded(UrlRequest request, UrlResponseInfo info
) { |
| 436 if (info.getHttpStatusCode() != 200 || mBytesReceived != mLe
ngth) { |
| 437 System.out.println("Failed: response code: " + info.getH
ttpStatusCode() |
| 438 + " bytes: " + mBytesReceived); |
| 439 mFailed = true; |
| 440 } |
| 413 mCompletionCallback.run(); | 441 mCompletionCallback.run(); |
| 414 } | 442 } |
| 415 | 443 |
| 416 @Override | 444 @Override |
| 417 public void onFailed( | 445 public void onFailed( |
| 418 UrlRequest request, UrlResponseInfo info, UrlRequestExce
ption e) { | 446 UrlRequest request, UrlResponseInfo info, UrlRequestExce
ption e) { |
| 419 System.out.println("Async request failed with " + e); | 447 System.out.println("Async request failed with " + e); |
| 420 mFailed = true; | 448 mFailed = true; |
| 421 } | 449 } |
| 422 } | 450 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 } | 603 } |
| 576 } | 604 } |
| 577 finish(); | 605 finish(); |
| 578 return null; | 606 return null; |
| 579 } | 607 } |
| 580 } | 608 } |
| 581 | 609 |
| 582 @Override | 610 @Override |
| 583 public void onCreate(Bundle savedInstanceState) { | 611 public void onCreate(Bundle savedInstanceState) { |
| 584 super.onCreate(savedInstanceState); | 612 super.onCreate(savedInstanceState); |
| 613 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, t
his); |
| 585 mConfig = getIntent().getData(); | 614 mConfig = getIntent().getData(); |
| 586 // Execute benchmarks on another thread to avoid networking on main thre
ad. | 615 // Execute benchmarks on another thread to avoid networking on main thre
ad. |
| 587 new BenchmarkTask().execute(); | 616 new BenchmarkTask().execute(); |
| 588 } | 617 } |
| 589 } | 618 } |
| OLD | NEW |