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