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 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 5 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <map> | |
| 12 #include <string> | |
| 11 | 13 |
| 12 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 14 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 17 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
| 20 #include "net/base/network_quality.h" | |
| 18 | 21 |
| 19 namespace net { | 22 namespace net { |
| 20 | 23 |
| 21 class NetworkQuality; | |
| 22 | |
| 23 // NetworkQualityEstimator provides network quality estimates (quality of the | 24 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 24 // full paths to all origins that have been connected to). | 25 // full paths to all origins that have been connected to). |
| 25 // The estimates are based on the observed organic traffic. | 26 // The estimates are based on the observed organic traffic. |
| 26 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 27 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 27 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 28 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 28 // A single instance of NQE can be attached to multiple URLRequestContexts, | 29 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 29 // thereby increasing the single NQE instance's accuracy by providing more | 30 // thereby increasing the single NQE instance's accuracy by providing more |
| 30 // observed traffic characteristics. | 31 // observed traffic characteristics. |
| 31 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 32 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 32 : public NetworkChangeNotifier::ConnectionTypeObserver { | 33 : public NetworkChangeNotifier::ConnectionTypeObserver { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 44 // Notifies NetworkQualityEstimator that a response has been received. | 45 // Notifies NetworkQualityEstimator that a response has been received. |
| 45 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior | 46 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior |
| 46 // to applying filters (e.g. decompression, SDCH) from request creation time | 47 // to applying filters (e.g. decompression, SDCH) from request creation time |
| 47 // until now. | 48 // until now. |
| 48 // |prefiltered_bytes_read| is the count of the bytes received prior | 49 // |prefiltered_bytes_read| is the count of the bytes received prior |
| 49 // to applying filters in the most recent read. | 50 // to applying filters in the most recent read. |
| 50 void NotifyDataReceived(const URLRequest& request, | 51 void NotifyDataReceived(const URLRequest& request, |
| 51 int64_t cumulative_prefilter_bytes_read, | 52 int64_t cumulative_prefilter_bytes_read, |
| 52 int64_t prefiltered_bytes_read); | 53 int64_t prefiltered_bytes_read); |
| 53 | 54 |
| 55 protected: | |
|
pauljensen
2015/06/18 16:20:26
Is this protected (rather than private) only for t
tbansal1
2015/06/18 20:57:41
Yes, it is only for testing. Added comment. Done.
| |
| 56 // NetworkID is used to uniquely identify a network. | |
| 57 // For the purpose of network quality estimation and caching, a network is | |
| 58 // uniquely identified by a combination of |type| and | |
| 59 // |id|. This approach is unable to distinguish networks with | |
| 60 // same name (e.g., different Wi-Fi networks with same SSID). | |
| 61 struct NetworkID { | |
| 62 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | |
| 63 : type(type), id(id) {} | |
| 64 | |
| 65 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {} | |
| 66 | |
| 67 ~NetworkID() {} | |
| 68 | |
| 69 NetworkID& operator=(const NetworkID& other) { | |
| 70 type = other.type; | |
| 71 id = other.id; | |
| 72 return *this; | |
| 73 } | |
| 74 | |
| 75 // Overloaded because NetworkID is used as key in a map. | |
| 76 bool operator<(const NetworkID& other) const { | |
| 77 return type < other.type || (type == other.type && id < other.id); | |
| 78 } | |
| 79 | |
| 80 // Connection type of the network. | |
| 81 NetworkChangeNotifier::ConnectionType type; | |
| 82 | |
| 83 // Name of this network. This is set to: | |
| 84 // - Wi-Fi SSID (if the user is connected to a Wi-Fi access point and the | |
|
pauljensen
2015/06/19 03:10:07
user->device
remove ()
This will make it like the
tbansal1
2015/06/19 23:34:40
Done.
| |
| 85 // SSID name is available), or | |
| 86 // - MCC/MNC code of the cellular carrier if the device is connected to a | |
| 87 // cellular network, or | |
| 88 // - An empty string in all other cases or if the network name is not | |
| 89 // exposed by platform APIs. | |
| 90 std::string id; | |
| 91 }; | |
| 92 | |
| 93 // Construct a NetworkQualityEstimator instance allowing for test | |
| 94 // configuration. | |
| 95 // Registers for network type change notifications so estimates can be kept | |
| 96 // network specific. | |
| 97 // |allow_local_host_requests_for_tests| should only be true when testing | |
| 98 // against local HTTP server and allows the requests to local host to be | |
| 99 // used for network quality estimation. | |
| 100 // |allow_smaller_responses_for_tests| should only be true when testing | |
| 101 // against local HTTP server and allows the responses smaller than | |
| 102 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds| | |
| 103 // to be used for network quality estimation. | |
| 104 NetworkQualityEstimator(bool allow_local_host_requests_for_tests, | |
| 105 bool allow_smaller_responses_for_tests); | |
| 106 | |
| 107 // Returns true if the cached network quality estimate was successfully read. | |
| 108 bool ReadCachedNetworkQualityEstimate(); | |
| 109 | |
| 110 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 111 // |type| is ignored. | |
| 112 void OnConnectionTypeChanged( | |
| 113 NetworkChangeNotifier::ConnectionType type) override; | |
| 114 | |
| 115 // Returns the number of entries in the network quality cache. | |
| 116 // Used only for testing. | |
| 117 size_t GetNetworkQualityCacheSizeForTests() const; | |
| 118 | |
| 54 private: | 119 private: |
| 55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 120 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 56 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 121 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 57 TestPeakKbpsFastestRTTUpdates); | 122 TestPeakKbpsFastestRTTUpdates); |
| 58 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); | 123 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
| 124 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | |
| 125 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | |
| 126 TestLRUCacheMaximumSize); | |
| 59 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 127 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 60 | 128 |
| 129 // CachedNetworkQuality stores the quality of a previously seen network. | |
| 130 class CachedNetworkQuality { | |
| 131 public: | |
| 132 explicit CachedNetworkQuality(const NetworkQuality& network_quality); | |
| 133 | |
| 134 ~CachedNetworkQuality(); | |
| 135 | |
| 136 // Returns the network quality associated with this cached entry. | |
| 137 const NetworkQuality network_quality() const { return network_quality_; } | |
| 138 | |
| 139 // Updates the network quality to the specified |median_kbps| and | |
| 140 // |median_rtt|. | |
| 141 void UpdateNetworkQuality(int32_t median_kbps, | |
| 142 const base::TimeDelta& median_rtt); | |
| 143 | |
| 144 // Returns true if this cache entry was updated before | |
| 145 // |cached_network_quality|. | |
| 146 bool OlderThan(const CachedNetworkQuality& cached_network_quality) const; | |
| 147 | |
| 148 private: | |
| 149 // Time when this cache entry was last updated. | |
| 150 base::TimeTicks last_update_time_; | |
| 151 | |
| 152 // Quality of this cached network. | |
| 153 NetworkQuality network_quality_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality); | |
| 156 }; | |
| 157 | |
| 61 // Records the round trip time or throughput observation, along with the time | 158 // Records the round trip time or throughput observation, along with the time |
| 62 // the observation was made. | 159 // the observation was made. |
| 63 struct Observation { | 160 struct Observation { |
| 64 Observation(int32_t value, base::TimeTicks timestamp); | 161 Observation(int32_t value, base::TimeTicks timestamp); |
| 65 | 162 |
| 66 ~Observation(); | 163 ~Observation(); |
| 67 | 164 |
| 68 // Value of the observation. | 165 // Value of the observation. |
| 69 const int32_t value; | 166 const int32_t value; |
| 70 | 167 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 92 private: | 189 private: |
| 93 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 190 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 94 | 191 |
| 95 // Holds observations sorted by time, with the oldest observation at the | 192 // Holds observations sorted by time, with the oldest observation at the |
| 96 // front of the queue. | 193 // front of the queue. |
| 97 std::deque<Observation> observations_; | 194 std::deque<Observation> observations_; |
| 98 | 195 |
| 99 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | 196 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 100 }; | 197 }; |
| 101 | 198 |
| 199 // This does not use a unordered_map or hash_map for code simplicity (key just | |
| 200 // implements operator<, rather than hash and equality) and because the map is | |
| 201 // tiny. | |
| 202 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> | |
| 203 CachedNetworkQualities; | |
| 204 | |
| 102 // Tiny transfer sizes may give inaccurate throughput results. | 205 // Tiny transfer sizes may give inaccurate throughput results. |
| 103 // Minimum size of the transfer over which the throughput is computed. | 206 // Minimum size of the transfer over which the throughput is computed. |
| 104 static const int kMinTransferSizeInBytes = 10000; | 207 static const int kMinTransferSizeInBytes = 10000; |
| 105 | 208 |
| 106 // Minimum duration (in microseconds) of the transfer over which the | 209 // Minimum duration (in microseconds) of the transfer over which the |
| 107 // throughput is computed. | 210 // throughput is computed. |
| 108 static const int kMinRequestDurationMicroseconds = 1000; | 211 static const int kMinRequestDurationMicroseconds = 1000; |
| 109 | 212 |
| 110 // Construct a NetworkQualityEstimator instance allowing for test | 213 // Maximum size of the cache that holds network quality estimates. |
| 111 // configuration. | 214 // Smaller size may reduce the cache hit rate due to frequent evictions. |
| 112 // Registers for network type change notifications so estimates can be kept | 215 // Larger size may affect performance. |
| 113 // network specific. | 216 static const size_t kMaximumNetworkQualityCacheSize; |
| 114 // |allow_local_host_requests_for_tests| should only be true when testing | |
| 115 // against local HTTP server and allows the requests to local host to be | |
| 116 // used for network quality estimation. | |
| 117 // |allow_smaller_responses_for_tests| should only be true when testing | |
| 118 // against local HTTP server and allows the responses smaller than | |
| 119 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds| | |
| 120 // to be used for network quality estimation. | |
| 121 NetworkQualityEstimator(bool allow_local_host_requests_for_tests, | |
| 122 bool allow_smaller_responses_for_tests); | |
| 123 | 217 |
| 124 // Returns the maximum size of the observation buffer. | 218 // Returns the maximum size of the observation buffers. Used for testing. |
| 125 // Used for testing. | |
| 126 size_t GetMaximumObservationBufferSizeForTests() const; | 219 size_t GetMaximumObservationBufferSizeForTests() const; |
| 127 | 220 |
| 128 // Returns true if the size of all observation buffers is equal to the | 221 // Returns the current size of the Kbps observation buffer. Used for testing. |
| 129 // |expected_size|. Used for testing. | 222 size_t GetKbpsObservationBufferSizeForTests() const; |
| 130 bool VerifyBufferSizeForTests(size_t expected_size) const; | |
| 131 | 223 |
| 132 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 224 // Returns the current size of the RTT observation buffer. Used for testing. |
| 133 void OnConnectionTypeChanged( | 225 size_t GetRTTObservationBufferSizeForTests() const; |
| 134 NetworkChangeNotifier::ConnectionType type) override; | 226 |
| 227 // Returns the current network ID checking by calling the platform APIs. | |
| 228 // Virtualized for testing. | |
| 229 virtual NetworkID GetCurrentNetworkID() const; | |
| 230 | |
| 231 // Writes the estimated quality of the current network to the cache. | |
| 232 void CacheNetworkQualityEstimate(); | |
| 135 | 233 |
| 136 // Determines if the requests to local host can be used in estimating the | 234 // Determines if the requests to local host can be used in estimating the |
| 137 // network quality. Set to true only for tests. | 235 // network quality. Set to true only for tests. |
| 138 const bool allow_localhost_requests_; | 236 const bool allow_localhost_requests_; |
| 139 | 237 |
| 140 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 238 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 141 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 239 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 142 // network quality. Set to true only for tests. | 240 // network quality. Set to true only for tests. |
| 143 const bool allow_small_responses_; | 241 const bool allow_small_responses_; |
| 144 | 242 |
| 145 // Time when last connection change was observed. | 243 // Time when last connection change was observed. |
| 146 base::TimeTicks last_connection_change_; | 244 base::TimeTicks last_connection_change_; |
| 147 | 245 |
| 148 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 246 // ID of the current network. |
| 149 // current connection type. | 247 NetworkID current_network_id_; |
| 150 NetworkChangeNotifier::ConnectionType current_connection_type_; | |
| 151 | 248 |
| 152 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 249 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 153 // from URLRequest creation until first byte received. | 250 // from URLRequest creation until first byte received. |
| 154 base::TimeDelta fastest_rtt_since_last_connection_change_; | 251 base::TimeDelta fastest_rtt_since_last_connection_change_; |
| 155 | 252 |
| 253 // Cache that stores quality of previously seen networks. | |
| 254 CachedNetworkQualities cached_network_qualities_; | |
| 255 | |
| 156 // Rough measurement of downstream peak Kbps witnessed since last connectivity | 256 // Rough measurement of downstream peak Kbps witnessed since last connectivity |
| 157 // change. The accuracy is decreased by ignoring these factors: | 257 // change. The accuracy is decreased by ignoring these factors: |
| 158 // 1) Multiple URLRequests can occur concurrently. | 258 // 1) Multiple URLRequests can occur concurrently. |
| 159 // 2) The transfer time includes at least one RTT while no bytes are read. | 259 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 160 int32_t peak_kbps_since_last_connection_change_; | 260 int32_t peak_kbps_since_last_connection_change_; |
| 161 | 261 |
| 162 // Buffer that holds Kbps observations. | 262 // Buffer that holds Kbps observations. |
| 163 ObservationBuffer kbps_observations_; | 263 ObservationBuffer kbps_observations_; |
| 164 | 264 |
| 165 // Buffer that holds RTT (in milliseconds) observations. | 265 // Buffer that holds RTT (in milliseconds) observations. |
| 166 ObservationBuffer rtt_msec_observations_; | 266 ObservationBuffer rtt_msec_observations_; |
| 167 | 267 |
| 168 base::ThreadChecker thread_checker_; | 268 base::ThreadChecker thread_checker_; |
| 169 | 269 |
| 170 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 270 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 171 }; | 271 }; |
| 172 | 272 |
| 173 } // namespace net | 273 } // namespace net |
| 174 | 274 |
| 175 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 275 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |