Chromium Code Reviews| 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 #include "net/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 12 #include "net/base/network_quality.h" | 12 #include "net/base/network_quality.h" |
| 13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 // Maximum number of observations to hold in the ObservationBuffer. | |
|
mmenke
2015/06/12 17:34:42
Description should go with the variable declaratio
tbansal1
2015/06/12 19:14:46
Done.
| |
| 19 const size_t NetworkQualityEstimator::ObservationBuffer::kMaximumObservations = | |
| 20 500; | |
|
mmenke
2015/06/12 17:34:42
Your other constants are inlined in the header. C
tbansal1
2015/06/12 19:14:46
Yes, I tried that before: size_t causes issues.
| |
| 21 | |
| 18 NetworkQualityEstimator::NetworkQualityEstimator() | 22 NetworkQualityEstimator::NetworkQualityEstimator() |
| 19 : NetworkQualityEstimator(false) { | 23 : NetworkQualityEstimator(false) { |
| 20 } | 24 } |
| 21 | 25 |
| 22 NetworkQualityEstimator::NetworkQualityEstimator( | 26 NetworkQualityEstimator::NetworkQualityEstimator( |
| 23 bool allow_local_host_requests_for_tests) | 27 bool allow_local_host_requests_for_tests) |
| 24 : allow_localhost_requests_(allow_local_host_requests_for_tests), | 28 : allow_localhost_requests_(allow_local_host_requests_for_tests), |
| 25 last_connection_change_(base::TimeTicks::Now()), | 29 last_connection_change_(base::TimeTicks::Now()), |
| 26 current_connection_type_(NetworkChangeNotifier::GetConnectionType()), | 30 current_connection_type_(NetworkChangeNotifier::GetConnectionType()), |
| 27 bytes_read_since_last_connection_change_(false), | 31 fastest_rtt_since_last_connection_change_(base::TimeDelta::Max()), |
| 28 peak_kbps_since_last_connection_change_(0) { | 32 peak_kbps_since_last_connection_change_(0) { |
| 29 static_assert(kMinRequestDurationMicroseconds > 0, | 33 static_assert(kMinRequestDurationMicroseconds > 0, |
| 30 "Minimum request duration must be > 0"); | 34 "Minimum request duration must be > 0"); |
| 31 NetworkChangeNotifier::AddConnectionTypeObserver(this); | 35 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 32 } | 36 } |
| 33 | 37 |
| 34 NetworkQualityEstimator::~NetworkQualityEstimator() { | 38 NetworkQualityEstimator::~NetworkQualityEstimator() { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 36 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 40 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 37 } | 41 } |
| 38 | 42 |
| 39 void NetworkQualityEstimator::NotifyDataReceived(const URLRequest& request, | 43 void NetworkQualityEstimator::NotifyDataReceived( |
| 40 int64_t prefilter_bytes_read) { | 44 const URLRequest& request, |
| 45 int64_t cumulative_prefilter_bytes_read, | |
| 46 int64_t prefiltered_bytes_read) { | |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 DCHECK_GT(prefilter_bytes_read, 0); | 48 DCHECK_GT(cumulative_prefilter_bytes_read, 0); |
| 49 DCHECK_GT(prefiltered_bytes_read, 0); | |
| 43 | 50 |
| 44 if (!request.url().is_valid() || | 51 if (!request.url().is_valid() || |
| 45 (!allow_localhost_requests_ && IsLocalhost(request.url().host())) || | 52 (!allow_localhost_requests_ && IsLocalhost(request.url().host())) || |
| 46 !request.url().SchemeIsHTTPOrHTTPS() || | 53 !request.url().SchemeIsHTTPOrHTTPS() || |
| 47 // Verify that response headers are received, so it can be ensured that | 54 // Verify that response headers are received, so it can be ensured that |
| 48 // response is not cached. | 55 // response is not cached. |
| 49 request.response_info().response_time.is_null() || request.was_cached() || | 56 request.response_info().response_time.is_null() || request.was_cached() || |
| 50 request.creation_time() < last_connection_change_) { | 57 request.creation_time() < last_connection_change_) { |
| 51 return; | 58 return; |
| 52 } | 59 } |
| 53 | 60 |
| 54 base::TimeTicks now = base::TimeTicks::Now(); | 61 base::TimeTicks now = base::TimeTicks::Now(); |
| 55 base::TimeDelta request_duration = now - request.creation_time(); | 62 base::TimeDelta request_duration = now - request.creation_time(); |
| 56 DCHECK_GE(request_duration, base::TimeDelta()); | 63 DCHECK_GE(request_duration, base::TimeDelta()); |
| 57 if (!bytes_read_since_last_connection_change_) | |
| 58 fastest_RTT_since_last_connection_change_ = request_duration; | |
| 59 | 64 |
| 60 bytes_read_since_last_connection_change_ = true; | 65 // Only add RTT observation if this is the first read for this response. |
| 61 if (request_duration < fastest_RTT_since_last_connection_change_) | 66 if (cumulative_prefilter_bytes_read == prefiltered_bytes_read) { |
| 62 fastest_RTT_since_last_connection_change_ = request_duration; | 67 if (request_duration < fastest_rtt_since_last_connection_change_) |
| 68 fastest_rtt_since_last_connection_change_ = request_duration; | |
| 69 | |
| 70 rtt_msec_observations_.AddObservation( | |
| 71 Observation(request_duration.InMilliseconds(), now)); | |
| 72 } | |
| 63 | 73 |
| 64 // Ignore tiny transfers which will not produce accurate rates. | 74 // Ignore tiny transfers which will not produce accurate rates. |
| 65 // Ignore short duration transfers. | 75 // Ignore short duration transfers. |
| 66 if (prefilter_bytes_read >= kMinTransferSizeInBytes && | 76 if (cumulative_prefilter_bytes_read >= kMinTransferSizeInBytes && |
| 67 request_duration >= | 77 request_duration >= |
| 68 base::TimeDelta::FromMicroseconds(kMinRequestDurationMicroseconds)) { | 78 base::TimeDelta::FromMicroseconds(kMinRequestDurationMicroseconds)) { |
| 69 uint64_t kbps = static_cast<uint64_t>(prefilter_bytes_read * 8 * 1000 / | 79 int32_t kbps = |
| 70 request_duration.InMicroseconds()); | 80 static_cast<int32_t>(cumulative_prefilter_bytes_read * 8 * 1000 / |
| 71 if (kbps > peak_kbps_since_last_connection_change_) | 81 request_duration.InMicroseconds()); |
| 72 peak_kbps_since_last_connection_change_ = kbps; | 82 |
| 83 if (kbps > 0) { | |
|
mmenke
2015/06/12 17:34:42
This doesn't cover all overflow cases. Consider t
tbansal1
2015/06/12 19:14:46
Fixed but how is 2 gigabits per microsecond == 26
mmenke
2015/06/12 19:36:06
Sorry, my mistake. What I meant was this overflow
tbansal1
2015/06/12 19:56:26
Not sure. Because |cumulative_prefilter_bytes_read
mmenke
2015/06/12 20:31:41
Ah, right. I realized that yesterday, but today i
| |
| 84 if (kbps > peak_kbps_since_last_connection_change_) | |
| 85 peak_kbps_since_last_connection_change_ = kbps; | |
| 86 | |
| 87 kbps_observations_.AddObservation(Observation(kbps, now)); | |
|
mmenke
2015/06/12 18:08:18
Also, I wonder about repeatedly adding observation
tbansal1
2015/06/12 19:14:46
Yes, I have been playing around with it. General o
mmenke
2015/06/12 19:36:06
You could also try subtracting out 1/2 of rtt from
tbansal1
2015/06/12 19:56:26
Good suggestion. Will do that.
| |
| 88 } | |
| 73 } | 89 } |
| 74 } | 90 } |
| 75 | 91 |
| 76 void NetworkQualityEstimator::OnConnectionTypeChanged( | 92 void NetworkQualityEstimator::OnConnectionTypeChanged( |
| 77 NetworkChangeNotifier::ConnectionType type) { | 93 NetworkChangeNotifier::ConnectionType type) { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 if (bytes_read_since_last_connection_change_) { | 95 if (fastest_rtt_since_last_connection_change_ != base::TimeDelta::Max()) { |
| 80 switch (current_connection_type_) { | 96 switch (current_connection_type_) { |
| 81 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | 97 case NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| 82 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Unknown", | 98 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Unknown", |
| 83 fastest_RTT_since_last_connection_change_); | 99 fastest_rtt_since_last_connection_change_); |
| 84 break; | 100 break; |
| 85 case NetworkChangeNotifier::CONNECTION_ETHERNET: | 101 case NetworkChangeNotifier::CONNECTION_ETHERNET: |
| 86 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Ethernet", | 102 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Ethernet", |
| 87 fastest_RTT_since_last_connection_change_); | 103 fastest_rtt_since_last_connection_change_); |
| 88 break; | 104 break; |
| 89 case NetworkChangeNotifier::CONNECTION_WIFI: | 105 case NetworkChangeNotifier::CONNECTION_WIFI: |
| 90 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Wifi", | 106 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Wifi", |
| 91 fastest_RTT_since_last_connection_change_); | 107 fastest_rtt_since_last_connection_change_); |
| 92 break; | 108 break; |
| 93 case NetworkChangeNotifier::CONNECTION_2G: | 109 case NetworkChangeNotifier::CONNECTION_2G: |
| 94 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.2G", | 110 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.2G", |
| 95 fastest_RTT_since_last_connection_change_); | 111 fastest_rtt_since_last_connection_change_); |
| 96 break; | 112 break; |
| 97 case NetworkChangeNotifier::CONNECTION_3G: | 113 case NetworkChangeNotifier::CONNECTION_3G: |
| 98 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.3G", | 114 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.3G", |
| 99 fastest_RTT_since_last_connection_change_); | 115 fastest_rtt_since_last_connection_change_); |
| 100 break; | 116 break; |
| 101 case NetworkChangeNotifier::CONNECTION_4G: | 117 case NetworkChangeNotifier::CONNECTION_4G: |
| 102 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.4G", | 118 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.4G", |
| 103 fastest_RTT_since_last_connection_change_); | 119 fastest_rtt_since_last_connection_change_); |
| 104 break; | 120 break; |
| 105 case NetworkChangeNotifier::CONNECTION_NONE: | 121 case NetworkChangeNotifier::CONNECTION_NONE: |
| 106 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.None", | 122 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.None", |
| 107 fastest_RTT_since_last_connection_change_); | 123 fastest_rtt_since_last_connection_change_); |
| 108 break; | 124 break; |
| 109 case NetworkChangeNotifier::CONNECTION_BLUETOOTH: | 125 case NetworkChangeNotifier::CONNECTION_BLUETOOTH: |
| 110 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Bluetooth", | 126 UMA_HISTOGRAM_TIMES("NQE.FastestRTT.Bluetooth", |
| 111 fastest_RTT_since_last_connection_change_); | 127 fastest_rtt_since_last_connection_change_); |
| 112 break; | 128 break; |
| 113 default: | 129 default: |
| 114 NOTREACHED(); | 130 NOTREACHED(); |
| 115 break; | 131 break; |
| 116 } | 132 } |
| 117 } | 133 } |
| 118 | 134 |
| 119 if (peak_kbps_since_last_connection_change_) { | 135 if (peak_kbps_since_last_connection_change_) { |
| 120 switch (current_connection_type_) { | 136 switch (current_connection_type_) { |
| 121 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | 137 case NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 150 UMA_HISTOGRAM_COUNTS("NQE.PeakKbps.Bluetooth", | 166 UMA_HISTOGRAM_COUNTS("NQE.PeakKbps.Bluetooth", |
| 151 peak_kbps_since_last_connection_change_); | 167 peak_kbps_since_last_connection_change_); |
| 152 break; | 168 break; |
| 153 default: | 169 default: |
| 154 NOTREACHED(); | 170 NOTREACHED(); |
| 155 break; | 171 break; |
| 156 } | 172 } |
| 157 } | 173 } |
| 158 | 174 |
| 159 last_connection_change_ = base::TimeTicks::Now(); | 175 last_connection_change_ = base::TimeTicks::Now(); |
| 160 bytes_read_since_last_connection_change_ = false; | |
| 161 peak_kbps_since_last_connection_change_ = 0; | 176 peak_kbps_since_last_connection_change_ = 0; |
| 177 fastest_rtt_since_last_connection_change_ = base::TimeDelta::Max(); | |
| 178 kbps_observations_.Clear(); | |
| 179 rtt_msec_observations_.Clear(); | |
| 162 current_connection_type_ = type; | 180 current_connection_type_ = type; |
| 163 } | 181 } |
| 164 | 182 |
| 165 NetworkQuality NetworkQualityEstimator::GetEstimate() const { | 183 NetworkQuality NetworkQualityEstimator::GetPeakEstimate() const { |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 167 | 185 |
| 168 if (!bytes_read_since_last_connection_change_) { | 186 return NetworkQuality(fastest_rtt_since_last_connection_change_, |
| 169 return NetworkQuality(fastest_RTT_since_last_connection_change_, 0, | 187 peak_kbps_since_last_connection_change_); |
| 170 peak_kbps_since_last_connection_change_, 0); | 188 } |
| 171 } | 189 |
| 172 if (!peak_kbps_since_last_connection_change_) { | 190 NetworkQualityEstimator::Observation::Observation(int32_t value, |
| 173 return NetworkQuality(fastest_RTT_since_last_connection_change_, 0.1, | 191 base::TimeTicks timestamp) |
| 174 peak_kbps_since_last_connection_change_, 0); | 192 : value(value), timestamp(timestamp) { |
| 175 } | 193 DCHECK_GE(value, 0); |
| 176 return NetworkQuality(fastest_RTT_since_last_connection_change_, 0.1, | 194 DCHECK(!timestamp.is_null()); |
| 177 peak_kbps_since_last_connection_change_, 0.1); | 195 } |
| 196 | |
| 197 NetworkQualityEstimator::Observation::~Observation() { | |
| 198 } | |
| 199 | |
| 200 NetworkQualityEstimator::ObservationBuffer::ObservationBuffer() { | |
| 201 static_assert(kMaximumObservations > 0U, | |
| 202 "Minimum size of observation buffer must be > 0"); | |
| 203 } | |
| 204 | |
| 205 NetworkQualityEstimator::ObservationBuffer::~ObservationBuffer() { | |
| 206 } | |
| 207 | |
| 208 void NetworkQualityEstimator::ObservationBuffer::AddObservation( | |
| 209 const Observation& observation) { | |
| 210 DCHECK_LE(observations_.size(), kMaximumObservations); | |
| 211 // Evict the oldest element if the buffer is already full. | |
| 212 if (observations_.size() == kMaximumObservations) | |
| 213 observations_.pop_front(); | |
| 214 | |
| 215 observations_.push_back(observation); | |
| 216 DCHECK_LE(observations_.size(), kMaximumObservations); | |
| 217 } | |
| 218 | |
| 219 size_t NetworkQualityEstimator::ObservationBuffer::Size() const { | |
| 220 return observations_.size(); | |
| 221 } | |
| 222 | |
| 223 void NetworkQualityEstimator::ObservationBuffer::Clear() { | |
| 224 observations_.clear(); | |
| 225 DCHECK(observations_.empty()); | |
| 178 } | 226 } |
| 179 | 227 |
| 180 } // namespace net | 228 } // namespace net |
| OLD | NEW |