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> | |
| 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 |
| 37 NetworkQuality GetEstimate() const; | 40 // current network. |
| 41 NetworkQuality GetPeakEstimate() const; | |
| 38 | 42 |
| 39 // Notifies NetworkQualityEstimator that a response has been received. | 43 // Notifies NetworkQualityEstimator that a response has been received. |
| 40 // |prefilter_bytes_read| is the count of the bytes received prior to | 44 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior |
| 41 // applying filters (e.g. decompression, SDCH) from request creation time | 45 // to applying filters (e.g. decompression, SDCH) from request creation time |
| 42 // until now. | 46 // until now. |
| 47 // |prefiltered_bytes_read| is the count of the bytes received prior | |
| 48 // to applying filters in the most recent read. | |
| 43 void NotifyDataReceived(const URLRequest& request, | 49 void NotifyDataReceived(const URLRequest& request, |
| 44 int64_t prefilter_bytes_read); | 50 int64_t cumulative_prefilter_bytes_read, |
| 51 int64_t prefiltered_bytes_read); | |
| 45 | 52 |
| 46 private: | 53 private: |
| 54 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | |
| 47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 48 TestPeakKbpsFastestRTTUpdates); | 56 TestPeakKbpsFastestRTTUpdates); |
| 57 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); | |
| 49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); | 58 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
| 50 | 59 |
| 60 // Records the round trip time or throughput observation, along with the time | |
| 61 // the observation was made. | |
| 62 struct Observation { | |
| 63 Observation(int32_t value, base::TimeTicks timestamp); | |
| 64 | |
| 65 ~Observation(); | |
| 66 | |
| 67 // Value of the observation. | |
| 68 const int32_t value; | |
| 69 | |
| 70 // Time when the observation was taken. | |
| 71 const base::TimeTicks timestamp; | |
| 72 }; | |
| 73 | |
| 74 // Stores observations sorted by time. | |
| 75 class ObservationBuffer { | |
| 76 public: | |
| 77 ObservationBuffer(); | |
| 78 | |
| 79 ~ObservationBuffer(); | |
| 80 | |
| 81 // Adds an observation to the buffer. The oldest observation in the buffer | |
| 82 // will be evicted to make room if the buffer is already full. | |
| 83 void AddObservation(int32_t value); | |
| 84 | |
| 85 // Returns the number of observations in this buffer. | |
| 86 size_t size() const; | |
| 87 | |
| 88 private: | |
| 89 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); | |
| 90 | |
| 91 // Maximum number of observations that can be held in the ObservationBuffer. | |
| 92 static const size_t kMaximumObservations; | |
| 93 | |
| 94 // Holds observations sorted by time, with the oldest observation at the | |
| 95 // front of the queue. | |
| 96 std::deque<Observation> observations_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | |
| 99 }; | |
| 100 | |
| 51 // Tiny transfer sizes may give inaccurate throughput results. | 101 // Tiny transfer sizes may give inaccurate throughput results. |
| 52 // Minimum size of the transfer over which the throughput is computed. | 102 // Minimum size of the transfer over which the throughput is computed. |
| 53 static const int kMinTransferSizeInBytes = 10000; | 103 static const int kMinTransferSizeInBytes = 10000; |
| 54 | 104 |
| 55 // Minimum duration (in microseconds) of the transfer over which the | 105 // Minimum duration (in microseconds) of the transfer over which the |
| 56 // throughput is computed. | 106 // throughput is computed. |
| 57 static const int kMinRequestDurationMicroseconds = 1000; | 107 static const int kMinRequestDurationMicroseconds = 1000; |
| 58 | 108 |
| 59 // Construct a NetworkQualityEstimator instance allowing for test | 109 // Construct a NetworkQualityEstimator instance allowing for test |
| 60 // configuration. | 110 // configuration. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 74 const bool allow_localhost_requests_; | 124 const bool allow_localhost_requests_; |
| 75 | 125 |
| 76 // Time when last connection change was observed. | 126 // Time when last connection change was observed. |
| 77 base::TimeTicks last_connection_change_; | 127 base::TimeTicks last_connection_change_; |
| 78 | 128 |
| 79 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 129 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
| 80 // current connection type. | 130 // current connection type. |
| 81 NetworkChangeNotifier::ConnectionType current_connection_type_; | 131 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 82 | 132 |
| 83 // Set if any network data has been received since last connectivity change. | 133 // Set if any network data has been received since last connectivity change. |
| 84 bool bytes_read_since_last_connection_change_; | 134 bool have_received_bytes_since_last_connection_change_; |
| 85 | 135 |
| 86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured | 136 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
| 87 // from URLRequest creation until first byte received. | 137 // from URLRequest creation until first byte received. |
| 88 base::TimeDelta fastest_RTT_since_last_connection_change_; | 138 base::TimeDelta fastest_rtt_since_last_connection_change_; |
| 89 | 139 |
| 90 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 140 // Rough measurement of downstream peak Kbps witnessed since last connectivity |
| 91 // change. The accuracy is decreased by ignoring these factors: | 141 // change. The accuracy is decreased by ignoring these factors: |
| 92 // 1) Multiple URLRequests can occur concurrently. | 142 // 1) Multiple URLRequests can occur concurrently. |
| 93 // 2) The transfer time includes at least one RTT while no bytes are read. | 143 // 2) The transfer time includes at least one RTT while no bytes are read. |
| 94 uint64_t peak_kbps_since_last_connection_change_; | 144 int32_t peak_kbps_since_last_connection_change_; |
|
mmenke
2015/06/11 15:11:01
Should we really provide kilobits per second? See
tbansal1
2015/06/11 19:27:50
It seems that (kilo|mega) bits per second is used
| |
| 145 | |
| 146 // Buffer that holds Kbps observations. | |
| 147 ObservationBuffer kbps_observations_; | |
| 148 | |
| 149 // Buffer that holds RTT (in milliseconds) observations. | |
| 150 ObservationBuffer rtt_msec_observations_; | |
| 95 | 151 |
| 96 base::ThreadChecker thread_checker_; | 152 base::ThreadChecker thread_checker_; |
| 97 | 153 |
| 98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 154 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 99 }; | 155 }; |
| 100 | 156 |
| 101 } // namespace net | 157 } // namespace net |
| 102 | 158 |
| 103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 159 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |