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 <map> | |
| 11 #include <string> | |
| 12 | |
| 10 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 14 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 18 #include "net/base/network_quality.h" | |
| 15 | 19 |
| 16 namespace net { | 20 namespace net { |
| 17 | 21 |
| 18 struct NetworkQuality; | |
| 19 | |
| 20 // NetworkQualityEstimator provides network quality estimates (quality of the | 22 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 21 // full paths to all origins that have been connected to). | 23 // full paths to all origins that have been connected to). |
| 22 // The estimates are based on the observed organic traffic. | 24 // The estimates are based on the observed organic traffic. |
| 23 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 25 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 24 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 26 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 25 // A single instance of NQE can be attached to multiple URLRequestContexts, | 27 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 26 // thereby increasing the single NQE instance's accuracy by providing more | 28 // thereby increasing the single NQE instance's accuracy by providing more |
| 27 // observed traffic characteristics. | 29 // observed traffic characteristics. |
| 28 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 30 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 29 : public NetworkChangeNotifier::ConnectionTypeObserver { | 31 : public NetworkChangeNotifier::ConnectionTypeObserver { |
| 30 public: | 32 public: |
| 31 // Creates a new NetworkQualityEstimator. | 33 // Creates a new NetworkQualityEstimator. |
| 32 NetworkQualityEstimator(); | 34 NetworkQualityEstimator(); |
| 33 | 35 |
| 34 ~NetworkQualityEstimator() override; | 36 ~NetworkQualityEstimator() override; |
| 35 | 37 |
| 36 // Returns an estimate of the current network quality. | 38 // Returns an estimate of the current network quality. |
| 37 NetworkQuality GetEstimate() const; | 39 NetworkQuality GetEstimate() const; |
| 38 | 40 |
| 39 // Notifies NetworkQualityEstimator that a response has been received. | 41 // Notifies NetworkQualityEstimator that a response has been received. |
| 40 // |prefilter_bytes_read| is the count of the bytes received prior to | 42 // |prefilter_bytes_read| is the count of the bytes received prior to |
| 41 // applying filters (e.g. decompression, SDCH) from request creation time | 43 // applying filters (e.g. decompression, SDCH) from request creation time |
| 42 // until now. | 44 // until now. |
| 43 void NotifyDataReceived(const URLRequest& request, | 45 void NotifyDataReceived(const URLRequest& request, |
| 44 int64_t prefilter_bytes_read); | 46 int64_t prefilter_bytes_read); |
| 45 | 47 |
| 48 protected: | |
| 49 // Returns true if the cached network quality estimate was successfully read. | |
| 50 bool ReadCachedNetworkQualityEstimate(); | |
| 51 | |
| 52 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 53 void OnConnectionTypeChanged( | |
| 54 NetworkChangeNotifier::ConnectionType type) override; | |
| 55 | |
| 56 // Returns the number of entries in the cache. Used only for testing. | |
| 57 size_t GetCacheSizeForTests() const; | |
| 58 | |
| 46 private: | 59 private: |
| 47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 60 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 48 TestPeakKbpsFastestRTTUpdates); | 61 TestPeakKbpsFastestRTTUpdates); |
| 62 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | |
| 63 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | |
| 64 TestLRUCacheMaximumSize); | |
| 49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 65 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 50 | 66 |
| 67 // NetworkID is used to uniquely identify a network. | |
| 68 // For the purpose of network quality estimation and caching, a network is | |
| 69 // uniquely identified by a combination of |connection_type_| and | |
| 70 // |network_name_|. This approach is unable to distinguish networks with | |
| 71 // same name (e.g., different Wi-Fi networks with same SSID). | |
| 72 struct NetworkID { | |
| 73 NetworkChangeNotifier::ConnectionType type; | |
| 74 std::string id; | |
| 75 | |
| 76 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | |
| 77 : type(type), id(id) {} | |
| 78 | |
| 79 ~NetworkID() {} | |
| 80 | |
| 81 NetworkID& operator=(const NetworkID& other) { | |
| 82 type = other.type; | |
| 83 id = other.id; | |
| 84 return *this; | |
| 85 } | |
| 86 | |
| 87 // Overloaded because NetworkID is used as key in a map. | |
| 88 bool operator<(const NetworkID& other) const { | |
| 89 return type < other.type || (type == other.type && id < other.id); | |
| 90 } | |
| 91 }; | |
| 92 | |
| 93 // CachedNetworkQuality stores the quality of a previously seen network. | |
| 94 class CachedNetworkQuality { | |
| 95 public: | |
| 96 explicit CachedNetworkQuality(const NetworkQuality& network_quality); | |
| 97 | |
| 98 ~CachedNetworkQuality(); | |
| 99 | |
| 100 // Returns the network quality associated with this cached entry. | |
| 101 const NetworkQuality GetNetworkQuality() const; | |
| 102 | |
| 103 // Updates the network quality to the specified |median_kbps| and | |
| 104 // |median_rtt|. | |
| 105 void UpdateNetworkQuality(uint64_t median_kbps, | |
| 106 const base::TimeDelta& median_rtt); | |
| 107 | |
| 108 // Returns the time when this cached entry was last updated. | |
| 109 base::TimeTicks last_update_time() const { return last_update_time_; } | |
| 110 | |
| 111 private: | |
| 112 // Time when this cache entry was last updated. | |
| 113 base::TimeTicks last_update_time_; | |
| 114 | |
| 115 // Quality of this cached network. | |
| 116 NetworkQuality network_quality_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality); | |
| 119 }; | |
| 120 | |
| 121 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> | |
| 122 CachedNetworkQualities; | |
| 123 | |
| 51 // Tiny transfer sizes may give inaccurate throughput results. | 124 // Tiny transfer sizes may give inaccurate throughput results. |
| 52 // Minimum size of the transfer over which the throughput is computed. | 125 // Minimum size of the transfer over which the throughput is computed. |
| 53 static const int kMinTransferSizeInBytes = 10000; | 126 static const int kMinTransferSizeInBytes = 10000; |
| 54 | 127 |
| 55 // Minimum duration (in microseconds) of the transfer over which the | 128 // Minimum duration (in microseconds) of the transfer over which the |
| 56 // throughput is computed. | 129 // throughput is computed. |
| 57 static const int kMinRequestDurationMicroseconds = 1000; | 130 static const int kMinRequestDurationMicroseconds = 1000; |
| 58 | 131 |
| 132 // Maximum size of the cache that holds network quality estimates. | |
| 133 // Smaller size may reduce the cache hit rate due to frequent evictions. | |
| 134 // Larger size may affect performance. | |
| 135 static const uint32_t kMaximumNetworkQualityCacheSize; | |
| 136 | |
| 59 // Construct a NetworkQualityEstimator instance allowing for test | 137 // Construct a NetworkQualityEstimator instance allowing for test |
| 60 // configuration. | 138 // configuration. |
| 61 // Registers for network type change notifications so estimates can be kept | 139 // Registers for network type change notifications so estimates can be kept |
| 62 // network specific. | 140 // network specific. |
| 63 // |allow_local_host_requests_for_tests| should only be true when testing | 141 // |allow_local_host_requests_for_tests| should only be true when testing |
| 64 // against local HTTP server and allows the requests to local host to be | 142 // against local HTTP server and allows the requests to local host to be |
| 65 // used for network quality estimation. | 143 // used for network quality estimation. |
| 66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); | 144 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| 67 | 145 |
| 68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 146 // Updates the current network name to: |
| 69 void OnConnectionTypeChanged( | 147 // WiFi SSID (if the user is connected to a WiFi access point and the SSID |
| 70 NetworkChangeNotifier::ConnectionType type) override; | 148 // name is available), or |
| 149 // the MCC/MNC code of the cellular carrier if the device is connected to a | |
| 150 // cellular network. | |
| 151 // Updates the current network name to an empty string in all other cases or | |
| 152 // if the network name is not exposed by platform APIs. | |
| 153 // Virtualized for testing. | |
| 154 virtual std::string GetCurrentNetworkName() const; | |
| 155 | |
| 156 // Writes the estimated quality of the current network to the cache. | |
| 157 void CacheNetworkQualityEstimate(); | |
| 71 | 158 |
| 72 // Determines if the requests to local host can be used in estimating the | 159 // Determines if the requests to local host can be used in estimating the |
| 73 // network quality. Set to true only for tests. | 160 // network quality. Set to true only for tests. |
| 74 const bool allow_localhost_requests_; | 161 const bool allow_localhost_requests_; |
| 75 | 162 |
| 76 // Time when last connection change was observed. | 163 // Time when last connection change was observed. |
| 77 base::TimeTicks last_connection_change_; | 164 base::TimeTicks last_connection_change_; |
| 78 | 165 |
| 79 // Last value passed to |OnConnectionTypeChanged|. This indicates the | |
| 80 // current connection type. | |
| 81 NetworkChangeNotifier::ConnectionType current_connection_type_; | |
| 82 | |
| 83 // Set if any network data has been received since last connectivity change. | 166 // Set if any network data has been received since last connectivity change. |
| 84 bool bytes_read_since_last_connection_change_; | 167 bool bytes_read_since_last_connection_change_; |
| 85 | 168 |
| 86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 169 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 87 // from URLRequest creation until first byte received. | 170 // from URLRequest creation until first byte received. |
| 88 base::TimeDelta fastest_RTT_since_last_connection_change_; | 171 base::TimeDelta fastest_RTT_since_last_connection_change_; |
| 89 | 172 |
| 173 // Cache to store quality of previously seen networks. | |
| 174 CachedNetworkQualities cached_network_quality_; | |
|
bengr
2015/06/13 00:28:55
cached_network_qualities_
| |
| 175 | |
| 90 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 176 // Rough measurement of downlink peak Kbps witnessed since last connectivity |
| 91 // change. The accuracy is decreased by ignoring these factors: | 177 // change. The accuracy is decreased by ignoring these factors: |
| 92 // 1) Multiple URLRequests can occur concurrently. | 178 // 1) Multiple URLRequests can occur concurrently. |
| 93 // 2) The transfer time includes at least one RTT while no bytes are read. | 179 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 94 uint64_t peak_kbps_since_last_connection_change_; | 180 uint64_t peak_kbps_since_last_connection_change_; |
| 95 | 181 |
| 182 // ID of the current network. | |
| 183 NetworkID current_network_id_; | |
| 184 | |
| 96 base::ThreadChecker thread_checker_; | 185 base::ThreadChecker thread_checker_; |
| 97 | 186 |
| 98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 187 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 99 }; | 188 }; |
| 100 | 189 |
| 101 } // namespace net | 190 } // namespace net |
| 102 | 191 |
| 103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 192 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |