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 "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 // CachedNetworkQuality stores the quality of a previously seen network. | |
| 19 // A network is uniquely identified by combination of |connection_type| and | |
| 20 // |network_name|. | |
| 21 struct NET_EXPORT_PRIVATE CachedNetworkQuality { | |
|
mmenke
2015/05/28 15:25:02
Since a subclass for testing only needs to be able
mmenke
2015/05/28 15:25:02
This should be a protected or private class - priv
tbansal1
2015/05/29 02:27:23
Done.
tbansal1
2015/05/29 02:27:24
Done.
| |
| 22 CachedNetworkQuality(NetworkChangeNotifier::ConnectionType connection_type, | |
| 23 std::string network_name, | |
| 24 int median_kbps, | |
| 25 int median_rtt_milliseconds); | |
| 26 | |
| 27 ~CachedNetworkQuality(); | |
| 28 | |
| 29 void UpdateNetworkQuality(int updated_median_kbps, | |
| 30 int updated_median_rtt_milliseconds); | |
| 31 | |
| 32 // Connection type of this cached network. | |
| 33 NetworkChangeNotifier::ConnectionType connection_type; | |
| 34 | |
| 35 // Name of this cached network. | |
| 36 std::string network_name; | |
| 37 | |
| 38 // Median Kbps of this cached network. | |
| 39 int median_kbps; | |
| 40 | |
| 41 // Median RTT (in milliseconds) of this cached network. | |
| 42 int median_rtt_milliseconds; | |
| 43 | |
| 44 // Time when this cache entry was last updated. | |
| 45 base::TimeTicks last_updated; | |
| 46 }; | |
| 47 | |
| 18 struct NetworkQuality; | 48 struct NetworkQuality; |
| 19 | 49 |
| 20 // NetworkQualityEstimator provides network quality estimates (quality of the | 50 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 21 // full paths to all origins that have been connected to). | 51 // full paths to all origins that have been connected to). |
| 22 // The estimates are based on the observed organic traffic. | 52 // The estimates are based on the observed organic traffic. |
| 23 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 53 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 24 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 54 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 25 // A single instance of NQE can be attached to multiple URLRequestContexts, | 55 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 26 // thereby increasing the single NQE instance's accuracy by providing more | 56 // thereby increasing the single NQE instance's accuracy by providing more |
| 27 // observed traffic characteristics. | 57 // observed traffic characteristics. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 40 // |prefilter_bytes_read| is the count of the bytes received prior to | 70 // |prefilter_bytes_read| is the count of the bytes received prior to |
| 41 // applying filters (e.g. decompression, SDCH) from request creation time | 71 // applying filters (e.g. decompression, SDCH) from request creation time |
| 42 // until now. | 72 // until now. |
| 43 void NotifyDataReceived(const URLRequest& request, | 73 void NotifyDataReceived(const URLRequest& request, |
| 44 int64_t prefilter_bytes_read); | 74 int64_t prefilter_bytes_read); |
| 45 | 75 |
| 46 private: | 76 private: |
| 47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 77 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 48 TestPeakKbpsFastestRTTUpdates); | 78 TestPeakKbpsFastestRTTUpdates); |
| 49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 79 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 80 friend class NetworkQualityEstimatorTest; | |
|
mmenke
2015/05/28 15:25:02
Should not need to friend a subclass. Just make p
tbansal1
2015/05/29 02:27:24
Done.
| |
| 50 | 81 |
| 51 // Tiny transfer sizes may give inaccurate throughput results. | 82 // Tiny transfer sizes may give inaccurate throughput results. |
| 52 // Minimum size of the transfer over which the throughput is computed. | 83 // Minimum size of the transfer over which the throughput is computed. |
| 53 static const int kMinTransferSizeInBytes = 10000; | 84 static const int kMinTransferSizeInBytes = 10000; |
| 54 | 85 |
| 55 // Minimum duration (in microseconds) of the transfer over which the | 86 // Minimum duration (in microseconds) of the transfer over which the |
| 56 // throughput is computed. | 87 // throughput is computed. |
| 57 static const int kMinRequestDurationMicroseconds = 1000; | 88 static const int kMinRequestDurationMicroseconds = 1000; |
| 58 | 89 |
| 59 // Construct a NetworkQualityEstimator instance allowing for test | 90 // Construct a NetworkQualityEstimator instance allowing for test |
| 60 // configuration. | 91 // configuration. |
| 61 // Registers for network type change notifications so estimates can be kept | 92 // Registers for network type change notifications so estimates can be kept |
| 62 // network specific. | 93 // network specific. |
| 63 // |allow_local_host_requests_for_tests| should only be true when testing | 94 // |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 | 95 // against local HTTP server and allows the requests to local host to be |
| 65 // used for network quality estimation. | 96 // used for network quality estimation. |
| 66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); | 97 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| 67 | 98 |
| 68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 99 // NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 69 void OnConnectionTypeChanged( | 100 void OnConnectionTypeChanged( |
| 70 NetworkChangeNotifier::ConnectionType type) override; | 101 NetworkChangeNotifier::ConnectionType type) override; |
| 71 | 102 |
| 103 // Returns the current network name: | |
| 104 // WiFi SSID (if the user is connected to a WiFi access point and the SSID | |
| 105 // name is available), or | |
| 106 // The MCCMNC code of the cellular carrier if the device is connected to a | |
| 107 // cellular network, or | |
| 108 // "ethernet" if the device is connected to an Ethernet network. | |
| 109 // Returns empty string in all other cases or if the network name is not | |
| 110 // exposed by platform APIs. | |
| 111 // Virtualized for testing. | |
| 112 virtual std::string GetCurrentNetworkName() const; | |
|
mmenke
2015/05/28 15:25:02
include <string>
tbansal1
2015/05/29 02:27:24
Done.
| |
| 113 | |
| 114 // Returns true if cached network quality estimate was successfully read. | |
| 115 bool ReadCachedNetworkQualityEstimate(); | |
| 116 | |
| 117 // Write the estimated quality of the current network to the cache. | |
| 118 void CacheNetworkQualityEstimate(); | |
| 119 | |
| 72 // Determines if the requests to local host can be used in estimating the | 120 // Determines if the requests to local host can be used in estimating the |
| 73 // network quality. Set to true only for tests. | 121 // network quality. Set to true only for tests. |
| 74 const bool allow_localhost_requests_; | 122 const bool allow_localhost_requests_; |
| 75 | 123 |
| 76 // Time when last connection change was observed. | 124 // Time when last connection change was observed. |
| 77 base::TimeTicks last_connection_change_; | 125 base::TimeTicks last_connection_change_; |
| 78 | 126 |
| 79 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 127 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| 80 // current connection type. | 128 // current connection type. |
| 81 NetworkChangeNotifier::ConnectionType current_connection_type_; | 129 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 82 | 130 |
| 83 // Set if any network data has been received since last connectivity change. | 131 // Set if any network data has been received since last connectivity change. |
| 84 bool bytes_read_since_last_connection_change_; | 132 bool bytes_read_since_last_connection_change_; |
| 85 | 133 |
| 86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 134 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 87 // from URLRequest creation until first byte received. | 135 // from URLRequest creation until first byte received. |
| 88 base::TimeDelta fastest_RTT_since_last_connection_change_; | 136 base::TimeDelta fastest_RTT_since_last_connection_change_; |
| 89 | 137 |
| 138 // Cache to store quality of previously seen networks. | |
| 139 std::vector<CachedNetworkQuality> cached_network_quality_; | |
|
mmenke
2015/05/28 15:25:03
include <vector>
tbansal1
2015/05/29 02:27:24
Done.
| |
| 140 | |
| 90 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 141 // Rough measurement of downlink peak Kbps witnessed since last connectivity |
| 91 // change. The accuracy is decreased by ignoring these factors: | 142 // change. The accuracy is decreased by ignoring these factors: |
| 92 // 1) Multiple URLRequests can occur concurrently. | 143 // 1) Multiple URLRequests can occur concurrently. |
| 93 // 2) The transfer time includes at least one RTT while no bytes are read. | 144 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 94 uint64_t peak_kbps_since_last_connection_change_; | 145 uint64_t peak_kbps_since_last_connection_change_; |
| 95 | 146 |
| 147 // Name of the current network as returned by GetCurrentNetworkName. | |
| 148 std::string current_network_name_; | |
| 149 | |
| 96 base::ThreadChecker thread_checker_; | 150 base::ThreadChecker thread_checker_; |
| 97 | 151 |
| 98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 152 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 99 }; | 153 }; |
| 100 | 154 |
| 101 } // namespace net | 155 } // namespace net |
| 102 | 156 |
| 103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 157 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |