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; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 return name; | 104 return name; |
105 } | 105 } |
106 | 106 |
107 // Responsible for running one particular benchmark and timing it. | 107 // Responsible for running one particular benchmark and timing it. |
108 private class Benchmark { | 108 private class Benchmark { |
109 private final Mode mMode; | 109 private final Mode mMode; |
110 private final Direction mDirection; | 110 private final Direction mDirection; |
111 private final Protocol mProtocol; | 111 private final Protocol mProtocol; |
112 private final URL mUrl; | 112 private final URL mUrl; |
113 private final String mName; | 113 private final String mName; |
114 private final UrlRequestContext mCronetContext; | 114 private final CronetEngine mCronetEngine; |
115 // Size in bytes of content being uploaded or downloaded. | 115 // Size in bytes of content being uploaded or downloaded. |
116 private final int mLength; | 116 private final int mLength; |
117 // How many requests to execute. | 117 // How many requests to execute. |
118 private final int mIterations; | 118 private final int mIterations; |
119 // How many requests to execute in parallel at any one time. | 119 // How many requests to execute in parallel at any one time. |
120 private final int mConcurrency; | 120 private final int mConcurrency; |
121 // Dictionary of benchmark names mapped to times to complete the benchma
rks. | 121 // Dictionary of benchmark names mapped to times to complete the benchma
rks. |
122 private final JSONObject mResults; | 122 private final JSONObject mResults; |
123 // How large a buffer to use for passing content, in bytes. | 123 // How large a buffer to use for passing content, in bytes. |
124 private final int mBufferSize; | 124 private final int mBufferSize; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 break; | 171 break; |
172 default: | 172 default: |
173 throw new IllegalArgumentException("Unknown protocol: " + pr
otocol); | 173 throw new IllegalArgumentException("Unknown protocol: " + pr
otocol); |
174 } | 174 } |
175 try { | 175 try { |
176 mUrl = new URL("http", getConfigString("HOST"), port, resource); | 176 mUrl = new URL("http", getConfigString("HOST"), port, resource); |
177 } catch (MalformedURLException e) { | 177 } catch (MalformedURLException e) { |
178 throw new IllegalArgumentException("Bad URL: " + getConfigString
("HOST") + ":" | 178 throw new IllegalArgumentException("Bad URL: " + getConfigString
("HOST") + ":" |
179 + port + "/" + resource); | 179 + port + "/" + resource); |
180 } | 180 } |
181 final UrlRequestContextConfig cronetConfig = new UrlRequestContextCo
nfig(); | 181 final CronetEngine.Builder cronetEngineBuilder = |
| 182 new CronetEngine.Builder(CronetPerfTestActivity.this); |
182 if (mProtocol == Protocol.QUIC) { | 183 if (mProtocol == Protocol.QUIC) { |
183 cronetConfig.enableQUIC(true); | 184 cronetEngineBuilder.enableQUIC(true); |
184 cronetConfig.addQuicHint(getConfigString("HOST"), getConfigInt("
QUIC_PORT"), | 185 cronetEngineBuilder.addQuicHint(getConfigString("HOST"), getConf
igInt("QUIC_PORT"), |
185 getConfigInt("QUIC_PORT")); | 186 getConfigInt("QUIC_PORT")); |
186 } | 187 } |
187 mCronetContext = new CronetUrlRequestContext(CronetPerfTestActivity.
this, cronetConfig); | 188 mCronetEngine = cronetEngineBuilder.build(); |
188 mName = buildBenchmarkName(mode, direction, protocol, concurrency, m
Iterations); | 189 mName = buildBenchmarkName(mode, direction, protocol, concurrency, m
Iterations); |
189 mConcurrency = concurrency; | 190 mConcurrency = concurrency; |
190 mResults = results; | 191 mResults = results; |
191 mBufferSize = mLength > getConfigInt("MAX_BUFFER_SIZE") | 192 mBufferSize = mLength > getConfigInt("MAX_BUFFER_SIZE") |
192 ? getConfigInt("MAX_BUFFER_SIZE") | 193 ? getConfigInt("MAX_BUFFER_SIZE") |
193 : mLength; | 194 : mLength; |
194 mUseNetworkThread = getConfigBoolean("CRONET_ASYNC_USE_NETWORK_THREA
D"); | 195 mUseNetworkThread = getConfigBoolean("CRONET_ASYNC_USE_NETWORK_THREA
D"); |
195 } | 196 } |
196 | 197 |
197 private void startTimer() { | 198 private void startTimer() { |
(...skipping 11 matching lines...) Expand all Loading... |
209 mResults.put(mName, mStopTimeMs - mStartTimeMs); | 210 mResults.put(mName, mStopTimeMs - mStartTimeMs); |
210 } catch (JSONException e) { | 211 } catch (JSONException e) { |
211 System.out.println("Failed to write JSON result for " + mName); | 212 System.out.println("Failed to write JSON result for " + mName); |
212 } | 213 } |
213 } | 214 } |
214 | 215 |
215 // TODO(pauljensen): Remove @SuppressLint once crbug.com/501591 is fixed
. | 216 // TODO(pauljensen): Remove @SuppressLint once crbug.com/501591 is fixed
. |
216 @SuppressLint("NewApi") | 217 @SuppressLint("NewApi") |
217 private void startLogging() { | 218 private void startLogging() { |
218 if (getConfigBoolean("CAPTURE_NETLOG")) { | 219 if (getConfigBoolean("CAPTURE_NETLOG")) { |
219 mCronetContext.startNetLogToFile(getFilesDir().getPath() + "/" +
mName + ".json", | 220 mCronetEngine.startNetLogToFile( |
220 false); | 221 getFilesDir().getPath() + "/" + mName + ".json", false); |
221 } | 222 } |
222 if (getConfigBoolean("CAPTURE_TRACE")) { | 223 if (getConfigBoolean("CAPTURE_TRACE")) { |
223 Debug.startMethodTracing(getFilesDir().getPath() + "/" + mName +
".trace"); | 224 Debug.startMethodTracing(getFilesDir().getPath() + "/" + mName +
".trace"); |
224 } else if (getConfigBoolean("CAPTURE_SAMPLED_TRACE")) { | 225 } else if (getConfigBoolean("CAPTURE_SAMPLED_TRACE")) { |
225 Debug.startMethodTracingSampling( | 226 Debug.startMethodTracingSampling( |
226 getFilesDir().getPath() + "/" + mName + ".trace", 800000
0, 10); | 227 getFilesDir().getPath() + "/" + mName + ".trace", 800000
0, 10); |
227 } | 228 } |
228 } | 229 } |
229 | 230 |
230 private void stopLogging() { | 231 private void stopLogging() { |
231 if (getConfigBoolean("CAPTURE_NETLOG")) { | 232 if (getConfigBoolean("CAPTURE_NETLOG")) { |
232 mCronetContext.stopNetLog(); | 233 mCronetEngine.stopNetLog(); |
233 } | 234 } |
234 if (getConfigBoolean("CAPTURE_TRACE") || getConfigBoolean("CAPTURE_S
AMPLED_TRACE")) { | 235 if (getConfigBoolean("CAPTURE_TRACE") || getConfigBoolean("CAPTURE_S
AMPLED_TRACE")) { |
235 Debug.stopMethodTracing(); | 236 Debug.stopMethodTracing(); |
236 } | 237 } |
237 } | 238 } |
238 | 239 |
239 /** | 240 /** |
240 * Transfer {@code mLength} bytes through HttpURLConnection in {@code mD
irection} direction. | 241 * Transfer {@code mLength} bytes through HttpURLConnection in {@code mD
irection} direction. |
241 * @param connection The HttpURLConnection to use for transfer. | 242 * @param connection The HttpURLConnection to use for transfer. |
242 * @param buffer A buffer of length |mBufferSize| to use for transfer. | 243 * @param buffer A buffer of length |mBufferSize| to use for transfer. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 }); | 337 }); |
337 } | 338 } |
338 return; | 339 return; |
339 } | 340 } |
340 mRemainingRequests--; | 341 mRemainingRequests--; |
341 final Runnable completionCallback = new Runnable() { | 342 final Runnable completionCallback = new Runnable() { |
342 public void run() { | 343 public void run() { |
343 initiateRequest(buffer); | 344 initiateRequest(buffer); |
344 } | 345 } |
345 }; | 346 }; |
346 final UrlRequest request = mCronetContext.createRequest(mUrl.toS
tring(), | 347 final UrlRequest.Builder builder = new UrlRequest.Builder(mUrl.t
oString(), |
347 new Listener(buffer, completionCallback), mWorkQueueExec
utor); | 348 new Listener(buffer, completionCallback), mWorkQueueExec
utor, |
| 349 mCronetEngine); |
348 if (mDirection == Direction.UP) { | 350 if (mDirection == Direction.UP) { |
349 request.setUploadDataProvider(new Uploader(buffer), mWorkQue
ueExecutor); | 351 builder.setUploadDataProvider(new Uploader(buffer), mWorkQue
ueExecutor); |
350 request.addHeader("Content-Type", "application/octet-stream"
); | 352 builder.addHeader("Content-Type", "application/octet-stream"
); |
351 } | 353 } |
352 request.start(); | 354 builder.build().start(); |
353 } | 355 } |
354 | 356 |
355 private class Uploader extends UploadDataProvider { | 357 private class Uploader extends UploadDataProvider { |
356 private final ByteBuffer mBuffer; | 358 private final ByteBuffer mBuffer; |
357 private int mRemainingBytes; | 359 private int mRemainingBytes; |
358 | 360 |
359 Uploader(ByteBuffer buffer) { | 361 Uploader(ByteBuffer buffer) { |
360 mBuffer = buffer; | 362 mBuffer = buffer; |
361 mRemainingBytes = mLength; | 363 mRemainingBytes = mLength; |
362 } | 364 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 startLogging(); | 474 startLogging(); |
473 // Prepare list of tasks to run. | 475 // Prepare list of tasks to run. |
474 switch (mMode) { | 476 switch (mMode) { |
475 case SYSTEM_HUC: | 477 case SYSTEM_HUC: |
476 for (int i = 0; i < mIterations; i++) { | 478 for (int i = 0; i < mIterations; i++) { |
477 tasks.add(new SystemHttpURLConnectionFetchTask()); | 479 tasks.add(new SystemHttpURLConnectionFetchTask()); |
478 } | 480 } |
479 break; | 481 break; |
480 case CRONET_HUC: { | 482 case CRONET_HUC: { |
481 final CronetHttpURLStreamHandler cronetStreamHandler = | 483 final CronetHttpURLStreamHandler cronetStreamHandler = |
482 new CronetHttpURLStreamHandler(mCronetContext); | 484 new CronetHttpURLStreamHandler(mCronetEngine); |
483 for (int i = 0; i < mIterations; i++) { | 485 for (int i = 0; i < mIterations; i++) { |
484 tasks.add(new CronetHttpURLConnectionFetchTask(cronetStr
eamHandler)); | 486 tasks.add(new CronetHttpURLConnectionFetchTask(cronetStr
eamHandler)); |
485 } | 487 } |
486 break; | 488 break; |
487 } | 489 } |
488 case CRONET_ASYNC: | 490 case CRONET_ASYNC: |
489 tasks.add(new CronetAsyncFetchTask()); | 491 tasks.add(new CronetAsyncFetchTask()); |
490 break; | 492 break; |
491 default: | 493 default: |
492 throw new IllegalArgumentException("Unknown mode: " + mMode)
; | 494 throw new IllegalArgumentException("Unknown mode: " + mMode)
; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 } | 588 } |
587 | 589 |
588 @Override | 590 @Override |
589 public void onCreate(Bundle savedInstanceState) { | 591 public void onCreate(Bundle savedInstanceState) { |
590 super.onCreate(savedInstanceState); | 592 super.onCreate(savedInstanceState); |
591 mConfig = getIntent().getData(); | 593 mConfig = getIntent().getData(); |
592 // Execute benchmarks on another thread to avoid networking on main thre
ad. | 594 // Execute benchmarks on another thread to avoid networking on main thre
ad. |
593 new BenchmarkTask().execute(); | 595 new BenchmarkTask().execute(); |
594 } | 596 } |
595 } | 597 } |
OLD | NEW |