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: | |
| 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 // This is a protected member to expose it to tests. | |
| 62 struct NetworkID { | |
| 63 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | |
| 64 : type(type), id(id) {} | |
| 65 | |
|
pauljensen
2015/06/22 18:39:53
Remove new line
tbansal1
2015/06/22 20:28:26
Aah, did not know about the new line style. I thou
| |
| 66 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {} | |
|
pauljensen
2015/06/22 18:39:53
does this need explicit?
tbansal1
2015/06/22 20:28:26
don't think so. From:
http://google-styleguide.goo
pauljensen
2015/06/23 11:52:10
Then please fix CachedNetworkQuality()'s copy cons
tbansal1
2015/07/13 21:21:26
I may be looking it wrong but that's not a copy co
| |
| 67 | |
|
pauljensen
2015/06/22 18:39:53
Remove new line
tbansal1
2015/06/22 20:28:26
Done.
| |
| 68 ~NetworkID() {} | |
| 69 | |
| 70 NetworkID& operator=(const NetworkID& other) { | |
| 71 type = other.type; | |
| 72 id = other.id; | |
| 73 return *this; | |
| 74 } | |
| 75 | |
| 76 // Overloaded because NetworkID is used as key in a map. | |
| 77 bool operator<(const NetworkID& other) const { | |
| 78 return type < other.type || (type == other.type && id < other.id); | |
| 79 } | |
| 80 | |
| 81 // Connection type of the network. | |
| 82 NetworkChangeNotifier::ConnectionType type; | |
| 83 | |
| 84 // Name of this network. This is set to: | |
| 85 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the | |
| 86 // SSID name is available, or | |
| 87 // - MCC/MNC code of the cellular carrier if the device is connected to a | |
| 88 // cellular network, or | |
| 89 // - An empty string in all other cases or if the network name is not | |
| 90 // exposed by platform APIs. | |
| 91 std::string id; | |
| 92 }; | |
| 93 | |
| 94 // Construct a NetworkQualityEstimator instance allowing for test | |
| 95 // configuration. | |
| 96 // Registers for network type change notifications so estimates can be kept | |
| 97 // network specific. | |
| 98 // |allow_local_host_requests_for_tests| should only be true when testing | |
| 99 // against local HTTP server and allows the requests to local host to be | |
| 100 // used for network quality estimation. | |
| 101 // |allow_smaller_responses_for_tests| should only be true when testing | |
| 102 // against local HTTP server and allows the responses smaller than | |
| 103 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds| | |
| 104 // to be used for network quality estimation. | |
| 105 NetworkQualityEstimator(bool allow_local_host_requests_for_tests, | |
| 106 bool allow_smaller_responses_for_tests); | |
| 107 | |
| 108 // Returns true if the cached network quality estimate was successfully read. | |
| 109 bool ReadCachedNetworkQualityEstimate(); | |
| 110 | |
| 111 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 112 // |type| is ignored. | |
| 113 void OnConnectionTypeChanged( | |
| 114 NetworkChangeNotifier::ConnectionType type) override; | |
| 115 | |
| 116 // Returns the number of entries in the network quality cache. | |
| 117 // Used only for testing. | |
| 118 size_t GetNetworkQualityCacheSizeForTests() const; | |
| 119 | |
| 54 private: | 120 private: |
| 55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 121 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 56 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 122 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 57 TestPeakKbpsFastestRTTUpdates); | 123 TestPeakKbpsFastestRTTUpdates); |
| 58 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); | 124 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
| 125 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | |
| 126 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | |
| 127 TestLRUCacheMaximumSize); | |
| 59 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 128 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 129 friend class ObservationBuffer; | |
|
pauljensen
2015/06/22 18:39:52
What is this needed for?
tbansal1
2015/06/22 20:28:26
Done.
| |
| 130 | |
| 131 // CachedNetworkQuality stores the quality of a previously seen network. | |
| 132 class CachedNetworkQuality { | |
| 133 public: | |
| 134 explicit CachedNetworkQuality(const NetworkQuality& network_quality); | |
| 135 | |
|
pauljensen
2015/06/22 18:39:53
Remove new line
tbansal1
2015/06/22 20:28:26
Done.
| |
| 136 ~CachedNetworkQuality(); | |
| 137 | |
| 138 // Returns the network quality associated with this cached entry. | |
| 139 const NetworkQuality network_quality() const { return network_quality_; } | |
| 140 | |
| 141 // Updates the network quality to the specified |median_kbps| and | |
| 142 // |median_rtt|. | |
| 143 void UpdateNetworkQuality(int32_t median_kbps, | |
| 144 const base::TimeDelta& median_rtt); | |
| 145 | |
| 146 // Returns true if this cache entry was updated before | |
| 147 // |cached_network_quality|. | |
| 148 bool OlderThan(const CachedNetworkQuality& cached_network_quality) const; | |
| 149 | |
| 150 private: | |
| 151 // Time when this cache entry was last updated. | |
| 152 base::TimeTicks last_update_time_; | |
| 153 | |
| 154 // Quality of this cached network. | |
| 155 NetworkQuality network_quality_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality); | |
| 158 }; | |
| 60 | 159 |
| 61 // Records the round trip time or throughput observation, along with the time | 160 // Records the round trip time or throughput observation, along with the time |
| 62 // the observation was made. | 161 // the observation was made. |
| 63 struct Observation { | 162 struct Observation { |
| 64 Observation(int32_t value, base::TimeTicks timestamp); | 163 Observation(int32_t value, base::TimeTicks timestamp); |
| 65 | 164 |
|
pauljensen
2015/06/22 18:39:52
Remove new line
tbansal1
2015/06/22 20:28:26
Done.
| |
| 66 ~Observation(); | 165 ~Observation(); |
| 67 | 166 |
| 68 // Value of the observation. | 167 // Value of the observation. |
| 69 const int32_t value; | 168 const int32_t value; |
| 70 | 169 |
| 71 // Time when the observation was taken. | 170 // Time when the observation was taken. |
| 72 const base::TimeTicks timestamp; | 171 const base::TimeTicks timestamp; |
| 73 }; | 172 }; |
| 74 | 173 |
| 75 // Stores observations sorted by time. | 174 // Stores observations sorted by time. |
| 76 class ObservationBuffer { | 175 class ObservationBuffer { |
| 77 public: | 176 public: |
| 78 ObservationBuffer(); | 177 ObservationBuffer(); |
| 79 | 178 |
|
pauljensen
2015/06/22 18:39:52
remove new line
tbansal1
2015/06/22 20:28:26
Done.
| |
| 80 ~ObservationBuffer(); | 179 ~ObservationBuffer(); |
| 81 | 180 |
| 82 // Adds |observation| to the buffer. The oldest observation in the buffer | 181 // Adds |observation| to the buffer. The oldest observation in the buffer |
| 83 // will be evicted to make room if the buffer is already full. | 182 // will be evicted to make room if the buffer is already full. |
| 84 void AddObservation(const Observation& observation); | 183 void AddObservation(const Observation& observation); |
| 85 | 184 |
| 86 // Returns the number of observations in this buffer. | 185 // Returns the number of observations in this buffer. |
| 87 size_t Size() const; | 186 size_t Size() const; |
|
pauljensen
2015/06/22 18:39:52
I think this function is dead once the unneeded "F
tbansal1
2015/06/22 20:28:26
Please see the comment on the ForTest() functions.
| |
| 88 | 187 |
| 89 // Clears the observations stored in this buffer. | 188 // Clears the observations stored in this buffer. |
| 90 void Clear(); | 189 void Clear(); |
| 91 | 190 |
| 92 private: | 191 private: |
| 93 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | 192 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 94 | 193 |
| 95 // Holds observations sorted by time, with the oldest observation at the | 194 // Holds observations sorted by time, with the oldest observation at the |
| 96 // front of the queue. | 195 // front of the queue. |
| 97 std::deque<Observation> observations_; | 196 std::deque<Observation> observations_; |
| 98 | 197 |
| 99 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | 198 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 100 }; | 199 }; |
| 101 | 200 |
| 201 // This does not use a unordered_map or hash_map for code simplicity (key just | |
| 202 // implements operator<, rather than hash and equality) and because the map is | |
| 203 // tiny. | |
| 204 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> | |
| 205 CachedNetworkQualities; | |
| 206 | |
| 102 // Tiny transfer sizes may give inaccurate throughput results. | 207 // Tiny transfer sizes may give inaccurate throughput results. |
| 103 // Minimum size of the transfer over which the throughput is computed. | 208 // Minimum size of the transfer over which the throughput is computed. |
| 104 static const int kMinTransferSizeInBytes = 10000; | 209 static const int kMinTransferSizeInBytes = 10000; |
| 105 | 210 |
| 106 // Minimum duration (in microseconds) of the transfer over which the | 211 // Minimum duration (in microseconds) of the transfer over which the |
| 107 // throughput is computed. | 212 // throughput is computed. |
| 108 static const int kMinRequestDurationMicroseconds = 1000; | 213 static const int kMinRequestDurationMicroseconds = 1000; |
| 109 | 214 |
| 110 // Construct a NetworkQualityEstimator instance allowing for test | 215 // Maximum size of the cache that holds network quality estimates. |
| 111 // configuration. | 216 // Smaller size may reduce the cache hit rate due to frequent evictions. |
| 112 // Registers for network type change notifications so estimates can be kept | 217 // Larger size may affect performance. |
| 113 // network specific. | 218 static const size_t kMaximumNetworkQualityCacheSize = 10; |
| 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 | 219 |
| 124 // Returns the maximum size of the observation buffer. | 220 // Maximum number of observations that can be held in the ObservationBuffer. |
| 125 // Used for testing. | 221 static const size_t kMaximumObservationsBufferSize = 500; |
| 126 size_t GetMaximumObservationBufferSizeForTests() const; | |
| 127 | 222 |
| 128 // Returns true if the size of all observation buffers is equal to the | 223 // Returns the current size of the Kbps observation buffer. Used for testing. |
| 129 // |expected_size|. Used for testing. | 224 size_t GetKbpsObservationBufferSizeForTests() const; |
|
pauljensen
2015/06/22 18:39:53
Can you get rid of this too?
tbansal1
2015/06/22 20:28:26
Can you please explain how I can get rid of them.
pauljensen
2015/06/23 11:52:10
I cannot see the build logs. The build logs get w
tbansal1
2015/07/13 21:21:26
Fixed in a separate CL. Adding NET_EXPORT_PRIVATE
| |
| 130 bool VerifyBufferSizeForTests(size_t expected_size) const; | |
| 131 | 225 |
| 132 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 226 // Returns the current size of the RTT observation buffer. Used for testing. |
| 133 void OnConnectionTypeChanged( | 227 size_t GetRTTObservationBufferSizeForTests() const; |
|
pauljensen
2015/06/22 18:39:52
ditto
tbansal1
2015/06/22 20:28:26
Please see above.
| |
| 134 NetworkChangeNotifier::ConnectionType type) override; | 228 |
| 229 // Returns the current network ID checking by calling the platform APIs. | |
| 230 // Virtualized for testing. | |
| 231 virtual NetworkID GetCurrentNetworkID() const; | |
| 232 | |
| 233 // Writes the estimated quality of the current network to the cache. | |
| 234 void CacheNetworkQualityEstimate(); | |
| 135 | 235 |
| 136 // Determines if the requests to local host can be used in estimating the | 236 // Determines if the requests to local host can be used in estimating the |
| 137 // network quality. Set to true only for tests. | 237 // network quality. Set to true only for tests. |
| 138 const bool allow_localhost_requests_; | 238 const bool allow_localhost_requests_; |
| 139 | 239 |
| 140 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 240 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 141 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 241 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 142 // network quality. Set to true only for tests. | 242 // network quality. Set to true only for tests. |
| 143 const bool allow_small_responses_; | 243 const bool allow_small_responses_; |
| 144 | 244 |
| 145 // Time when last connection change was observed. | 245 // Time when last connection change was observed. |
| 146 base::TimeTicks last_connection_change_; | 246 base::TimeTicks last_connection_change_; |
| 147 | 247 |
| 148 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 248 // ID of the current network. |
| 149 // current connection type. | 249 NetworkID current_network_id_; |
| 150 NetworkChangeNotifier::ConnectionType current_connection_type_; | |
| 151 | 250 |
| 152 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 251 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 153 // from URLRequest creation until first byte received. | 252 // from URLRequest creation until first byte received. |
| 154 base::TimeDelta fastest_rtt_since_last_connection_change_; | 253 base::TimeDelta fastest_rtt_since_last_connection_change_; |
|
pauljensen
2015/06/22 18:39:52
Could we combine fastest_rtt_since_last_connection
tbansal1
2015/06/22 20:28:26
Done.
| |
| 155 | 254 |
| 255 // Cache that stores quality of previously seen networks. | |
| 256 CachedNetworkQualities cached_network_qualities_; | |
| 257 | |
| 156 // Rough measurement of downstream peak Kbps witnessed since last connectivity | 258 // Rough measurement of downstream peak Kbps witnessed since last connectivity |
| 157 // change. The accuracy is decreased by ignoring these factors: | 259 // change. The accuracy is decreased by ignoring these factors: |
| 158 // 1) Multiple URLRequests can occur concurrently. | 260 // 1) Multiple URLRequests can occur concurrently. |
| 159 // 2) The transfer time includes at least one RTT while no bytes are read. | 261 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 160 int32_t peak_kbps_since_last_connection_change_; | 262 int32_t peak_kbps_since_last_connection_change_; |
| 161 | 263 |
| 162 // Buffer that holds Kbps observations. | 264 // Buffer that holds Kbps observations. |
| 163 ObservationBuffer kbps_observations_; | 265 ObservationBuffer kbps_observations_; |
| 164 | 266 |
| 165 // Buffer that holds RTT (in milliseconds) observations. | 267 // Buffer that holds RTT (in milliseconds) observations. |
| 166 ObservationBuffer rtt_msec_observations_; | 268 ObservationBuffer rtt_msec_observations_; |
| 167 | 269 |
| 168 base::ThreadChecker thread_checker_; | 270 base::ThreadChecker thread_checker_; |
| 169 | 271 |
| 170 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 272 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 171 }; | 273 }; |
| 172 | 274 |
| 173 } // namespace net | 275 } // namespace net |
| 174 | 276 |
| 175 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 277 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |