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

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: rebased again 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 // Returns true if the cached network quality estimate was successfully read.
57 bool ReadCachedNetworkQualityEstimate();
58
59 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
60 void OnConnectionTypeChanged(
61 NetworkChangeNotifier::ConnectionType type) override;
62
63 // Returns the number of entries in the network quality cache.
64 // Used only for testing.
65 size_t GetNetworkQualityCacheSizeForTests() const;
66
54 private: 67 private:
55 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); 68 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
56 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, 69 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
57 TestPeakKbpsFastestRTTUpdates); 70 TestPeakKbpsFastestRTTUpdates);
58 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); 71 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
72 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
73 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
74 TestLRUCacheMaximumSize);
59 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); 75 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
60 76
77 // NetworkID is used to uniquely identify a network.
78 // For the purpose of network quality estimation and caching, a network is
79 // uniquely identified by a combination of |type| and
80 // |id|. This approach is unable to distinguish networks with
81 // same name (e.g., different Wi-Fi networks with same SSID).
82 struct NetworkID {
83 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
84 : type(type), id(id) {}
85
86 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {}
87
88 ~NetworkID() {}
89
90 NetworkID& operator=(const NetworkID& other) {
91 type = other.type;
92 id = other.id;
93 return *this;
94 }
95
96 // Overloaded because NetworkID is used as key in a map.
97 bool operator<(const NetworkID& other) const {
98 return type < other.type || (type == other.type && id < other.id);
99 }
100
101 // Connection type of the network.
102 NetworkChangeNotifier::ConnectionType type;
103
104 // Name of this network as returned by |GetCurrentNetworkName| function.
105 std::string id;
106 };
107
108 // CachedNetworkQuality stores the quality of a previously seen network.
109 class CachedNetworkQuality {
110 public:
111 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
112
113 ~CachedNetworkQuality();
114
115 // Returns the network quality associated with this cached entry.
116 const NetworkQuality network_quality() const { return network_quality_; }
117
118 // Updates the network quality to the specified |median_kbps| and
119 // |median_rtt|.
120 void UpdateNetworkQuality(int32_t median_kbps,
121 const base::TimeDelta& median_rtt);
122
123 // Returns the time when this cached entry was last updated.
124 base::TimeTicks last_update_time() const { return last_update_time_; }
pauljensen 2015/06/17 14:23:26 Slightly nicer encapsulation if we replaced this f
tbansal1 2015/06/17 21:18:46 Done.
125
126 private:
127 // Time when this cache entry was last updated.
128 base::TimeTicks last_update_time_;
129
130 // Quality of this cached network.
131 NetworkQuality network_quality_;
132
133 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
134 };
135
61 // Records the round trip time or throughput observation, along with the time 136 // Records the round trip time or throughput observation, along with the time
62 // the observation was made. 137 // the observation was made.
63 struct Observation { 138 struct Observation {
64 Observation(int32_t value, base::TimeTicks timestamp); 139 Observation(int32_t value, base::TimeTicks timestamp);
65 140
66 ~Observation(); 141 ~Observation();
67 142
68 // Value of the observation. 143 // Value of the observation.
69 const int32_t value; 144 const int32_t value;
70 145
(...skipping 21 matching lines...) Expand all
92 private: 167 private:
93 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); 168 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
94 169
95 // Holds observations sorted by time, with the oldest observation at the 170 // Holds observations sorted by time, with the oldest observation at the
96 // front of the queue. 171 // front of the queue.
97 std::deque<Observation> observations_; 172 std::deque<Observation> observations_;
98 173
99 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); 174 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
100 }; 175 };
101 176
177 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>>
pauljensen 2015/06/17 14:19:15 I don't see an ordered traversal and assume we'll
tbansal1 2015/06/17 21:18:46 Done.
178 CachedNetworkQualities;
179
102 // Tiny transfer sizes may give inaccurate throughput results. 180 // Tiny transfer sizes may give inaccurate throughput results.
103 // Minimum size of the transfer over which the throughput is computed. 181 // Minimum size of the transfer over which the throughput is computed.
104 static const int kMinTransferSizeInBytes = 10000; 182 static const int kMinTransferSizeInBytes = 10000;
105 183
106 // Minimum duration (in microseconds) of the transfer over which the 184 // Minimum duration (in microseconds) of the transfer over which the
107 // throughput is computed. 185 // throughput is computed.
108 static const int kMinRequestDurationMicroseconds = 1000; 186 static const int kMinRequestDurationMicroseconds = 1000;
109 187
188 // Maximum size of the cache that holds network quality estimates.
189 // Smaller size may reduce the cache hit rate due to frequent evictions.
190 // Larger size may affect performance.
191 static const size_t kMaximumNetworkQualityCacheSize;
192
110 // Construct a NetworkQualityEstimator instance allowing for test 193 // Construct a NetworkQualityEstimator instance allowing for test
111 // configuration. 194 // configuration.
112 // Registers for network type change notifications so estimates can be kept 195 // Registers for network type change notifications so estimates can be kept
113 // network specific. 196 // network specific.
114 // |allow_local_host_requests_for_tests| should only be true when testing 197 // |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 198 // against local HTTP server and allows the requests to local host to be
116 // used for network quality estimation. 199 // used for network quality estimation.
117 // |allow_smaller_responses_for_tests| should only be true when testing 200 // |allow_smaller_responses_for_tests| should only be true when testing
118 // against local HTTP server and allows the responses smaller than 201 // against local HTTP server and allows the responses smaller than
119 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds| 202 // |kMinTransferSizeInBytes| or shorter than |kMinRequestDurationMicroseconds|
120 // to be used for network quality estimation. 203 // to be used for network quality estimation.
121 NetworkQualityEstimator(bool allow_local_host_requests_for_tests, 204 NetworkQualityEstimator(bool allow_local_host_requests_for_tests,
122 bool allow_smaller_responses_for_tests); 205 bool allow_smaller_responses_for_tests);
123 206
124 // Returns the maximum size of the observation buffer. 207 // Returns the maximum size of the observation buffer.
125 // Used for testing. 208 // Used for testing.
126 size_t GetMaximumObservationBufferSizeForTests() const; 209 size_t GetMaximumObservationBufferSizeForTests() const;
pauljensen 2015/06/17 14:19:15 Can we remove this and have the tests compare to t
tbansal1 2015/06/17 21:18:46 Right now kMaximumObservationsBufferSize is define
pauljensen 2015/06/19 03:10:07 I don't understand. Why can't we replace this fun
tbansal1 2015/06/19 23:34:40 For some reason, this gives the linking error both
pauljensen 2015/06/20 01:30:45 What link error are you seeing? There are plenty
tbansal1 2015/06/20 03:09:30 Forgot to mention that the linking error comes onl
tbansal1 2015/06/20 03:13:56 My regex was incorrect. It does not capture variab
tbansal1 2015/06/20 03:21:17 It seems like existing code also does typecast bef
tbansal1 2015/06/22 17:51:22 Done.
127 210
128 // Returns true if the size of all observation buffers is equal to the 211 // Returns true if the size of all observation buffers is equal to the
129 // |expected_size|. Used for testing. 212 // |expected_size|. Used for testing.
130 bool VerifyBufferSizeForTests(size_t expected_size) const; 213 bool VerifyBufferSizeForTests(size_t expected_size) const;
pauljensen 2015/06/17 14:19:15 Can we remove this and have the tests compare to t
tbansal1 2015/06/17 21:18:46 Removed this function but had to add 2 more functi
131 214
132 // NetworkChangeNotifier::ConnectionTypeObserver implementation. 215 // Updates the current network name to:
133 void OnConnectionTypeChanged( 216 // WiFi SSID (if the user is connected to a WiFi access point and the SSID
pauljensen 2015/06/17 14:19:15 can you add " - " before "WiFi" on this line and b
tbansal1 2015/06/17 21:18:46 Done.
134 NetworkChangeNotifier::ConnectionType type) override; 217 // name is available), or
218 // the MCC/MNC code of the cellular carrier if the device is connected to a
219 // cellular network.
220 // Updates the current network name to an empty string in all other cases or
221 // if the network name is not exposed by platform APIs.
222 // Virtualized for testing.
223 virtual std::string GetCurrentNetworkName() const;
224
225 // Writes the estimated quality of the current network to the cache.
226 void CacheNetworkQualityEstimate();
135 227
136 // Determines if the requests to local host can be used in estimating the 228 // Determines if the requests to local host can be used in estimating the
137 // network quality. Set to true only for tests. 229 // network quality. Set to true only for tests.
138 const bool allow_localhost_requests_; 230 const bool allow_localhost_requests_;
139 231
140 // Determines if the responses smaller than |kMinTransferSizeInBytes| 232 // Determines if the responses smaller than |kMinTransferSizeInBytes|
141 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the 233 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the
142 // network quality. Set to true only for tests. 234 // network quality. Set to true only for tests.
143 const bool allow_small_responses_; 235 const bool allow_small_responses_;
144 236
145 // Time when last connection change was observed. 237 // Time when last connection change was observed.
146 base::TimeTicks last_connection_change_; 238 base::TimeTicks last_connection_change_;
147 239
148 // Last value passed to |OnConnectionTypeChanged|. This indicates the 240 // ID of the current network.
149 // current connection type. 241 NetworkID current_network_id_;
150 NetworkChangeNotifier::ConnectionType current_connection_type_;
151 242
152 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured 243 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured
153 // from URLRequest creation until first byte received. 244 // from URLRequest creation until first byte received.
154 base::TimeDelta fastest_rtt_since_last_connection_change_; 245 base::TimeDelta fastest_rtt_since_last_connection_change_;
155 246
247 // Cache that stores quality of previously seen networks.
248 CachedNetworkQualities cached_network_qualities_;
249
156 // Rough measurement of downstream peak Kbps witnessed since last connectivity 250 // Rough measurement of downstream peak Kbps witnessed since last connectivity
157 // change. The accuracy is decreased by ignoring these factors: 251 // change. The accuracy is decreased by ignoring these factors:
158 // 1) Multiple URLRequests can occur concurrently. 252 // 1) Multiple URLRequests can occur concurrently.
159 // 2) The transfer time includes at least one RTT while no bytes are read. 253 // 2) The transfer time includes at least one RTT while no bytes are read.
160 int32_t peak_kbps_since_last_connection_change_; 254 int32_t peak_kbps_since_last_connection_change_;
161 255
162 // Buffer that holds Kbps observations. 256 // Buffer that holds Kbps observations.
163 ObservationBuffer kbps_observations_; 257 ObservationBuffer kbps_observations_;
164 258
165 // Buffer that holds RTT (in milliseconds) observations. 259 // Buffer that holds RTT (in milliseconds) observations.
166 ObservationBuffer rtt_msec_observations_; 260 ObservationBuffer rtt_msec_observations_;
167 261
168 base::ThreadChecker thread_checker_; 262 base::ThreadChecker thread_checker_;
169 263
170 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); 264 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
171 }; 265 };
172 266
173 } // namespace net 267 } // namespace net
174 268
175 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ 269 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698