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 <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "net/base/load_timing_info.h" |
12 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
13 #include "net/base/network_quality.h" | 14 #include "net/base/network_quality.h" |
14 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Maximum number of observations that can be held in the ObservationBuffer. | 20 // Maximum number of observations that can be held in the ObservationBuffer. |
20 const size_t kMaximumObservationsBufferSize = 500; | 21 const size_t kMaximumObservationsBufferSize = 500; |
21 | 22 |
22 } // namespace | 23 } // namespace |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 | 26 |
26 NetworkQualityEstimator::NetworkQualityEstimator() | 27 NetworkQualityEstimator::NetworkQualityEstimator() |
27 : NetworkQualityEstimator(false) { | 28 : NetworkQualityEstimator(false, false) { |
28 } | 29 } |
29 | 30 |
30 NetworkQualityEstimator::NetworkQualityEstimator( | 31 NetworkQualityEstimator::NetworkQualityEstimator( |
31 bool allow_local_host_requests_for_tests) | 32 bool allow_local_host_requests_for_tests, |
| 33 bool allow_smaller_responses_for_tests) |
32 : allow_localhost_requests_(allow_local_host_requests_for_tests), | 34 : allow_localhost_requests_(allow_local_host_requests_for_tests), |
| 35 allow_small_responses_(allow_smaller_responses_for_tests), |
33 last_connection_change_(base::TimeTicks::Now()), | 36 last_connection_change_(base::TimeTicks::Now()), |
34 current_connection_type_(NetworkChangeNotifier::GetConnectionType()), | 37 current_connection_type_(NetworkChangeNotifier::GetConnectionType()), |
35 fastest_rtt_since_last_connection_change_(base::TimeDelta::Max()), | 38 fastest_rtt_since_last_connection_change_(base::TimeDelta::Max()), |
36 peak_kbps_since_last_connection_change_(0) { | 39 peak_kbps_since_last_connection_change_(0) { |
37 static_assert(kMinRequestDurationMicroseconds > 0, | 40 static_assert(kMinRequestDurationMicroseconds > 0, |
38 "Minimum request duration must be > 0"); | 41 "Minimum request duration must be > 0"); |
39 NetworkChangeNotifier::AddConnectionTypeObserver(this); | 42 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
40 } | 43 } |
41 | 44 |
42 NetworkQualityEstimator::~NetworkQualityEstimator() { | 45 NetworkQualityEstimator::~NetworkQualityEstimator() { |
(...skipping 13 matching lines...) Expand all Loading... |
56 (!allow_localhost_requests_ && IsLocalhost(request.url().host())) || | 59 (!allow_localhost_requests_ && IsLocalhost(request.url().host())) || |
57 !request.url().SchemeIsHTTPOrHTTPS() || | 60 !request.url().SchemeIsHTTPOrHTTPS() || |
58 // Verify that response headers are received, so it can be ensured that | 61 // Verify that response headers are received, so it can be ensured that |
59 // response is not cached. | 62 // response is not cached. |
60 request.response_info().response_time.is_null() || request.was_cached() || | 63 request.response_info().response_time.is_null() || request.was_cached() || |
61 request.creation_time() < last_connection_change_) { | 64 request.creation_time() < last_connection_change_) { |
62 return; | 65 return; |
63 } | 66 } |
64 | 67 |
65 base::TimeTicks now = base::TimeTicks::Now(); | 68 base::TimeTicks now = base::TimeTicks::Now(); |
66 base::TimeDelta request_duration = now - request.creation_time(); | 69 LoadTimingInfo load_timing_info; |
67 DCHECK_GE(request_duration, base::TimeDelta()); | 70 request.GetLoadTimingInfo(&load_timing_info); |
| 71 |
| 72 // If the load timing info is unavailable, it probably means that the request |
| 73 // did not go over the network. |
| 74 if (load_timing_info.send_start.is_null() || |
| 75 load_timing_info.receive_headers_end.is_null()) { |
| 76 return; |
| 77 } |
| 78 |
| 79 // Time when the resource was requested. |
| 80 base::TimeTicks request_start_time = load_timing_info.send_start; |
| 81 |
| 82 // Time when the headers were received. |
| 83 base::TimeTicks headers_received_time = load_timing_info.receive_headers_end; |
68 | 84 |
69 // Only add RTT observation if this is the first read for this response. | 85 // Only add RTT observation if this is the first read for this response. |
70 if (cumulative_prefilter_bytes_read == prefiltered_bytes_read) { | 86 if (cumulative_prefilter_bytes_read == prefiltered_bytes_read) { |
71 if (request_duration < fastest_rtt_since_last_connection_change_) | 87 // Duration between when the resource was requested and when response |
72 fastest_rtt_since_last_connection_change_ = request_duration; | 88 // headers were received. |
| 89 base::TimeDelta observed_rtt = headers_received_time - request_start_time; |
| 90 DCHECK_GE(observed_rtt, base::TimeDelta()); |
| 91 |
| 92 if (observed_rtt < fastest_rtt_since_last_connection_change_) |
| 93 fastest_rtt_since_last_connection_change_ = observed_rtt; |
73 | 94 |
74 rtt_msec_observations_.AddObservation( | 95 rtt_msec_observations_.AddObservation( |
75 Observation(request_duration.InMilliseconds(), now)); | 96 Observation(observed_rtt.InMilliseconds(), now)); |
76 } | 97 } |
77 | 98 |
| 99 // Time since the resource was requested. |
| 100 base::TimeDelta since_request_start = now - request_start_time; |
| 101 DCHECK_GE(since_request_start, base::TimeDelta()); |
| 102 |
78 // Ignore tiny transfers which will not produce accurate rates. | 103 // Ignore tiny transfers which will not produce accurate rates. |
79 // Ignore short duration transfers. | 104 // Ignore short duration transfers. |
80 if (cumulative_prefilter_bytes_read >= kMinTransferSizeInBytes && | 105 // Skip the checks if |allow_small_responses_| is true. |
81 request_duration >= | 106 if (allow_small_responses_ || |
82 base::TimeDelta::FromMicroseconds(kMinRequestDurationMicroseconds)) { | 107 (cumulative_prefilter_bytes_read >= kMinTransferSizeInBytes && |
| 108 since_request_start >= base::TimeDelta::FromMicroseconds( |
| 109 kMinRequestDurationMicroseconds))) { |
83 double kbps_f = cumulative_prefilter_bytes_read * 8.0 / 1000.0 / | 110 double kbps_f = cumulative_prefilter_bytes_read * 8.0 / 1000.0 / |
84 request_duration.InSecondsF(); | 111 since_request_start.InSecondsF(); |
85 DCHECK_GE(kbps_f, 0.0); | 112 DCHECK_GE(kbps_f, 0.0); |
86 | 113 |
87 // Check overflow errors. This may happen if the kbpsF is more than | 114 // Check overflow errors. This may happen if the kbpsF is more than |
88 // 2 * 10^9 (= 2000 Gbps). | 115 // 2 * 10^9 (= 2000 Gbps). |
89 if (kbps_f >= std::numeric_limits<int32_t>::max()) | 116 if (kbps_f >= std::numeric_limits<int32_t>::max()) |
90 kbps_f = std::numeric_limits<int32_t>::max() - 1; | 117 kbps_f = std::numeric_limits<int32_t>::max() - 1; |
91 | 118 |
92 int32_t kbps = static_cast<int32_t>(kbps_f); | 119 int32_t kbps = static_cast<int32_t>(kbps_f); |
93 | 120 |
| 121 // If the |kbps| is less than 1, we set it to 1 to differentiate from case |
| 122 // when there is no connection. |
| 123 if (kbps_f > 0.0 && kbps == 0) |
| 124 kbps = 1; |
| 125 |
94 if (kbps > 0) { | 126 if (kbps > 0) { |
95 if (kbps > peak_kbps_since_last_connection_change_) | 127 if (kbps > peak_kbps_since_last_connection_change_) |
96 peak_kbps_since_last_connection_change_ = kbps; | 128 peak_kbps_since_last_connection_change_ = kbps; |
97 | 129 |
98 kbps_observations_.AddObservation(Observation(kbps, now)); | 130 kbps_observations_.AddObservation(Observation(kbps, now)); |
99 } | 131 } |
100 } | 132 } |
101 } | 133 } |
102 | 134 |
103 void NetworkQualityEstimator::OnConnectionTypeChanged( | 135 void NetworkQualityEstimator::OnConnectionTypeChanged( |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 size_t NetworkQualityEstimator::ObservationBuffer::Size() const { | 273 size_t NetworkQualityEstimator::ObservationBuffer::Size() const { |
242 return observations_.size(); | 274 return observations_.size(); |
243 } | 275 } |
244 | 276 |
245 void NetworkQualityEstimator::ObservationBuffer::Clear() { | 277 void NetworkQualityEstimator::ObservationBuffer::Clear() { |
246 observations_.clear(); | 278 observations_.clear(); |
247 DCHECK(observations_.empty()); | 279 DCHECK(observations_.empty()); |
248 } | 280 } |
249 | 281 |
250 } // namespace net | 282 } // namespace net |
OLD | NEW |