OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.net.impl; |
| 6 |
| 7 import android.support.annotation.Nullable; |
| 8 |
| 9 import org.chromium.base.VisibleForTesting; |
| 10 import org.chromium.net.RequestFinishedInfo; |
| 11 |
| 12 import java.util.Date; |
| 13 |
| 14 /** |
| 15 * Implementation of {@link RequestFinishedInfo.Metrics}. |
| 16 */ |
| 17 @VisibleForTesting |
| 18 public final class CronetMetrics extends RequestFinishedInfo.Metrics { |
| 19 private final long mRequestStartMs; |
| 20 private final long mDnsStartMs; |
| 21 private final long mDnsEndMs; |
| 22 private final long mConnectStartMs; |
| 23 private final long mConnectEndMs; |
| 24 private final long mSslStartMs; |
| 25 private final long mSslEndMs; |
| 26 private final long mSendingStartMs; |
| 27 private final long mSendingEndMs; |
| 28 private final long mPushStartMs; |
| 29 private final long mPushEndMs; |
| 30 private final long mResponseStartMs; |
| 31 private final long mResponseEndMs; |
| 32 private final boolean mSocketReused; |
| 33 |
| 34 // TODO(mgersh): Delete after the switch to the new API http://crbug.com/629
194 |
| 35 @Nullable |
| 36 private final Long mTtfbMs; |
| 37 // TODO(mgersh): Delete after the switch to the new API http://crbug.com/629
194 |
| 38 @Nullable |
| 39 private final Long mTotalTimeMs; |
| 40 @Nullable |
| 41 private final Long mSentBytesCount; |
| 42 @Nullable |
| 43 private final Long mReceivedBytesCount; |
| 44 |
| 45 @Nullable |
| 46 private static Date toDate(long timestamp) { |
| 47 if (timestamp != -1) { |
| 48 return new Date(timestamp); |
| 49 } |
| 50 return null; |
| 51 } |
| 52 |
| 53 private static boolean checkOrder(long start, long end) { |
| 54 // If end doesn't exist, start can be anything, including also not exist
ing |
| 55 // If end exists, start must also exist and be before end |
| 56 return (end >= start && start != -1) || end == -1; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Old-style constructor |
| 61 * TODO(mgersh): Delete after the switch to the new API http://crbug.com/629
194 |
| 62 */ |
| 63 public CronetMetrics(@Nullable Long ttfbMs, @Nullable Long totalTimeMs, |
| 64 @Nullable Long sentBytesCount, @Nullable Long receivedBytesCount) { |
| 65 mTtfbMs = ttfbMs; |
| 66 mTotalTimeMs = totalTimeMs; |
| 67 mSentBytesCount = sentBytesCount; |
| 68 mReceivedBytesCount = receivedBytesCount; |
| 69 |
| 70 // Everything else is -1 (translates to null) for now |
| 71 mRequestStartMs = -1; |
| 72 mDnsStartMs = -1; |
| 73 mDnsEndMs = -1; |
| 74 mConnectStartMs = -1; |
| 75 mConnectEndMs = -1; |
| 76 mSslStartMs = -1; |
| 77 mSslEndMs = -1; |
| 78 mSendingStartMs = -1; |
| 79 mSendingEndMs = -1; |
| 80 mPushStartMs = -1; |
| 81 mPushEndMs = -1; |
| 82 mResponseStartMs = -1; |
| 83 mResponseEndMs = -1; |
| 84 mSocketReused = false; |
| 85 } |
| 86 |
| 87 /** |
| 88 * New-style constructor |
| 89 */ |
| 90 public CronetMetrics(long requestStartMs, long dnsStartMs, long dnsEndMs, lo
ng connectStartMs, |
| 91 long connectEndMs, long sslStartMs, long sslEndMs, long sendingStart
Ms, |
| 92 long sendingEndMs, long pushStartMs, long pushEndMs, long responseSt
artMs, |
| 93 long responseEndMs, boolean socketReused, long sentBytesCount, |
| 94 long receivedBytesCount) { |
| 95 // Check that no end times are before corresponding start times, |
| 96 // or exist when start time doesn't. |
| 97 assert checkOrder(dnsStartMs, dnsEndMs); |
| 98 assert checkOrder(connectStartMs, connectEndMs); |
| 99 assert checkOrder(sslStartMs, sslEndMs); |
| 100 assert checkOrder(sendingStartMs, sendingEndMs); |
| 101 assert checkOrder(pushStartMs, pushEndMs); |
| 102 assert checkOrder(responseStartMs, responseEndMs); |
| 103 // Spot-check some of the other orderings |
| 104 assert dnsStartMs >= requestStartMs || dnsStartMs == -1; |
| 105 assert sendingStartMs >= requestStartMs || sendingStartMs == -1; |
| 106 assert sslStartMs >= connectStartMs || sslStartMs == -1; |
| 107 assert responseStartMs >= sendingStartMs || responseStartMs == -1; |
| 108 mRequestStartMs = requestStartMs; |
| 109 mDnsStartMs = dnsStartMs; |
| 110 mDnsEndMs = dnsEndMs; |
| 111 mConnectStartMs = connectStartMs; |
| 112 mConnectEndMs = connectEndMs; |
| 113 mSslStartMs = sslStartMs; |
| 114 mSslEndMs = sslEndMs; |
| 115 mSendingStartMs = sendingStartMs; |
| 116 mSendingEndMs = sendingEndMs; |
| 117 mPushStartMs = pushStartMs; |
| 118 mPushEndMs = pushEndMs; |
| 119 mResponseStartMs = responseStartMs; |
| 120 mResponseEndMs = responseEndMs; |
| 121 mSocketReused = socketReused; |
| 122 mSentBytesCount = sentBytesCount; |
| 123 mReceivedBytesCount = receivedBytesCount; |
| 124 |
| 125 // Don't care about these anymore |
| 126 mTtfbMs = null; |
| 127 mTotalTimeMs = null; |
| 128 } |
| 129 |
| 130 @Nullable |
| 131 public Date getRequestStart() { |
| 132 return toDate(mRequestStartMs); |
| 133 } |
| 134 |
| 135 @Nullable |
| 136 public Date getDnsStart() { |
| 137 return toDate(mDnsStartMs); |
| 138 } |
| 139 |
| 140 @Nullable |
| 141 public Date getDnsEnd() { |
| 142 return toDate(mDnsEndMs); |
| 143 } |
| 144 |
| 145 @Nullable |
| 146 public Date getConnectStart() { |
| 147 return toDate(mConnectStartMs); |
| 148 } |
| 149 |
| 150 @Nullable |
| 151 public Date getConnectEnd() { |
| 152 return toDate(mConnectEndMs); |
| 153 } |
| 154 |
| 155 @Nullable |
| 156 public Date getSslStart() { |
| 157 return toDate(mSslStartMs); |
| 158 } |
| 159 |
| 160 @Nullable |
| 161 public Date getSslEnd() { |
| 162 return toDate(mSslEndMs); |
| 163 } |
| 164 |
| 165 @Nullable |
| 166 public Date getSendingStart() { |
| 167 return toDate(mSendingStartMs); |
| 168 } |
| 169 |
| 170 @Nullable |
| 171 public Date getSendingEnd() { |
| 172 return toDate(mSendingEndMs); |
| 173 } |
| 174 |
| 175 @Nullable |
| 176 public Date getPushStart() { |
| 177 return toDate(mPushStartMs); |
| 178 } |
| 179 |
| 180 @Nullable |
| 181 public Date getPushEnd() { |
| 182 return toDate(mPushEndMs); |
| 183 } |
| 184 |
| 185 @Nullable |
| 186 public Date getResponseStart() { |
| 187 return toDate(mResponseStartMs); |
| 188 } |
| 189 |
| 190 @Nullable |
| 191 public Date getResponseEnd() { |
| 192 return toDate(mResponseEndMs); |
| 193 } |
| 194 |
| 195 @Nullable |
| 196 public boolean getSocketReused() { |
| 197 return mSocketReused; |
| 198 } |
| 199 |
| 200 @Nullable |
| 201 public Long getTtfbMs() { |
| 202 return mTtfbMs; |
| 203 } |
| 204 |
| 205 @Nullable |
| 206 public Long getTotalTimeMs() { |
| 207 return mTotalTimeMs; |
| 208 } |
| 209 |
| 210 @Nullable |
| 211 public Long getSentBytesCount() { |
| 212 return mSentBytesCount; |
| 213 } |
| 214 |
| 215 @Nullable |
| 216 public Long getReceivedBytesCount() { |
| 217 return mReceivedBytesCount; |
| 218 } |
| 219 } |
OLD | NEW |