Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: net/base/network_quality_estimator.h

Issue 1144163008: Add in-memory caching of network quality estimates across network changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Paul's comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 10 #include <deque>
11 #include <map>
12 #include <string>
11 13
12 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
13 #include "base/macros.h" 15 #include "base/macros.h"
14 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h" 17 #include "base/time/time.h"
16 #include "net/base/net_export.h" 18 #include "net/base/net_export.h"
17 #include "net/base/network_change_notifier.h" 19 #include "net/base/network_change_notifier.h"
20 #include "net/base/network_quality.h"
18 21
19 namespace net { 22 namespace net {
20 23
21 class NetworkQuality;
22
23 // NetworkQualityEstimator provides network quality estimates (quality of the 24 // NetworkQualityEstimator provides network quality estimates (quality of the
24 // full paths to all origins that have been connected to). 25 // full paths to all origins that have been connected to).
25 // The estimates are based on the observed organic traffic. 26 // The estimates are based on the observed organic traffic.
26 // A NetworkQualityEstimator instance is attached to URLRequestContexts and 27 // A NetworkQualityEstimator instance is attached to URLRequestContexts and
27 // observes the traffic of URLRequests spawned from the URLRequestContexts. 28 // observes the traffic of URLRequests spawned from the URLRequestContexts.
28 // A single instance of NQE can be attached to multiple URLRequestContexts, 29 // A single instance of NQE can be attached to multiple URLRequestContexts,
29 // thereby increasing the single NQE instance's accuracy by providing more 30 // thereby increasing the single NQE instance's accuracy by providing more
30 // observed traffic characteristics. 31 // observed traffic characteristics.
31 class NET_EXPORT_PRIVATE NetworkQualityEstimator 32 class NET_EXPORT_PRIVATE NetworkQualityEstimator
32 : public NetworkChangeNotifier::ConnectionTypeObserver { 33 : public NetworkChangeNotifier::ConnectionTypeObserver {
(...skipping 11 matching lines...) Expand all
44 // Notifies NetworkQualityEstimator that a response has been received. 45 // Notifies NetworkQualityEstimator that a response has been received.
45 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior 46 // |cumulative_prefilter_bytes_read| is the count of the bytes received prior
46 // to applying filters (e.g. decompression, SDCH) from request creation time 47 // to applying filters (e.g. decompression, SDCH) from request creation time
47 // until now. 48 // until now.
48 // |prefiltered_bytes_read| is the count of the bytes received prior 49 // |prefiltered_bytes_read| is the count of the bytes received prior
49 // to applying filters in the most recent read. 50 // to applying filters in the most recent read.
50 void NotifyDataReceived(const URLRequest& request, 51 void NotifyDataReceived(const URLRequest& request,
51 int64_t cumulative_prefilter_bytes_read, 52 int64_t cumulative_prefilter_bytes_read,
52 int64_t prefiltered_bytes_read); 53 int64_t prefiltered_bytes_read);
53 54
55 protected:
56 // NetworkID is used to uniquely identify a network.
57 // For the purpose of network quality estimation and caching, a network is
58 // uniquely identified by a combination of |type| and
59 // |id|. This approach is unable to distinguish networks with
60 // same name (e.g., different Wi-Fi networks with same SSID).
61 // This is a protected member to expose it to tests.
62 struct NetworkID {
63 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
64 : type(type), id(id) {}
65
66 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {}
67
68 ~NetworkID() {}
69
70 NetworkID& operator=(const NetworkID& other) {
71 type = other.type;
72 id = other.id;
73 return *this;
74 }
75
76 // Overloaded because NetworkID is used as key in a map.
77 bool operator<(const NetworkID& other) const {
78 return type < other.type || (type == other.type && id < other.id);
79 }
80
81 // Connection type of the network.
82 NetworkChangeNotifier::ConnectionType type;
83
84 // Name of this network. This is set to:
85 // - Wi-Fi SSID (if the user is connected to a Wi-Fi access point and the
86 // SSID name is available), or
87 // - MCC/MNC code of the cellular carrier if the device is connected to a
88 // cellular network, or
89 // - An empty string in all other cases or if the network name is not
90 // exposed by platform APIs.
91 std::string id;
92 };
93
94 // Construct a NetworkQualityEstimator instance allowing for test
95 // configuration.
96 // Registers for network type change notifications so estimates can be kept
97 // network specific.
98 // |allow_local_host_requests_for_tests| should only be true when testing
99 // against local HTTP server and allows the requests to local host to be
100 // used for network quality estimation.
101 // |allow_smaller_responses_for_tests| should only be true when testing
102 // against local HTTP server and allows the responses smaller than
103 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds|
104 // to be used for network quality estimation.
105 NetworkQualityEstimator(bool allow_local_host_requests_for_tests,
106 bool allow_smaller_responses_for_tests);
107
108 // Returns true if the cached network quality estimate was successfully read.
109 bool ReadCachedNetworkQualityEstimate();
110
111 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
112 // |type| is ignored.
113 void OnConnectionTypeChanged(
114 NetworkChangeNotifier::ConnectionType type) override;
115
116 // Returns the number of entries in the network quality cache.
117 // Used only for testing.
118 size_t GetNetworkQualityCacheSizeForTests() const;
119
54 private: 120 private:
55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); 121 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
56 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, 122 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
57 TestPeakKbpsFastestRTTUpdates); 123 TestPeakKbpsFastestRTTUpdates);
58 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); 124 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
125 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
126 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
127 TestLRUCacheMaximumSize);
59 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); 128 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
60 129
130 // CachedNetworkQuality stores the quality of a previously seen network.
131 class CachedNetworkQuality {
132 public:
133 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
134
135 ~CachedNetworkQuality();
136
137 // Returns the network quality associated with this cached entry.
138 const NetworkQuality network_quality() const { return network_quality_; }
139
140 // Updates the network quality to the specified |median_kbps| and
141 // |median_rtt|.
142 void UpdateNetworkQuality(int32_t median_kbps,
143 const base::TimeDelta& median_rtt);
144
145 // Returns true if this cache entry was updated before
146 // |cached_network_quality|.
147 bool OlderThan(const CachedNetworkQuality& cached_network_quality) const;
148
149 private:
150 // Time when this cache entry was last updated.
151 base::TimeTicks last_update_time_;
152
153 // Quality of this cached network.
154 NetworkQuality network_quality_;
155
156 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
157 };
158
61 // Records the round trip time or throughput observation, along with the time 159 // Records the round trip time or throughput observation, along with the time
62 // the observation was made. 160 // the observation was made.
63 struct Observation { 161 struct Observation {
64 Observation(int32_t value, base::TimeTicks timestamp); 162 Observation(int32_t value, base::TimeTicks timestamp);
65 163
66 ~Observation(); 164 ~Observation();
67 165
68 // Value of the observation. 166 // Value of the observation.
69 const int32_t value; 167 const int32_t value;
70 168
(...skipping 21 matching lines...) Expand all
92 private: 190 private:
93 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); 191 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
94 192
95 // Holds observations sorted by time, with the oldest observation at the 193 // Holds observations sorted by time, with the oldest observation at the
96 // front of the queue. 194 // front of the queue.
97 std::deque<Observation> observations_; 195 std::deque<Observation> observations_;
98 196
99 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); 197 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
100 }; 198 };
101 199
200 // This does not use a unordered_map or hash_map for code simplicity (key just
201 // implements operator<, rather than hash and equality) and because the map is
202 // tiny.
203 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>>
204 CachedNetworkQualities;
205
102 // Tiny transfer sizes may give inaccurate throughput results. 206 // Tiny transfer sizes may give inaccurate throughput results.
103 // Minimum size of the transfer over which the throughput is computed. 207 // Minimum size of the transfer over which the throughput is computed.
104 static const int kMinTransferSizeInBytes = 10000; 208 static const int kMinTransferSizeInBytes = 10000;
105 209
106 // Minimum duration (in microseconds) of the transfer over which the 210 // Minimum duration (in microseconds) of the transfer over which the
107 // throughput is computed. 211 // throughput is computed.
108 static const int kMinRequestDurationMicroseconds = 1000; 212 static const int kMinRequestDurationMicroseconds = 1000;
109 213
110 // Construct a NetworkQualityEstimator instance allowing for test 214 // Maximum size of the cache that holds network quality estimates.
111 // configuration. 215 // Smaller size may reduce the cache hit rate due to frequent evictions.
112 // Registers for network type change notifications so estimates can be kept 216 // Larger size may affect performance.
113 // network specific. 217 static const size_t kMaximumNetworkQualityCacheSize;
114 // |allow_local_host_requests_for_tests| should only be true when testing
115 // against local HTTP server and allows the requests to local host to be
116 // used for network quality estimation.
117 // |allow_smaller_responses_for_tests| should only be true when testing
118 // against local HTTP server and allows the responses smaller than
119 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds|
120 // to be used for network quality estimation.
121 NetworkQualityEstimator(bool allow_local_host_requests_for_tests,
122 bool allow_smaller_responses_for_tests);
123 218
124 // Returns the maximum size of the observation buffer. 219 // Returns the maximum size of the observation buffers. Used for testing.
125 // Used for testing.
126 size_t GetMaximumObservationBufferSizeForTests() const; 220 size_t GetMaximumObservationBufferSizeForTests() const;
127 221
128 // Returns true if the size of all observation buffers is equal to the 222 // Returns the current size of the Kbps observation buffer. Used for testing.
129 // |expected_size|. Used for testing. 223 size_t GetKbpsObservationBufferSizeForTests() const;
130 bool VerifyBufferSizeForTests(size_t expected_size) const;
131 224
132 // NetworkChangeNotifier::ConnectionTypeObserver implementation. 225 // Returns the current size of the RTT observation buffer. Used for testing.
133 void OnConnectionTypeChanged( 226 size_t GetRTTObservationBufferSizeForTests() const;
134 NetworkChangeNotifier::ConnectionType type) override; 227
228 // Returns the current network ID checking by calling the platform APIs.
229 // Virtualized for testing.
230 virtual NetworkID GetCurrentNetworkID() const;
231
232 // Writes the estimated quality of the current network to the cache.
233 void CacheNetworkQualityEstimate();
135 234
136 // Determines if the requests to local host can be used in estimating the 235 // Determines if the requests to local host can be used in estimating the
137 // network quality. Set to true only for tests. 236 // network quality. Set to true only for tests.
138 const bool allow_localhost_requests_; 237 const bool allow_localhost_requests_;
139 238
140 // Determines if the responses smaller than |kMinTransferSizeInBytes| 239 // Determines if the responses smaller than |kMinTransferSizeInBytes|
141 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the 240 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the
142 // network quality. Set to true only for tests. 241 // network quality. Set to true only for tests.
143 const bool allow_small_responses_; 242 const bool allow_small_responses_;
144 243
145 // Time when last connection change was observed. 244 // Time when last connection change was observed.
146 base::TimeTicks last_connection_change_; 245 base::TimeTicks last_connection_change_;
147 246
148 // Last value passed to |OnConnectionTypeChanged|. This indicates the 247 // ID of the current network.
149 // current connection type. 248 NetworkID current_network_id_;
150 NetworkChangeNotifier::ConnectionType current_connection_type_;
151 249
152 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured 250 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured
153 // from URLRequest creation until first byte received. 251 // from URLRequest creation until first byte received.
154 base::TimeDelta fastest_rtt_since_last_connection_change_; 252 base::TimeDelta fastest_rtt_since_last_connection_change_;
155 253
254 // Cache that stores quality of previously seen networks.
255 CachedNetworkQualities cached_network_qualities_;
256
156 // Rough measurement of downstream peak Kbps witnessed since last connectivity 257 // Rough measurement of downstream peak Kbps witnessed since last connectivity
157 // change. The accuracy is decreased by ignoring these factors: 258 // change. The accuracy is decreased by ignoring these factors:
158 // 1) Multiple URLRequests can occur concurrently. 259 // 1) Multiple URLRequests can occur concurrently.
159 // 2) The transfer time includes at least one RTT while no bytes are read. 260 // 2) The transfer time includes at least one RTT while no bytes are read.
160 int32_t peak_kbps_since_last_connection_change_; 261 int32_t peak_kbps_since_last_connection_change_;
161 262
162 // Buffer that holds Kbps observations. 263 // Buffer that holds Kbps observations.
163 ObservationBuffer kbps_observations_; 264 ObservationBuffer kbps_observations_;
164 265
165 // Buffer that holds RTT (in milliseconds) observations. 266 // Buffer that holds RTT (in milliseconds) observations.
166 ObservationBuffer rtt_msec_observations_; 267 ObservationBuffer rtt_msec_observations_;
167 268
168 base::ThreadChecker thread_checker_; 269 base::ThreadChecker thread_checker_;
169 270
170 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); 271 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
171 }; 272 };
172 273
173 } // namespace net 274 } // namespace net
174 275
175 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ 276 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698