| 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> |
| 11 |
| 10 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "net/base/net_export.h" |
| 14 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 15 | 18 |
| 16 namespace net { | 19 namespace net { |
| 17 | 20 |
| 18 struct NetworkQuality; | 21 class NetworkQuality; |
| 19 | 22 |
| 20 // NetworkQualityEstimator provides network quality estimates (quality of the | 23 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 21 // full paths to all origins that have been connected to). | 24 // full paths to all origins that have been connected to). |
| 22 // The estimates are based on the observed organic traffic. | 25 // The estimates are based on the observed organic traffic. |
| 23 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 26 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 24 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 27 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 25 // A single instance of NQE can be attached to multiple URLRequestContexts, | 28 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 26 // thereby increasing the single NQE instance's accuracy by providing more | 29 // thereby increasing the single NQE instance's accuracy by providing more |
| 27 // observed traffic characteristics. | 30 // observed traffic characteristics. |
| 28 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 31 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 29 : public NetworkChangeNotifier::ConnectionTypeObserver { | 32 : public NetworkChangeNotifier::ConnectionTypeObserver { |
| 30 public: | 33 public: |
| 31 // Creates a new NetworkQualityEstimator. | 34 // Creates a new NetworkQualityEstimator. |
| 32 NetworkQualityEstimator(); | 35 NetworkQualityEstimator(); |
| 33 | 36 |
| 34 ~NetworkQualityEstimator() override; | 37 ~NetworkQualityEstimator() override; |
| 35 | 38 |
| 36 // Returns an estimate of the current network quality. | 39 // Returns the peak estimates (fastest RTT and peak throughput) of the |
| 40 // current network. |
| 37 // Virtualized for testing. | 41 // Virtualized for testing. |
| 38 virtual NetworkQuality GetEstimate() const; | 42 virtual NetworkQuality GetPeakEstimate() const; |
| 39 | 43 |
| 40 // Notifies NetworkQualityEstimator that a response has been received. | 44 // Notifies NetworkQualityEstimator that a response has been received. |
| 41 // |prefilter_bytes_read| is the count of the bytes received prior to | 45 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior |
| 42 // applying filters (e.g. decompression, SDCH) from request creation time | 46 // to applying filters (e.g. decompression, SDCH) from request creation time |
| 43 // until now. | 47 // until now. |
| 48 // |prefiltered_bytes_read| is the count of the bytes received prior |
| 49 // to applying filters in the most recent read. |
| 44 void NotifyDataReceived(const URLRequest& request, | 50 void NotifyDataReceived(const URLRequest& request, |
| 45 int64_t prefilter_bytes_read); | 51 int64_t cumulative_prefilter_bytes_read, |
| 52 int64_t prefiltered_bytes_read); |
| 46 | 53 |
| 47 private: | 54 private: |
| 55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 48 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 56 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 49 TestPeakKbpsFastestRTTUpdates); | 57 TestPeakKbpsFastestRTTUpdates); |
| 58 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
| 50 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 59 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 51 | 60 |
| 61 // Records the round trip time or throughput observation, along with the time |
| 62 // the observation was made. |
| 63 struct Observation { |
| 64 Observation(int32_t value, base::TimeTicks timestamp); |
| 65 |
| 66 ~Observation(); |
| 67 |
| 68 // Value of the observation. |
| 69 const int32_t value; |
| 70 |
| 71 // Time when the observation was taken. |
| 72 const base::TimeTicks timestamp; |
| 73 }; |
| 74 |
| 75 // Stores observations sorted by time. |
| 76 class ObservationBuffer { |
| 77 public: |
| 78 ObservationBuffer(); |
| 79 |
| 80 ~ObservationBuffer(); |
| 81 |
| 82 // Adds |observation| to the buffer. The oldest observation in the buffer |
| 83 // will be evicted to make room if the buffer is already full. |
| 84 void AddObservation(const Observation& observation); |
| 85 |
| 86 // Returns the number of observations in this buffer. |
| 87 size_t Size() const; |
| 88 |
| 89 // Clears the observations stored in this buffer. |
| 90 void Clear(); |
| 91 |
| 92 private: |
| 93 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
| 94 |
| 95 // Holds observations sorted by time, with the oldest observation at the |
| 96 // front of the queue. |
| 97 std::deque<Observation> observations_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 100 }; |
| 101 |
| 52 // Tiny transfer sizes may give inaccurate throughput results. | 102 // Tiny transfer sizes may give inaccurate throughput results. |
| 53 // Minimum size of the transfer over which the throughput is computed. | 103 // Minimum size of the transfer over which the throughput is computed. |
| 54 static const int kMinTransferSizeInBytes = 10000; | 104 static const int kMinTransferSizeInBytes = 10000; |
| 55 | 105 |
| 56 // Minimum duration (in microseconds) of the transfer over which the | 106 // Minimum duration (in microseconds) of the transfer over which the |
| 57 // throughput is computed. | 107 // throughput is computed. |
| 58 static const int kMinRequestDurationMicroseconds = 1000; | 108 static const int kMinRequestDurationMicroseconds = 1000; |
| 59 | 109 |
| 60 // Construct a NetworkQualityEstimator instance allowing for test | 110 // Construct a NetworkQualityEstimator instance allowing for test |
| 61 // configuration. | 111 // configuration. |
| 62 // Registers for network type change notifications so estimates can be kept | 112 // Registers for network type change notifications so estimates can be kept |
| 63 // network specific. | 113 // network specific. |
| 64 // |allow_local_host_requests_for_tests| should only be true when testing | 114 // |allow_local_host_requests_for_tests| should only be true when testing |
| 65 // against local HTTP server and allows the requests to local host to be | 115 // against local HTTP server and allows the requests to local host to be |
| 66 // used for network quality estimation. | 116 // used for network quality estimation. |
| 67 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); | 117 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); |
| 68 | 118 |
| 119 // Returns the maximum size of the observation buffer. |
| 120 // Used for testing. |
| 121 size_t GetMaximumObservationBufferSizeForTests() const; |
| 122 |
| 123 // Returns true if the size of all observation buffers is equal to the |
| 124 // |expected_size|. Used for testing. |
| 125 bool VerifyBufferSizeForTests(size_t expected_size) const; |
| 126 |
| 69 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | 127 // NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 70 void OnConnectionTypeChanged( | 128 void OnConnectionTypeChanged( |
| 71 NetworkChangeNotifier::ConnectionType type) override; | 129 NetworkChangeNotifier::ConnectionType type) override; |
| 72 | 130 |
| 73 // Determines if the requests to local host can be used in estimating the | 131 // Determines if the requests to local host can be used in estimating the |
| 74 // network quality. Set to true only for tests. | 132 // network quality. Set to true only for tests. |
| 75 const bool allow_localhost_requests_; | 133 const bool allow_localhost_requests_; |
| 76 | 134 |
| 77 // Time when last connection change was observed. | 135 // Time when last connection change was observed. |
| 78 base::TimeTicks last_connection_change_; | 136 base::TimeTicks last_connection_change_; |
| 79 | 137 |
| 80 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 138 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| 81 // current connection type. | 139 // current connection type. |
| 82 NetworkChangeNotifier::ConnectionType current_connection_type_; | 140 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 83 | 141 |
| 84 // Set if any network data has been received since last connectivity change. | |
| 85 bool bytes_read_since_last_connection_change_; | |
| 86 | |
| 87 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 142 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 88 // from URLRequest creation until first byte received. | 143 // from URLRequest creation until first byte received. |
| 89 base::TimeDelta fastest_RTT_since_last_connection_change_; | 144 base::TimeDelta fastest_rtt_since_last_connection_change_; |
| 90 | 145 |
| 91 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 146 // Rough measurement of downstream peak Kbps witnessed since last connectivity |
| 92 // change. The accuracy is decreased by ignoring these factors: | 147 // change. The accuracy is decreased by ignoring these factors: |
| 93 // 1) Multiple URLRequests can occur concurrently. | 148 // 1) Multiple URLRequests can occur concurrently. |
| 94 // 2) The transfer time includes at least one RTT while no bytes are read. | 149 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 95 uint64_t peak_kbps_since_last_connection_change_; | 150 int32_t peak_kbps_since_last_connection_change_; |
| 151 |
| 152 // Buffer that holds Kbps observations. |
| 153 ObservationBuffer kbps_observations_; |
| 154 |
| 155 // Buffer that holds RTT (in milliseconds) observations. |
| 156 ObservationBuffer rtt_msec_observations_; |
| 96 | 157 |
| 97 base::ThreadChecker thread_checker_; | 158 base::ThreadChecker thread_checker_; |
| 98 | 159 |
| 99 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 160 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 100 }; | 161 }; |
| 101 | 162 |
| 102 } // namespace net | 163 } // namespace net |
| 103 | 164 |
| 104 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 165 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |