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 // Maximum number of observations that can be held in the ObservationBuffer. |
| 96 static const size_t kMaximumObservations; |
| 97 |
| 98 // Holds observations sorted by time, with the oldest observation at the |
| 99 // front of the queue. |
| 100 std::deque<Observation> observations_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 103 }; |
| 104 |
52 // Tiny transfer sizes may give inaccurate throughput results. | 105 // Tiny transfer sizes may give inaccurate throughput results. |
53 // Minimum size of the transfer over which the throughput is computed. | 106 // Minimum size of the transfer over which the throughput is computed. |
54 static const int kMinTransferSizeInBytes = 10000; | 107 static const int kMinTransferSizeInBytes = 10000; |
55 | 108 |
56 // Minimum duration (in microseconds) of the transfer over which the | 109 // Minimum duration (in microseconds) of the transfer over which the |
57 // throughput is computed. | 110 // throughput is computed. |
58 static const int kMinRequestDurationMicroseconds = 1000; | 111 static const int kMinRequestDurationMicroseconds = 1000; |
59 | 112 |
60 // Construct a NetworkQualityEstimator instance allowing for test | 113 // Construct a NetworkQualityEstimator instance allowing for test |
61 // configuration. | 114 // configuration. |
(...skipping 12 matching lines...) Expand all Loading... |
74 // network quality. Set to true only for tests. | 127 // network quality. Set to true only for tests. |
75 const bool allow_localhost_requests_; | 128 const bool allow_localhost_requests_; |
76 | 129 |
77 // Time when last connection change was observed. | 130 // Time when last connection change was observed. |
78 base::TimeTicks last_connection_change_; | 131 base::TimeTicks last_connection_change_; |
79 | 132 |
80 // Last value passed to |OnConnectionTypeChanged|. This indicates the | 133 // Last value passed to |OnConnectionTypeChanged|. This indicates the |
81 // current connection type. | 134 // current connection type. |
82 NetworkChangeNotifier::ConnectionType current_connection_type_; | 135 NetworkChangeNotifier::ConnectionType current_connection_type_; |
83 | 136 |
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 | 137 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured |
88 // from URLRequest creation until first byte received. | 138 // from URLRequest creation until first byte received. |
89 base::TimeDelta fastest_RTT_since_last_connection_change_; | 139 base::TimeDelta fastest_rtt_since_last_connection_change_; |
90 | 140 |
91 // Rough measurement of downlink peak Kbps witnessed since last connectivity | 141 // Rough measurement of downstream peak Kbps witnessed since last connectivity |
92 // change. The accuracy is decreased by ignoring these factors: | 142 // change. The accuracy is decreased by ignoring these factors: |
93 // 1) Multiple URLRequests can occur concurrently. | 143 // 1) Multiple URLRequests can occur concurrently. |
94 // 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. |
95 uint64_t peak_kbps_since_last_connection_change_; | 145 int32_t peak_kbps_since_last_connection_change_; |
| 146 |
| 147 // Buffer that holds Kbps observations. |
| 148 ObservationBuffer kbps_observations_; |
| 149 |
| 150 // Buffer that holds RTT (in milliseconds) observations. |
| 151 ObservationBuffer rtt_msec_observations_; |
96 | 152 |
97 base::ThreadChecker thread_checker_; | 153 base::ThreadChecker thread_checker_; |
98 | 154 |
99 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 155 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
100 }; | 156 }; |
101 | 157 |
102 } // namespace net | 158 } // namespace net |
103 | 159 |
104 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 160 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |