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