| 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. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 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 |
| 55 * during the lifetime of the request, which can be used to build a detailed
timeline for |
| 56 * investigating performance. |
| 57 * |
| 58 * Events happen in this order: |
| 59 * <ol> |
| 60 * <li>{@link #getRequestStart request start}</li> |
| 61 * <li>{@link #getDnsStart DNS start}</li> |
| 62 * <li>{@link #getDnsEnd DNS end}</li> |
| 63 * <li>{@link #getConnectStart connect start}</li> |
| 64 * <li>{@link #getSslStart SSL start}</li> |
| 65 * <li>{@link #getSslEnd SSL end}</li> |
| 66 * <li>{@link #getConnectEnd connect end}</li> |
| 67 * <li>{@link #getSendingStart sending start}</li> |
| 68 * <li>{@link #getSendingEnd sending end}</li> |
| 69 * <li>{@link #getResponseStart response start}</li> |
| 70 * <li>{@link #getResponseEnd response end}</li> |
| 71 * </ol> |
| 72 * |
| 73 * Start times are reported as the time when a request started blocking on e
vent, not when the |
| 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>. |
| 51 * | 91 * |
| 52 * {@hide} as it's a prototype. | 92 * {@hide} as it's a prototype. |
| 53 */ | 93 */ |
| 54 public static class Metrics { | 94 public abstract static class Metrics { |
| 55 @Nullable | 95 /** |
| 56 private final Long mTtfbMs; | 96 * Returns time when the request started. |
| 57 @Nullable | 97 * @return {@link java.util.Date} representing when the native request a
ctually started. |
| 58 private final Long mTotalTimeMs; | 98 * This timestamp will match the system clock at the time it represents. |
| 59 @Nullable | 99 */ |
| 60 private final Long mSentBytesCount; | 100 @Nullable |
| 61 @Nullable | 101 public abstract Date getRequestStart(); |
| 62 private final Long mReceivedBytesCount; | 102 |
| 63 | 103 /** |
| 64 public Metrics(@Nullable Long ttfbMs, @Nullable Long totalTimeMs, | 104 * Returns time when DNS lookup started. This and {@link #getDnsEnd} wil
l return non-null |
| 65 @Nullable Long sentBytesCount, @Nullable Long receivedBytesCount
) { | 105 * values regardless of whether the result came from a DNS server or the
local cache. |
| 66 mTtfbMs = ttfbMs; | 106 * @return {@link java.util.Date} representing when DNS lookup started.
{@code null} if the |
| 67 mTotalTimeMs = totalTimeMs; | 107 * socket was reused (see {@link #getSocketReused}). |
| 68 mSentBytesCount = sentBytesCount; | 108 */ |
| 69 mReceivedBytesCount = receivedBytesCount; | 109 @Nullable |
| 70 } | 110 public abstract Date getDnsStart(); |
| 111 |
| 112 /** |
| 113 * Returns time when DNS lookup finished. This and {@link #getDnsStart}
will return non-null |
| 114 * values regardless of whether the result came from a DNS server or the
local cache. |
| 115 * @return {@link java.util.Date} representing when DNS lookup finished.
{@code null} if the |
| 116 * socket was reused (see {@link #getSocketReused}). |
| 117 */ |
| 118 @Nullable |
| 119 public abstract Date getDnsEnd(); |
| 120 |
| 121 /** |
| 122 * Returns time when connection establishment started. |
| 123 * @return {@link java.util.Date} representing when connection establish
ment started, |
| 124 * typically when DNS resolution finishes. {@code null} if the socket wa
s reused (see |
| 125 * {@link #getSocketReused}). |
| 126 */ |
| 127 @Nullable |
| 128 public abstract Date getConnectStart(); |
| 129 |
| 130 /** |
| 131 * Returns time when connection establishment finished. |
| 132 * @return {@link java.util.Date} representing when connection establish
ment finished, |
| 133 * after TCP connection is established and, if using HTTPS, SSL handshak
e is completed. |
| 134 * For QUIC 0-RTT, this represents the time of handshake confirmation an
d might happen |
| 135 * later than {@link #getSendingStart}. |
| 136 * {@code null} if the socket was reused (see {@link #getSocketReused}). |
| 137 */ |
| 138 @Nullable |
| 139 public abstract Date getConnectEnd(); |
| 140 |
| 141 /** |
| 142 * Returns time when SSL handshake started. For QUIC, this will be the s
ame time as |
| 143 * {@link #getConnectStart}. |
| 144 * @return {@link java.util.Date} representing when SSL handshake starte
d. {@code null} if |
| 145 * SSL is not used or if the socket was reused (see {@link #getSocketReu
sed}). |
| 146 */ |
| 147 @Nullable |
| 148 public abstract Date getSslStart(); |
| 149 |
| 150 /** |
| 151 * Returns time when SSL handshake finished. For QUIC, this will be the
same time as |
| 152 * {@link #getConnectEnd}. |
| 153 * @return {@link java.util.Date} representing when SSL handshake finish
ed. {@code null} if |
| 154 * SSL is not used or if the socket was reused (see {@link #getSocketReu
sed}). |
| 155 */ |
| 156 @Nullable |
| 157 public abstract Date getSslEnd(); |
| 158 |
| 159 /** |
| 160 * Returns time when sending the request started. |
| 161 * @return {@link java.util.Date} representing when sending HTTP request
headers started. |
| 162 */ |
| 163 @Nullable |
| 164 public abstract Date getSendingStart(); |
| 165 |
| 166 /** |
| 167 * Returns time when sending the request finished. |
| 168 * @return {@link java.util.Date} representing when sending HTTP request
body finished. |
| 169 * (Sending request body happens after sending request headers.) |
| 170 */ |
| 171 @Nullable |
| 172 public abstract Date getSendingEnd(); |
| 173 |
| 174 /** |
| 175 * Returns time when first byte of HTTP/2 server push was received. |
| 176 * @return {@link java.util.Date} representing when the first byte of an
HTTP/2 server push |
| 177 * was received. {@code null} if server push is not used. |
| 178 */ |
| 179 @Nullable |
| 180 public abstract Date getPushStart(); |
| 181 |
| 182 /** |
| 183 * Returns time when last byte of HTTP/2 server push was received. |
| 184 * @return {@link java.util.Date} representing when the last byte of an
HTTP/2 server push |
| 185 * was received. {@code null} if server push is not used. |
| 186 */ |
| 187 @Nullable |
| 188 public abstract Date getPushEnd(); |
| 189 |
| 190 /** |
| 191 * Returns time when the end of the response headers was received. |
| 192 * @return {@link java.util.Date} representing when the end of the respo
nse headers was |
| 193 * received. |
| 194 */ |
| 195 @Nullable |
| 196 public abstract Date getResponseStart(); |
| 197 |
| 198 /** |
| 199 * Returns time when last byte of response was received. |
| 200 * @return {@link java.util.Date} representing when the last byte of the
response was |
| 201 * received. |
| 202 */ |
| 203 @Nullable |
| 204 public abstract Date getResponseEnd(); |
| 205 |
| 206 /** |
| 207 * Returns whether the socket was reused from a previous request. In HTT
P/2 or QUIC, if |
| 208 * streams are multiplexed in a single connection, returns {@code true}
for all streams |
| 209 * after the first. |
| 210 * @return whether this request reused a socket from a previous request.
When {@code true}, |
| 211 * DNS, connection, and SSL times will be {@code null}. |
| 212 */ |
| 213 @Nullable |
| 214 public abstract boolean getSocketReused(); |
| 71 | 215 |
| 72 /** | 216 /** |
| 73 * Returns milliseconds between request initiation and first byte of res
ponse headers, | 217 * Returns milliseconds between request initiation and first byte of res
ponse headers, |
| 74 * or null if not collected. | 218 * or {@code null} if not collected. |
| 75 */ | 219 * TODO(mgersh): Remove once new API works http://crbug.com/629194 |
| 76 @Nullable | 220 * {@hide} |
| 77 public Long getTtfbMs() { | 221 */ |
| 78 return mTtfbMs; | 222 @Nullable |
| 79 } | 223 public abstract Long getTtfbMs(); |
| 80 | 224 |
| 81 /** | 225 /** |
| 82 * Returns milliseconds between request initiation and finish, | 226 * Returns milliseconds between request initiation and finish, |
| 83 * including a failure or cancellation, or null if not collected. | 227 * including a failure or cancellation, or {@code null} if not collected
. |
| 84 */ | 228 * TODO(mgersh): Remove once new API works http://crbug.com/629194 |
| 85 @Nullable | 229 * {@hide} |
| 86 public Long getTotalTimeMs() { | 230 */ |
| 87 return mTotalTimeMs; | 231 @Nullable |
| 88 } | 232 public abstract Long getTotalTimeMs(); |
| 89 | 233 |
| 90 /** | 234 /** |
| 91 * Returns total bytes sent over the network transport layer, or null if
not collected. | 235 * Returns total bytes sent over the network transport layer, or {@code
null} if not |
| 92 */ | 236 * collected. |
| 93 @Nullable | 237 */ |
| 94 public Long getSentBytesCount() { | 238 @Nullable |
| 95 return mSentBytesCount; | 239 public abstract Long getSentBytesCount(); |
| 96 } | 240 |
| 97 | 241 /** |
| 98 /** | 242 * Returns total bytes received over the network transport layer, or {@c
ode null} if not |
| 99 * Returns total bytes received over the network transport layer, or nul
l if not collected. | 243 * collected. |
| 100 */ | 244 */ |
| 101 @Nullable | 245 @Nullable |
| 102 public Long getReceivedBytesCount() { | 246 public abstract Long getReceivedBytesCount(); |
| 103 return mReceivedBytesCount; | |
| 104 } | |
| 105 } | 247 } |
| 106 | 248 |
| 107 private final String mUrl; | 249 private final String mUrl; |
| 108 private final Collection<Object> mAnnotations; | 250 private final Collection<Object> mAnnotations; |
| 109 private final Metrics mMetrics; | 251 private final Metrics mMetrics; |
| 252 |
| 253 /** @hide */ |
| 254 @IntDef({SUCCEEDED, FAILED, CANCELED}) |
| 255 @Retention(RetentionPolicy.SOURCE) |
| 256 public @interface FinishedReason {} |
| 257 |
| 258 /** |
| 259 * Reason value indicating that the request succeeded. Returned from {@link
#getFinishedReason}. |
| 260 */ |
| 261 public static final int SUCCEEDED = 0; |
| 262 /** |
| 263 * Reason value indicating that the request failed or returned an error. Ret
urned from |
| 264 * {@link #getFinishedReason}. |
| 265 */ |
| 266 public static final int FAILED = 1; |
| 267 /** |
| 268 * Reason value indicating that the request was canceled. Returned from |
| 269 * {@link #getFinishedReason}. |
| 270 */ |
| 271 public static final int CANCELED = 2; |
| 272 |
| 273 @FinishedReason |
| 274 private final int mFinishedReason; |
| 275 |
| 110 @Nullable | 276 @Nullable |
| 111 private final UrlResponseInfo mResponseInfo; | 277 private final UrlResponseInfo mResponseInfo; |
| 278 @Nullable |
| 279 private final UrlRequestException mException; |
| 112 | 280 |
| 113 /** | 281 /** |
| 114 * @hide only used by internal implementation. | 282 * @hide only used by internal implementation. |
| 115 */ | 283 */ |
| 116 public RequestFinishedInfo(String url, Collection<Object> annotations, Metri
cs metrics, | 284 public RequestFinishedInfo(String url, Collection<Object> annotations, Metri
cs metrics, |
| 117 @Nullable UrlResponseInfo responseInfo) { | 285 @FinishedReason int finishedReason, @Nullable UrlResponseInfo respon
seInfo, |
| 286 @Nullable UrlRequestException exception) { |
| 118 mUrl = url; | 287 mUrl = url; |
| 119 mAnnotations = annotations; | 288 mAnnotations = annotations; |
| 120 mMetrics = metrics; | 289 mMetrics = metrics; |
| 290 mFinishedReason = finishedReason; |
| 121 mResponseInfo = responseInfo; | 291 mResponseInfo = responseInfo; |
| 292 mException = exception; |
| 122 } | 293 } |
| 123 | 294 |
| 124 /** Returns the request's original URL. */ | 295 /** Returns the request's original URL. */ |
| 125 public String getUrl() { | 296 public String getUrl() { |
| 126 return mUrl; | 297 return mUrl; |
| 127 } | 298 } |
| 128 | 299 |
| 129 /** Returns the objects that the caller has supplied when initiating the req
uest. */ | 300 /** Returns the objects that the caller has supplied when initiating the req
uest. */ |
| 130 public Collection<Object> getAnnotations() { | 301 public Collection<Object> getAnnotations() { |
| 131 return mAnnotations; | 302 return mAnnotations; |
| 132 } | 303 } |
| 133 | 304 |
| 134 // TODO(klm): Collect and return a chain of Metrics objects for redirect res
ponses. | 305 // TODO(klm): Collect and return a chain of Metrics objects for redirect res
ponses. |
| 306 // TODO(mgersh): Update this javadoc when new metrics are fully implemented |
| 135 /** | 307 /** |
| 136 * Returns metrics collected for this request. | 308 * Returns metrics collected for this request. |
| 137 * | 309 * |
| 138 * <p>The reported times and bytes account for all redirects, i.e. | 310 * <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, | 311 * 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, | 312 * 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. | 313 * 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 | 314 * These cumulative metric definitions are debatable, but are chosen to make
sense |
| 143 * for user-facing latency analysis. | 315 * for user-facing latency analysis. |
| 144 * | 316 * |
| 145 * @return metrics collected for this request. | 317 * @return metrics collected for this request. |
| 146 */ | 318 */ |
| 147 public Metrics getMetrics() { | 319 public Metrics getMetrics() { |
| 148 return mMetrics; | 320 return mMetrics; |
| 149 } | 321 } |
| 150 | 322 |
| 151 /** | 323 /** |
| 324 * Returns the reason why the request finished. |
| 325 * @return one of {@link #SUCCEEDED}, {@link #FAILED}, or {@link #CANCELED} |
| 326 */ |
| 327 @FinishedReason |
| 328 public int getFinishedReason() { |
| 329 return mFinishedReason; |
| 330 } |
| 331 |
| 332 /** |
| 152 * Returns a {@link UrlResponseInfo} for the request, if its response had st
arted. | 333 * 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. | 334 * @return {@link UrlResponseInfo} for the request, if its response had star
ted. |
| 154 */ | 335 */ |
| 155 @Nullable | 336 @Nullable |
| 156 public UrlResponseInfo getResponseInfo() { | 337 public UrlResponseInfo getResponseInfo() { |
| 157 return mResponseInfo; | 338 return mResponseInfo; |
| 158 } | 339 } |
| 340 |
| 341 /** |
| 342 * If the request failed, returns the same {@link UrlRequestException} provi
ded to |
| 343 * {@link UrlRequest.Callback#onFailed}. |
| 344 * |
| 345 * @return the request's {@link UrlRequestException}, if the request failed |
| 346 */ |
| 347 @Nullable |
| 348 public UrlRequestException getException() { |
| 349 return mException; |
| 350 } |
| 159 } | 351 } |
| OLD | NEW |