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.json.JSONException; | 14 import org.json.JSONException; |
15 import org.json.JSONObject; | 15 import org.json.JSONObject; |
16 | 16 |
17 import org.chromium.base.ContextUtils; | |
18 import org.chromium.base.PathUtils; | 17 import org.chromium.base.PathUtils; |
19 | 18 |
20 import java.io.File; | 19 import java.io.File; |
21 import java.io.FileOutputStream; | 20 import java.io.FileOutputStream; |
22 import java.io.IOException; | 21 import java.io.IOException; |
23 import java.io.InputStream; | 22 import java.io.InputStream; |
24 import java.io.OutputStream; | 23 import java.io.OutputStream; |
25 import java.net.HttpURLConnection; | 24 import java.net.HttpURLConnection; |
26 import java.net.MalformedURLException; | 25 import java.net.MalformedURLException; |
27 import java.net.URL; | 26 import java.net.URL; |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 } catch (IOException e) { | 324 } catch (IOException e) { |
326 System.out.println("Cronet HttpURLConnection failed with " +
e); | 325 System.out.println("Cronet HttpURLConnection failed with " +
e); |
327 return false; | 326 return false; |
328 } | 327 } |
329 } | 328 } |
330 } | 329 } |
331 | 330 |
332 // GET or POST to one particular URL using Cronet's asynchronous API | 331 // GET or POST to one particular URL using Cronet's asynchronous API |
333 private class CronetAsyncFetchTask implements Callable<Boolean> { | 332 private class CronetAsyncFetchTask implements Callable<Boolean> { |
334 // A message-queue for asynchronous tasks to post back to. | 333 // A message-queue for asynchronous tasks to post back to. |
335 private final LinkedBlockingQueue<Runnable> mWorkQueue = new LinkedB
lockingQueue<>(); | 334 private final LinkedBlockingQueue<Runnable> mWorkQueue = |
| 335 new LinkedBlockingQueue<Runnable>(); |
336 private final WorkQueueExecutor mWorkQueueExecutor = new WorkQueueEx
ecutor(); | 336 private final WorkQueueExecutor mWorkQueueExecutor = new WorkQueueEx
ecutor(); |
337 | 337 |
338 private int mRemainingRequests; | 338 private int mRemainingRequests; |
339 private int mConcurrentFetchersDone; | 339 private int mConcurrentFetchersDone; |
340 private boolean mFailed; | 340 private boolean mFailed; |
341 | 341 |
342 CronetAsyncFetchTask() { | 342 CronetAsyncFetchTask() { |
343 mRemainingRequests = mIterations; | 343 mRemainingRequests = mIterations; |
344 mConcurrentFetchersDone = 0; | 344 mConcurrentFetchersDone = 0; |
345 mFailed = false; | 345 mFailed = false; |
346 } | 346 } |
347 | 347 |
348 private void initiateRequest(final ByteBuffer buffer) { | 348 private void initiateRequest(final ByteBuffer buffer) { |
349 if (mRemainingRequests == 0) { | 349 if (mRemainingRequests == 0) { |
350 mConcurrentFetchersDone++; | 350 mConcurrentFetchersDone++; |
351 if (mUseNetworkThread) { | 351 if (mUseNetworkThread) { |
352 // Post empty task so message loop exit condition is ret
ested. | 352 // Post empty task so message loop exit condition is ret
ested. |
353 postToWorkQueue(new Runnable() { | 353 postToWorkQueue(new Runnable() { |
354 @Override | |
355 public void run() {} | 354 public void run() {} |
356 }); | 355 }); |
357 } | 356 } |
358 return; | 357 return; |
359 } | 358 } |
360 mRemainingRequests--; | 359 mRemainingRequests--; |
361 final Runnable completionCallback = new Runnable() { | 360 final Runnable completionCallback = new Runnable() { |
362 @Override | |
363 public void run() { | 361 public void run() { |
364 initiateRequest(buffer); | 362 initiateRequest(buffer); |
365 } | 363 } |
366 }; | 364 }; |
367 final UrlRequest.Builder builder = new UrlRequest.Builder(mUrl.t
oString(), | 365 final UrlRequest.Builder builder = new UrlRequest.Builder(mUrl.t
oString(), |
368 new Callback(buffer, completionCallback), mWorkQueueExec
utor, | 366 new Callback(buffer, completionCallback), mWorkQueueExec
utor, |
369 mCronetEngine); | 367 mCronetEngine); |
370 if (mDirection == Direction.UP) { | 368 if (mDirection == Direction.UP) { |
371 builder.setUploadDataProvider(new Uploader(buffer), mWorkQue
ueExecutor); | 369 builder.setUploadDataProvider(new Uploader(buffer), mWorkQue
ueExecutor); |
372 builder.addHeader("Content-Type", "application/octet-stream"
); | 370 builder.addHeader("Content-Type", "application/octet-stream"
); |
373 } | 371 } |
374 builder.build().start(); | 372 builder.build().start(); |
375 } | 373 } |
376 | 374 |
377 private class Uploader extends UploadDataProvider { | 375 private class Uploader extends UploadDataProvider { |
378 private final ByteBuffer mBuffer; | 376 private final ByteBuffer mBuffer; |
379 private int mRemainingBytes; | 377 private int mRemainingBytes; |
380 | 378 |
381 Uploader(ByteBuffer buffer) { | 379 Uploader(ByteBuffer buffer) { |
382 mBuffer = buffer; | 380 mBuffer = buffer; |
383 mRemainingBytes = mLength; | 381 mRemainingBytes = mLength; |
384 } | 382 } |
385 | 383 |
386 @Override | |
387 public long getLength() { | 384 public long getLength() { |
388 return mLength; | 385 return mLength; |
389 } | 386 } |
390 | 387 |
391 @Override | |
392 public void read(UploadDataSink uploadDataSink, ByteBuffer byteB
uffer) { | 388 public void read(UploadDataSink uploadDataSink, ByteBuffer byteB
uffer) { |
393 mBuffer.clear(); | 389 mBuffer.clear(); |
394 // Don't post more than |mLength|. | 390 // Don't post more than |mLength|. |
395 if (mRemainingBytes < mBuffer.limit()) { | 391 if (mRemainingBytes < mBuffer.limit()) { |
396 mBuffer.limit(mRemainingBytes); | 392 mBuffer.limit(mRemainingBytes); |
397 } | 393 } |
398 // Don't overflow |byteBuffer|. | 394 // Don't overflow |byteBuffer|. |
399 if (byteBuffer.remaining() < mBuffer.limit()) { | 395 if (byteBuffer.remaining() < mBuffer.limit()) { |
400 mBuffer.limit(byteBuffer.remaining()); | 396 mBuffer.limit(byteBuffer.remaining()); |
401 } | 397 } |
402 byteBuffer.put(mBuffer); | 398 byteBuffer.put(mBuffer); |
403 mRemainingBytes -= mBuffer.position(); | 399 mRemainingBytes -= mBuffer.position(); |
404 uploadDataSink.onReadSucceeded(false); | 400 uploadDataSink.onReadSucceeded(false); |
405 } | 401 } |
406 | 402 |
407 @Override | |
408 public void rewind(UploadDataSink uploadDataSink) { | 403 public void rewind(UploadDataSink uploadDataSink) { |
409 uploadDataSink.onRewindError(new Exception("no rewinding")); | 404 uploadDataSink.onRewindError(new Exception("no rewinding")); |
410 } | 405 } |
411 } | 406 } |
412 | 407 |
413 private class Callback extends UrlRequest.Callback { | 408 private class Callback extends UrlRequest.Callback { |
414 private final ByteBuffer mBuffer; | 409 private final ByteBuffer mBuffer; |
415 private final Runnable mCompletionCallback; | 410 private final Runnable mCompletionCallback; |
416 private int mBytesReceived; | 411 private int mBytesReceived; |
417 | 412 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 } | 489 } |
495 return !mFailed; | 490 return !mFailed; |
496 } | 491 } |
497 } | 492 } |
498 | 493 |
499 /** | 494 /** |
500 * Executes the benchmark, times how long it takes, and records time in
|mResults|. | 495 * Executes the benchmark, times how long it takes, and records time in
|mResults|. |
501 */ | 496 */ |
502 public void run() { | 497 public void run() { |
503 final ExecutorService executor = Executors.newFixedThreadPool(mConcu
rrency); | 498 final ExecutorService executor = Executors.newFixedThreadPool(mConcu
rrency); |
504 final List<Callable<Boolean>> tasks = new ArrayList<>(mIterations); | 499 final List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean
>>(mIterations); |
505 startLogging(); | 500 startLogging(); |
506 // Prepare list of tasks to run. | 501 // Prepare list of tasks to run. |
507 switch (mMode) { | 502 switch (mMode) { |
508 case SYSTEM_HUC: | 503 case SYSTEM_HUC: |
509 for (int i = 0; i < mIterations; i++) { | 504 for (int i = 0; i < mIterations; i++) { |
510 tasks.add(new SystemHttpURLConnectionFetchTask()); | 505 tasks.add(new SystemHttpURLConnectionFetchTask()); |
511 } | 506 } |
512 break; | 507 break; |
513 case CRONET_HUC: { | 508 case CRONET_HUC: { |
514 for (int i = 0; i < mIterations; i++) { | 509 for (int i = 0; i < mIterations; i++) { |
515 tasks.add(new CronetHttpURLConnectionFetchTask()); | 510 tasks.add(new CronetHttpURLConnectionFetchTask()); |
516 } | 511 } |
517 break; | 512 break; |
518 } | 513 } |
519 case CRONET_ASYNC: | 514 case CRONET_ASYNC: |
520 tasks.add(new CronetAsyncFetchTask()); | 515 tasks.add(new CronetAsyncFetchTask()); |
521 break; | 516 break; |
522 default: | 517 default: |
523 throw new IllegalArgumentException("Unknown mode: " + mMode)
; | 518 throw new IllegalArgumentException("Unknown mode: " + mMode)
; |
524 } | 519 } |
525 // Execute tasks. | 520 // Execute tasks. |
526 boolean success = true; | 521 boolean success = true; |
527 List<Future<Boolean>> futures = new ArrayList<>(); | 522 List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>(); |
528 try { | 523 try { |
529 startTimer(); | 524 startTimer(); |
530 // If possible execute directly to lessen impact of thread-pool
overhead. | 525 // If possible execute directly to lessen impact of thread-pool
overhead. |
531 if (tasks.size() == 1 || mConcurrency == 1) { | 526 if (tasks.size() == 1 || mConcurrency == 1) { |
532 for (int i = 0; i < tasks.size(); i++) { | 527 for (int i = 0; i < tasks.size(); i++) { |
533 if (!tasks.get(i).call()) { | 528 if (!tasks.get(i).call()) { |
534 success = false; | 529 success = false; |
535 } | 530 } |
536 } | 531 } |
537 } else { | 532 } else { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 } | 607 } |
613 } | 608 } |
614 finish(); | 609 finish(); |
615 return null; | 610 return null; |
616 } | 611 } |
617 } | 612 } |
618 | 613 |
619 @Override | 614 @Override |
620 public void onCreate(Bundle savedInstanceState) { | 615 public void onCreate(Bundle savedInstanceState) { |
621 super.onCreate(savedInstanceState); | 616 super.onCreate(savedInstanceState); |
622 // Initializing application context here due to lack of custom CronetPer
fTestApplication. | 617 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, t
his); |
623 ContextUtils.initApplicationContext(getApplicationContext()); | |
624 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); | |
625 mConfig = getIntent().getData(); | 618 mConfig = getIntent().getData(); |
626 // Execute benchmarks on another thread to avoid networking on main thre
ad. | 619 // Execute benchmarks on another thread to avoid networking on main thre
ad. |
627 new BenchmarkTask().execute(); | 620 new BenchmarkTask().execute(); |
628 } | 621 } |
629 } | 622 } |
OLD | NEW |