Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.support.annotation.IntDef; | |
| 7 import android.support.annotation.Nullable; | 8 import android.support.annotation.Nullable; |
| 8 | 9 |
| 10 import java.lang.annotation.Retention; | |
| 11 import java.lang.annotation.RetentionPolicy; | |
| 9 import java.util.Collection; | 12 import java.util.Collection; |
| 13 import java.util.Date; | |
| 10 import java.util.concurrent.Executor; | 14 import java.util.concurrent.Executor; |
| 11 | 15 |
| 12 /** | 16 /** |
| 13 * Information about a finished request. Passed to {@link RequestFinishedInfo.Li stener}. | 17 * Information about a finished request. Passed to {@link RequestFinishedInfo.Li stener}. |
| 14 * | 18 * |
| 15 * {@hide} as it's a prototype. | 19 * {hide} as it's a prototype. |
| 16 */ | 20 */ |
| 17 public final class RequestFinishedInfo { | 21 public final class RequestFinishedInfo { |
| 18 /** | 22 /** |
| 19 * Listens for finished requests for the purpose of collecting metrics. | 23 * Listens for finished requests for the purpose of collecting metrics. |
| 20 * | 24 * |
| 21 * {@hide} as it's a prototype. | 25 * {hide} as it's a prototype. |
| 22 */ | 26 */ |
| 23 public abstract static class Listener { | 27 public abstract static class Listener { |
| 24 private final Executor mExecutor; | 28 private final Executor mExecutor; |
| 25 | 29 |
| 26 public Listener(Executor executor) { | 30 public Listener(Executor executor) { |
| 27 if (executor == null) { | 31 if (executor == null) { |
| 28 throw new IllegalStateException("Executor must not be null"); | 32 throw new IllegalStateException("Executor must not be null"); |
| 29 } | 33 } |
| 30 mExecutor = executor; | 34 mExecutor = executor; |
| 31 } | 35 } |
| 32 | 36 |
| 33 /** | 37 /** |
| 34 * Invoked with request info. Will be called in a task submitted to the | 38 * Invoked with request info. Will be called in a task submitted to the |
| 35 * {@link java.util.concurrent.Executor} returned by {@link #getExecutor }. | 39 * {@link java.util.concurrent.Executor} returned by {@link #getExecutor }. |
| 36 * @param requestInfo {@link RequestFinishedInfo} for finished request. | 40 * @param requestInfo {@link RequestFinishedInfo} for finished request. |
| 37 */ | 41 */ |
| 38 public abstract void onRequestFinished(RequestFinishedInfo requestInfo); | 42 public abstract void onRequestFinished(RequestFinishedInfo requestInfo); |
| 39 | 43 |
| 40 /** | 44 /** |
| 41 * Returns this listener's executor. Can be called on any thread. | 45 * Returns this listener's executor. Can be called on any thread. |
| 42 * @return this listener's {@link java.util.concurrent.Executor} | 46 * @return this listener's {@link java.util.concurrent.Executor} |
| 43 */ | 47 */ |
| 44 public Executor getExecutor() { | 48 public Executor getExecutor() { |
| 45 return mExecutor; | 49 return mExecutor; |
| 46 } | 50 } |
| 47 } | 51 } |
| 48 | 52 |
| 49 /** | 53 /** |
| 50 * Metrics collected for a single request. | 54 * Metrics collected for a single request. Most of these metrics are timesta mps for events |
| 51 * | 55 * during the lifetime of the request, which can be used to build a detailed timeline for |
| 52 * {@hide} as it's a prototype. | 56 * investigating performance. |
| 53 */ | 57 * |
| 54 public static class Metrics { | 58 * Events happen in this order: |
| 55 @Nullable | 59 * <ol> |
| 56 private final Long mTtfbMs; | 60 * <li>{@link #getRequestStart request start}</li> |
| 57 @Nullable | 61 * <li>{@link #getDnsStart DNS start}</li> |
| 58 private final Long mTotalTimeMs; | 62 * <li>{@link #getDnsEnd DNS end}</li> |
| 59 @Nullable | 63 * <li>{@link #getConnectStart connect start}</li> |
| 60 private final Long mSentBytesCount; | 64 * <li>{@link #getSslStart SSL start}</li> |
| 61 @Nullable | 65 * <li>{@link #getSslEnd SSL end}</li> |
| 62 private final Long mReceivedBytesCount; | 66 * <li>{@link #getConnectEnd connect end}</li> |
| 63 | 67 * <li>{@link #getSendingStart sending start}</li> |
| 64 public Metrics(@Nullable Long ttfbMs, @Nullable Long totalTimeMs, | 68 * <li>{@link #getSendingEnd sending end}</li> |
| 65 @Nullable Long sentBytesCount, @Nullable Long receivedBytesCount ) { | 69 * <li>{@link #getResponseStart response start}</li> |
| 66 mTtfbMs = ttfbMs; | 70 * <li>{@link #getResponseEnd response end}</li> |
| 67 mTotalTimeMs = totalTimeMs; | 71 * </ol> |
| 68 mSentBytesCount = sentBytesCount; | 72 * |
| 69 mReceivedBytesCount = receivedBytesCount; | 73 * Start times are reported as the time when a request started blocking on e vent, not when the |
| 70 } | 74 * event actually occurred, with the exception of push start and end. If a m etric is not |
| 75 * meaningful or not available, including cases when a request finished befo re reaching that | |
| 76 * stage, start and end times will be {@code null}. If no time was spent blo cking on an event, | |
| 77 * start and end will be the same time. | |
| 78 * | |
| 79 * If the system clock is adjusted during the request, some of the {@link ja va.util.Date} values | |
| 80 * might not match it. Timestamps are recorded using a clock that is guarant eed not to run | |
| 81 * backwards. All timestamps are correct relative to the system clock at the time of request | |
| 82 * start, and taking the difference between two timestamps will give the cor rect difference | |
| 83 * between the events. In order to preserve this property, timestamps for ev ents other than | |
| 84 * request start are not guaranteed to match the system clock at the times t hey represent. | |
| 85 * | |
| 86 * Most timing metrics are taken from | |
| 87 * <a | |
| 88 * href="https://cs.chromium.org/chromium/src/net/base/load_timing_info.h">L oadTimingInfo</a>, | |
| 89 * which holds the information for <a href="http://w3c.github.io/navigation- timing/"></a> and | |
| 90 * <a href="https://www.w3.org/TR/resource-timing/"></a>. | |
| 91 * | |
| 92 * {hide} as it's a prototype. | |
|
pauljensen
2016/09/12 11:50:09
hide->@hide
mgersh
2016/09/12 20:55:13
Oops. I was testing and forgot to change it back.
| |
| 93 */ | |
| 94 public abstract static class Metrics { | |
| 95 /** | |
| 96 * Returns time when the request started. | |
| 97 * @return {@link java.util.Date} representing when the native request a ctually started. | |
| 98 * This timestamp will match the system clock at the time it represents. | |
| 99 */ | |
| 100 @Nullable | |
| 101 public abstract Date getRequestStart(); | |
| 102 | |
| 103 /** | |
| 104 * Returns time when DNS lookup started. | |
| 105 * @return {@link java.util.Date} representing when DNS lookup started. {@code null} if the | |
| 106 * socket was reused (see {@link #getSocketReused}). | |
| 107 */ | |
| 108 @Nullable | |
| 109 public abstract Date getDnsStart(); | |
| 110 | |
| 111 /** | |
| 112 * Returns time when DNS lookup finished. | |
|
pauljensen
2016/09/12 11:50:09
We should address what happens if Cronet internall
mgersh
2016/09/12 20:55:13
Done.
| |
| 113 * @return {@link java.util.Date} representing when DNS lookup finished. {@code null} if the | |
| 114 * socket was reused (see {@link #getSocketReused}). | |
| 115 */ | |
| 116 @Nullable | |
| 117 public abstract Date getDnsEnd(); | |
| 118 | |
| 119 /** | |
| 120 * Returns time when connection establishmentn started. | |
|
pauljensen
2016/09/12 11:50:09
extra n on the end of establishment
mgersh
2016/09/12 20:55:13
Done.
| |
| 121 * @return {@link java.util.Date} representing when connection establish ment started, | |
| 122 * typically when DNS resolution finishes. {@code null} if the socket wa s reused (see | |
| 123 * {@link #getSocketReused}). | |
| 124 */ | |
| 125 @Nullable | |
| 126 public abstract Date getConnectStart(); | |
| 127 | |
| 128 /** | |
| 129 * Returns time when connection establishment finished. | |
| 130 * @return {@link java.util.Date} representing when connection establish ment finished, | |
| 131 * after TCP connection is established and, if using HTTPS, SSL handshak e is completed. | |
| 132 * {@code null} if the socket was reused (see {@link #getSocketReused}). | |
| 133 */ | |
| 134 @Nullable | |
| 135 public abstract Date getConnectEnd(); | |
| 136 | |
| 137 /** | |
| 138 * Returns time when SSL handshake started. | |
| 139 * @return {@link java.util.Date} representing when SSL handshake starte d. {@code null} if | |
| 140 * SSL is not used or if the socket was reused (see {@link #getSocketReu sed}). | |
| 141 */ | |
| 142 @Nullable | |
| 143 public abstract Date getSslStart(); | |
| 144 | |
| 145 /** | |
| 146 * Returns time when SSL handshake finished. | |
|
pauljensen
2016/09/12 11:50:09
What are the SSL timestamps for QUIC 0-RTT?
mgersh
2016/09/12 20:55:13
Done.
| |
| 147 * @return {@link java.util.Date} representing when SSL handshake finish ed. {@code null} if | |
| 148 * SSL is not used or if the socket was reused (see {@link #getSocketReu sed}). | |
| 149 */ | |
| 150 @Nullable | |
| 151 public abstract Date getSslEnd(); | |
| 152 | |
| 153 /** | |
| 154 * Returns time when sending the request started. | |
| 155 * @return {@link java.util.Date} representing when sending HTTP request headers started. | |
| 156 */ | |
| 157 @Nullable | |
| 158 public abstract Date getSendingStart(); | |
| 159 | |
| 160 /** | |
| 161 * Returns time when sending the request finished. | |
| 162 * @return {@link java.util.Date} representing when sending HTTP request body finished. | |
| 163 * (Sending request body happens after sending request headers.) | |
| 164 */ | |
| 165 @Nullable | |
| 166 public abstract Date getSendingEnd(); | |
| 167 | |
| 168 /** | |
| 169 * Returns time when first byte of HTTP/2 server push was received. | |
| 170 * @return {@link java.util.Date} representing when the first byte of an HTTP/2 server push | |
| 171 * was received. {@code null} if server push is not used. | |
| 172 */ | |
| 173 @Nullable | |
| 174 public abstract Date getPushStart(); | |
| 175 | |
| 176 /** | |
| 177 * Returns time when last byte of HTTP/2 server push was received. | |
| 178 * @return {@link java.util.Date} representing when the last byte of an HTTP/2 server push | |
| 179 * was received. {@code null} if server push is not used. | |
| 180 */ | |
| 181 @Nullable | |
| 182 public abstract Date getPushEnd(); | |
| 183 | |
| 184 /** | |
| 185 * Returns time when first byte of response was received. | |
|
pauljensen
2016/09/12 11:50:09
Does this include the headers and/or the body?
mgersh
2016/09/12 20:55:13
Took another look at what this is and rewrote it.
| |
| 186 * @return {@link java.util.Date} representing when the first byte of th e response was | |
| 187 * received. | |
| 188 */ | |
| 189 @Nullable | |
| 190 public abstract Date getResponseStart(); | |
| 191 | |
| 192 /** | |
| 193 * Returns time when last byte of response was received. | |
| 194 * @return {@link java.util.Date} representing when the last byte of the response was | |
| 195 * received. | |
| 196 */ | |
| 197 @Nullable | |
| 198 public abstract Date getResponseEnd(); | |
| 199 | |
| 200 /** | |
| 201 * Returns whether the socket was reused from a previous request. | |
| 202 * @return whether this request reused a socket from a previous request. When {@code true}, | |
| 203 * DNS, connection, and SSL times will be {@code null}. | |
|
pauljensen
2016/09/12 11:50:09
What happens for multiplexed streams (i.e. HTTP/2
mgersh
2016/09/12 20:55:13
Done.
| |
| 204 */ | |
| 205 @Nullable | |
| 206 public abstract boolean getSocketReused(); | |
| 71 | 207 |
| 72 /** | 208 /** |
| 73 * Returns milliseconds between request initiation and first byte of res ponse headers, | 209 * Returns milliseconds between request initiation and first byte of res ponse headers, |
| 74 * or null if not collected. | 210 * or {@code null} if not collected. |
| 75 */ | 211 * TODO(mgersh): Remove once new API works http://crbug.com/629194 |
|
pauljensen
2016/09/12 11:50:09
add a @hide here and on line 220; that way if some
mgersh
2016/09/12 20:55:13
Done.
| |
| 76 @Nullable | 212 */ |
| 77 public Long getTtfbMs() { | 213 @Nullable |
| 78 return mTtfbMs; | 214 public abstract Long getTtfbMs(); |
| 79 } | |
| 80 | 215 |
| 81 /** | 216 /** |
| 82 * Returns milliseconds between request initiation and finish, | 217 * Returns milliseconds between request initiation and finish, |
| 83 * including a failure or cancellation, or null if not collected. | 218 * including a failure or cancellation, or {@code null} if not collected . |
| 84 */ | 219 * TODO(mgersh): Remove once new API works http://crbug.com/629194 |
| 85 @Nullable | 220 */ |
| 86 public Long getTotalTimeMs() { | 221 @Nullable |
| 87 return mTotalTimeMs; | 222 public abstract Long getTotalTimeMs(); |
| 88 } | 223 |
| 89 | 224 /** |
| 90 /** | 225 * Returns total bytes sent over the network transport layer, or {@code null} if not |
| 91 * Returns total bytes sent over the network transport layer, or null if not collected. | 226 * collected. |
| 92 */ | 227 */ |
| 93 @Nullable | 228 @Nullable |
| 94 public Long getSentBytesCount() { | 229 public abstract Long getSentBytesCount(); |
| 95 return mSentBytesCount; | 230 |
| 96 } | 231 /** |
| 97 | 232 * Returns total bytes received over the network transport layer, or {@c ode null} if not |
| 98 /** | 233 * collected. |
| 99 * Returns total bytes received over the network transport layer, or nul l if not collected. | 234 */ |
| 100 */ | 235 @Nullable |
| 101 @Nullable | 236 public abstract Long getReceivedBytesCount(); |
| 102 public Long getReceivedBytesCount() { | |
| 103 return mReceivedBytesCount; | |
| 104 } | |
| 105 } | 237 } |
| 106 | 238 |
| 107 private final String mUrl; | 239 private final String mUrl; |
| 108 private final Collection<Object> mAnnotations; | 240 private final Collection<Object> mAnnotations; |
| 109 private final Metrics mMetrics; | 241 private final Metrics mMetrics; |
| 242 | |
| 243 /** @hide */ | |
| 244 @IntDef({SUCCEEDED, FAILED, CANCELED}) | |
| 245 @Retention(RetentionPolicy.SOURCE) | |
| 246 public @interface FinishedReason {} | |
| 247 | |
| 248 /** | |
| 249 * Reason value indicating that the request succeeded. Returned from {@link #getFinishedReason}. | |
| 250 */ | |
| 251 public static final int SUCCEEDED = 0; | |
| 252 /** | |
| 253 * Reason value indicating that the request failed or errored. Returned from | |
| 254 * {@link #getFinishedReason}. | |
| 255 */ | |
| 256 public static final int FAILED = 1; | |
| 257 /** | |
| 258 * Reason value indicating that the request was canceled. Returned from | |
| 259 * {@link #getFinishedReason}. | |
| 260 */ | |
| 261 public static final int CANCELED = 2; | |
| 262 | |
| 263 @FinishedReason | |
| 264 private final int mFinishedReason; | |
| 265 | |
| 110 @Nullable | 266 @Nullable |
| 111 private final UrlResponseInfo mResponseInfo; | 267 private final UrlResponseInfo mResponseInfo; |
| 268 @Nullable | |
| 269 private final UrlRequestException mException; | |
| 112 | 270 |
| 113 /** | 271 /** |
| 114 * @hide only used by internal implementation. | 272 * @hide only used by internal implementation. |
| 115 */ | 273 */ |
| 116 public RequestFinishedInfo(String url, Collection<Object> annotations, Metri cs metrics, | 274 public RequestFinishedInfo(String url, Collection<Object> annotations, Metri cs metrics, |
| 117 @Nullable UrlResponseInfo responseInfo) { | 275 @FinishedReason int finishedReason, @Nullable UrlResponseInfo respon seInfo, |
| 276 @Nullable UrlRequestException exception) { | |
| 118 mUrl = url; | 277 mUrl = url; |
| 119 mAnnotations = annotations; | 278 mAnnotations = annotations; |
| 120 mMetrics = metrics; | 279 mMetrics = metrics; |
| 280 mFinishedReason = finishedReason; | |
| 121 mResponseInfo = responseInfo; | 281 mResponseInfo = responseInfo; |
| 282 mException = exception; | |
| 122 } | 283 } |
| 123 | 284 |
| 124 /** Returns the request's original URL. */ | 285 /** Returns the request's original URL. */ |
| 125 public String getUrl() { | 286 public String getUrl() { |
| 126 return mUrl; | 287 return mUrl; |
| 127 } | 288 } |
| 128 | 289 |
| 129 /** Returns the objects that the caller has supplied when initiating the req uest. */ | 290 /** Returns the objects that the caller has supplied when initiating the req uest. */ |
| 130 public Collection<Object> getAnnotations() { | 291 public Collection<Object> getAnnotations() { |
| 131 return mAnnotations; | 292 return mAnnotations; |
| 132 } | 293 } |
| 133 | 294 |
| 134 // TODO(klm): Collect and return a chain of Metrics objects for redirect res ponses. | 295 // TODO(klm): Collect and return a chain of Metrics objects for redirect res ponses. |
| 296 // TODO(mgersh): Update this javadoc when new metrics are fully implemented | |
| 135 /** | 297 /** |
| 136 * Returns metrics collected for this request. | 298 * Returns metrics collected for this request. |
| 137 * | 299 * |
| 138 * <p>The reported times and bytes account for all redirects, i.e. | 300 * <p>The reported times and bytes account for all redirects, i.e. |
| 139 * the TTFB is from the start of the original request to the ultimate respon se headers, | 301 * the TTFB is from the start of the original request to the ultimate respon se headers, |
| 140 * the TTLB is from the start of the original request to the end of the ulti mate response, | 302 * the TTLB is from the start of the original request to the end of the ulti mate response, |
| 141 * the received byte count is for all redirects and the ultimate response co mbined. | 303 * the received byte count is for all redirects and the ultimate response co mbined. |
| 142 * These cumulative metric definitions are debatable, but are chosen to make sense | 304 * These cumulative metric definitions are debatable, but are chosen to make sense |
| 143 * for user-facing latency analysis. | 305 * for user-facing latency analysis. |
| 144 * | 306 * |
| 145 * @return metrics collected for this request. | 307 * @return metrics collected for this request. |
| 146 */ | 308 */ |
| 147 public Metrics getMetrics() { | 309 public Metrics getMetrics() { |
| 148 return mMetrics; | 310 return mMetrics; |
| 149 } | 311 } |
| 150 | 312 |
| 151 /** | 313 /** |
| 314 * Returns the reason why the request finished. | |
| 315 * @return one of {@link #SUCCEEDED}, {@link #FAILED}, or {@link #CANCELED} | |
| 316 */ | |
| 317 @FinishedReason | |
| 318 public int getFinishedReason() { | |
| 319 return mFinishedReason; | |
| 320 } | |
| 321 | |
| 322 /** | |
| 152 * Returns a {@link UrlResponseInfo} for the request, if its response had st arted. | 323 * Returns a {@link UrlResponseInfo} for the request, if its response had st arted. |
| 153 * @return {@link UrlResponseInfo} for the request, if its response had star ted. | 324 * @return {@link UrlResponseInfo} for the request, if its response had star ted. |
| 154 */ | 325 */ |
| 155 @Nullable | 326 @Nullable |
| 156 public UrlResponseInfo getResponseInfo() { | 327 public UrlResponseInfo getResponseInfo() { |
| 157 return mResponseInfo; | 328 return mResponseInfo; |
| 158 } | 329 } |
| 330 | |
| 331 /** | |
| 332 * If the request failed, returns the same {@link UrlRequestException} provi ded to | |
| 333 * {@link UrlRequest.Callback#onFailed}. | |
| 334 * | |
| 335 * @return the request's {@link UrlRequestException}, if the request failed | |
| 336 */ | |
| 337 @Nullable | |
| 338 public UrlRequestException getException() { | |
| 339 return mException; | |
| 340 } | |
| 159 } | 341 } |
| OLD | NEW |