Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(624)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequest.java

Issue 2351793003: Implement timing metrics for UrlRequest (Closed)
Patch Set: Small cleanups Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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.impl; 5 package org.chromium.net.impl;
6 6
7 import android.os.SystemClock; 7 import android.os.SystemClock;
8 import android.support.annotation.Nullable; 8 import android.support.annotation.Nullable;
9 import android.util.Log; 9 import android.util.Log;
10 10
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 58
59 @GuardedBy("mUrlRequestAdapterLock") 59 @GuardedBy("mUrlRequestAdapterLock")
60 private boolean mStarted = false; 60 private boolean mStarted = false;
61 @GuardedBy("mUrlRequestAdapterLock") 61 @GuardedBy("mUrlRequestAdapterLock")
62 private boolean mWaitingOnRedirect = false; 62 private boolean mWaitingOnRedirect = false;
63 @GuardedBy("mUrlRequestAdapterLock") 63 @GuardedBy("mUrlRequestAdapterLock")
64 private boolean mWaitingOnRead = false; 64 private boolean mWaitingOnRead = false;
65 @GuardedBy("mUrlRequestAdapterLock") 65 @GuardedBy("mUrlRequestAdapterLock")
66 @Nullable 66 @Nullable
67 private final UrlRequestMetricsAccumulator mRequestMetricsAccumulator; 67 private final UrlRequestMetricsAccumulator mRequestMetricsAccumulator;
68 @GuardedBy("mUrlRequestAdapterLock")
69 private RequestFinishedInfo.Metrics mMetrics;
68 70
69 /* 71 /*
70 * Synchronize access to mUrlRequestAdapter, mStarted, mWaitingOnRedirect, 72 * Synchronize access to mUrlRequestAdapter, mStarted, mWaitingOnRedirect,
71 * and mWaitingOnRead. 73 * and mWaitingOnRead.
72 */ 74 */
73 private final Object mUrlRequestAdapterLock = new Object(); 75 private final Object mUrlRequestAdapterLock = new Object();
74 private final CronetUrlRequestContext mRequestContext; 76 private final CronetUrlRequestContext mRequestContext;
75 private final Executor mExecutor; 77 private final Executor mExecutor;
76 78
77 /* 79 /*
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 } 196 }
195 197
196 @Override 198 @Override
197 public void start() { 199 public void start() {
198 synchronized (mUrlRequestAdapterLock) { 200 synchronized (mUrlRequestAdapterLock) {
199 checkNotStarted(); 201 checkNotStarted();
200 202
201 try { 203 try {
202 mUrlRequestAdapter = 204 mUrlRequestAdapter =
203 nativeCreateRequestAdapter(mRequestContext.getUrlRequest ContextAdapter(), 205 nativeCreateRequestAdapter(mRequestContext.getUrlRequest ContextAdapter(),
204 mInitialUrl, mPriority, mDisableCache, mDisableC onnectionMigration); 206 mInitialUrl, mPriority, mDisableCache, mDisableC onnectionMigration,
207 mRequestContext.hasRequestFinishedListener());
205 mRequestContext.onRequestStarted(); 208 mRequestContext.onRequestStarted();
206 if (mInitialMethod != null) { 209 if (mInitialMethod != null) {
207 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod) ) { 210 if (!nativeSetHttpMethod(mUrlRequestAdapter, mInitialMethod) ) {
208 throw new IllegalArgumentException("Invalid http method " + mInitialMethod); 211 throw new IllegalArgumentException("Invalid http method " + mInitialMethod);
209 } 212 }
210 } 213 }
211 214
212 boolean hasContentType = false; 215 boolean hasContentType = false;
213 for (Map.Entry<String, String> header : mRequestHeaders) { 216 for (Map.Entry<String, String> header : mRequestHeaders) {
214 if (header.getKey().equalsIgnoreCase("Content-Type") 217 if (header.getKey().equalsIgnoreCase("Content-Type")
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 425
423 private void destroyRequestAdapter(boolean sendOnCanceled) { 426 private void destroyRequestAdapter(boolean sendOnCanceled) {
424 synchronized (mUrlRequestAdapterLock) { 427 synchronized (mUrlRequestAdapterLock) {
425 if (mUrlRequestAdapter == 0) { 428 if (mUrlRequestAdapter == 0) {
426 return; 429 return;
427 } 430 }
428 if (mRequestMetricsAccumulator != null) { 431 if (mRequestMetricsAccumulator != null) {
429 mRequestMetricsAccumulator.onRequestFinished(); 432 mRequestMetricsAccumulator.onRequestFinished();
430 } 433 }
431 nativeDestroy(mUrlRequestAdapter, sendOnCanceled); 434 nativeDestroy(mUrlRequestAdapter, sendOnCanceled);
432 mRequestContext.reportFinished(getRequestFinishedInfo());
433 mRequestContext.onRequestDestroyed(); 435 mRequestContext.onRequestDestroyed();
434 mUrlRequestAdapter = 0; 436 mUrlRequestAdapter = 0;
435 if (mOnDestroyedCallbackForTesting != null) { 437 if (mOnDestroyedCallbackForTesting != null) {
436 mOnDestroyedCallbackForTesting.run(); 438 mOnDestroyedCallbackForTesting.run();
437 } 439 }
438 } 440 }
439 } 441 }
440 442
441 /** 443 /**
442 * If callback method throws an exception, request gets canceled 444 * If callback method throws an exception, request gets canceled
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 private void onStatus(final UrlRequest.StatusListener listener, final int lo adState) { 694 private void onStatus(final UrlRequest.StatusListener listener, final int lo adState) {
693 Runnable task = new Runnable() { 695 Runnable task = new Runnable() {
694 @Override 696 @Override
695 public void run() { 697 public void run() {
696 listener.onStatus(UrlRequest.Status.convertLoadState(loadState)) ; 698 listener.onStatus(UrlRequest.Status.convertLoadState(loadState)) ;
697 } 699 }
698 }; 700 };
699 postTaskToExecutor(task); 701 postTaskToExecutor(task);
700 } 702 }
701 703
704 /**
705 * Called by the native code to report metrics.
706 */
707 @SuppressWarnings("unused")
708 @CalledByNative
709 private void onMetricsCollected(long requestStartMs, long dnsStartMs, long d nsEndMs,
710 long connectStartMs, long connectEndMs, long sslStartMs, long sslEnd Ms,
711 long sendingStartMs, long sendingEndMs, long pushStartMs, long pushE ndMs,
712 long responseStartMs, long responseEndMs, boolean socketReused, long sentBytesCount,
713 long receivedBytesCount) {
714 synchronized (mUrlRequestAdapterLock) {
715 if (mMetrics != null) {
716 throw new IllegalStateException("Metrics collection should only happen once.");
717 }
718 mMetrics = new CronetMetrics(requestStartMs, dnsStartMs, dnsEndMs, c onnectStartMs,
719 connectEndMs, sslStartMs, sslEndMs, sendingStartMs, sendingE ndMs, pushStartMs,
720 pushEndMs, responseStartMs, responseEndMs, socketReused, sen tBytesCount,
721 receivedBytesCount);
722 }
723 mRequestContext.reportFinished(getRequestFinishedInfo());
724 }
725
702 private RequestFinishedInfo getRequestFinishedInfo() { 726 private RequestFinishedInfo getRequestFinishedInfo() {
703 // TODO(mgersh): fill in real values for finishedReason and exception 727 // TODO(mgersh): fill in real values for finishedReason and exception
704 return new RequestFinishedInfo(mInitialUrl, mRequestAnnotations, 728 return new RequestFinishedInfo(mInitialUrl, mRequestAnnotations, mMetric s,
705 (mRequestMetricsAccumulator != null ? mRequestMetricsAccumulator .getRequestMetrics()
706 : EMPTY_METRICS),
707 RequestFinishedInfo.SUCCEEDED, mResponseInfo, null); 729 RequestFinishedInfo.SUCCEEDED, mResponseInfo, null);
708 } 730 }
709 731
710 private final class UrlRequestMetricsAccumulator { 732 private final class UrlRequestMetricsAccumulator {
711 @Nullable 733 @Nullable
712 private Long mRequestStartTime; 734 private Long mRequestStartTime;
713 @Nullable 735 @Nullable
714 private Long mTtfbMs; 736 private Long mTtfbMs;
715 @Nullable 737 @Nullable
716 private Long mTotalTimeMs; 738 private Long mTotalTimeMs;
(...skipping 27 matching lines...) Expand all
744 /** Enforces prohibition of direct execution. */ 766 /** Enforces prohibition of direct execution. */
745 void checkCallingThread() { 767 void checkCallingThread() {
746 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) { 768 if (!mAllowDirectExecutor && mRequestContext.isNetworkThread(Thread.curr entThread())) {
747 throw new InlineExecutionProhibitedException(); 769 throw new InlineExecutionProhibitedException();
748 } 770 }
749 } 771 }
750 772
751 // Native methods are implemented in cronet_url_request_adapter.cc. 773 // Native methods are implemented in cronet_url_request_adapter.cc.
752 774
753 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url, 775 private native long nativeCreateRequestAdapter(long urlRequestContextAdapter , String url,
754 int priority, boolean disableCache, boolean disableConnectionMigrati on); 776 int priority, boolean disableCache, boolean disableConnectionMigrati on,
777 boolean enableMetrics);
755 778
756 @NativeClassQualifiedName("CronetURLRequestAdapter") 779 @NativeClassQualifiedName("CronetURLRequestAdapter")
757 private native boolean nativeSetHttpMethod(long nativePtr, String method); 780 private native boolean nativeSetHttpMethod(long nativePtr, String method);
758 781
759 @NativeClassQualifiedName("CronetURLRequestAdapter") 782 @NativeClassQualifiedName("CronetURLRequestAdapter")
760 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value); 783 private native boolean nativeAddRequestHeader(long nativePtr, String name, S tring value);
761 784
762 @NativeClassQualifiedName("CronetURLRequestAdapter") 785 @NativeClassQualifiedName("CronetURLRequestAdapter")
763 private native void nativeStart(long nativePtr); 786 private native void nativeStart(long nativePtr);
764 787
765 @NativeClassQualifiedName("CronetURLRequestAdapter") 788 @NativeClassQualifiedName("CronetURLRequestAdapter")
766 private native void nativeFollowDeferredRedirect(long nativePtr); 789 private native void nativeFollowDeferredRedirect(long nativePtr);
767 790
768 @NativeClassQualifiedName("CronetURLRequestAdapter") 791 @NativeClassQualifiedName("CronetURLRequestAdapter")
769 private native boolean nativeReadData( 792 private native boolean nativeReadData(
770 long nativePtr, ByteBuffer byteBuffer, int position, int capacity); 793 long nativePtr, ByteBuffer byteBuffer, int position, int capacity);
771 794
772 @NativeClassQualifiedName("CronetURLRequestAdapter") 795 @NativeClassQualifiedName("CronetURLRequestAdapter")
773 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled); 796 private native void nativeDestroy(long nativePtr, boolean sendOnCanceled);
774 797
775 @NativeClassQualifiedName("CronetURLRequestAdapter") 798 @NativeClassQualifiedName("CronetURLRequestAdapter")
776 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener); 799 private native void nativeGetStatus(long nativePtr, UrlRequest.StatusListene r listener);
777 } 800 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698