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

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 comments, added more tests, cleaned up tests 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 <map>
11 #include <string>
12
10 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
11 #include "base/macros.h" 14 #include "base/macros.h"
12 #include "base/threading/thread_checker.h" 15 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h" 16 #include "base/time/time.h"
14 #include "net/base/network_change_notifier.h" 17 #include "net/base/network_change_notifier.h"
18 #include "net/base/network_quality.h"
15 19
16 namespace net { 20 namespace net {
17 21
18 struct NetworkQuality;
19
20 // NetworkQualityEstimator provides network quality estimates (quality of the 22 // NetworkQualityEstimator provides network quality estimates (quality of the
21 // full paths to all origins that have been connected to). 23 // full paths to all origins that have been connected to).
22 // The estimates are based on the observed organic traffic. 24 // The estimates are based on the observed organic traffic.
23 // A NetworkQualityEstimator instance is attached to URLRequestContexts and 25 // A NetworkQualityEstimator instance is attached to URLRequestContexts and
24 // observes the traffic of URLRequests spawned from the URLRequestContexts. 26 // observes the traffic of URLRequests spawned from the URLRequestContexts.
25 // A single instance of NQE can be attached to multiple URLRequestContexts, 27 // A single instance of NQE can be attached to multiple URLRequestContexts,
26 // thereby increasing the single NQE instance's accuracy by providing more 28 // thereby increasing the single NQE instance's accuracy by providing more
27 // observed traffic characteristics. 29 // observed traffic characteristics.
28 class NET_EXPORT_PRIVATE NetworkQualityEstimator 30 class NET_EXPORT_PRIVATE NetworkQualityEstimator
29 : public NetworkChangeNotifier::ConnectionTypeObserver { 31 : public NetworkChangeNotifier::ConnectionTypeObserver {
30 public: 32 public:
31 // Creates a new NetworkQualityEstimator. 33 // Creates a new NetworkQualityEstimator.
32 NetworkQualityEstimator(); 34 NetworkQualityEstimator();
33 35
34 ~NetworkQualityEstimator() override; 36 ~NetworkQualityEstimator() override;
35 37
36 // Returns an estimate of the current network quality. 38 // Returns an estimate of the current network quality.
37 NetworkQuality GetEstimate() const; 39 NetworkQuality GetEstimate() const;
38 40
39 // Notifies NetworkQualityEstimator that a response has been received. 41 // Notifies NetworkQualityEstimator that a response has been received.
40 // |prefilter_bytes_read| is the count of the bytes received prior to 42 // |prefilter_bytes_read| is the count of the bytes received prior to
41 // applying filters (e.g. decompression, SDCH) from request creation time 43 // applying filters (e.g. decompression, SDCH) from request creation time
42 // until now. 44 // until now.
43 void NotifyDataReceived(const URLRequest& request, 45 void NotifyDataReceived(const URLRequest& request,
44 int64_t prefilter_bytes_read); 46 int64_t prefilter_bytes_read);
45 47
48 protected:
49 // Returns true if the cached network quality estimate was successfully read.
50 bool ReadCachedNetworkQualityEstimate();
51
52 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
53 void OnConnectionTypeChanged(
54 NetworkChangeNotifier::ConnectionType type) override;
55
56 // Returns the number of entries in the cache. Used only for testing.
57 size_t GetCacheSizeForTests() const;
58
46 private: 59 private:
47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, 60 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
48 TestPeakKbpsFastestRTTUpdates); 61 TestPeakKbpsFastestRTTUpdates);
62 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
63 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
64 TestLRUCacheMaximumSize);
49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); 65 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
50 66
67 // NetworkID is used to uniquely identify a network.
68 // For the purpose of network quality estimation and caching, a network is
69 // uniquely identified by a combination of |connection_type_| and
70 // |network_name_|. This approach is unable to distinguish networks with
71 // same name (e.g., different Wi-Fi networks with same SSID).
bengr 2015/06/11 00:02:28 same name -> the same name Also, could you use BS
tbansal1 2015/06/11 02:20:23 I believe BSSID is currently exposed only on chrom
bengr 2015/06/13 00:28:54 Acknowledged.
72 struct NetworkID {
73 NetworkChangeNotifier::ConnectionType type;
74 std::string id;
75
76 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
77 : type(type), id(id) {}
78
79 ~NetworkID() {}
80
81 NetworkID& operator=(const NetworkID& other) {
82 type = other.type;
83 id = other.id;
84 return *this;
85 }
86
87 // Overloaded because NetworkID is used as key in a map.
88 bool operator<(const NetworkID& other) const {
89 return type < other.type || (type == other.type && id < other.id);
90 }
91 };
92
93 // CachedNetworkQuality stores the quality of a previously seen network.
94 class CachedNetworkQuality {
95 public:
96 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
97
98 ~CachedNetworkQuality();
99
100 // Returns the network quality associated with this cached entry.
101 const NetworkQuality GetNetworkQuality() const;
102
103 // Updates the network quality to the specified |median_kbps| and
104 // |median_rtt|.
105 void UpdateNetworkQuality(uint64_t median_kbps,
106 const base::TimeDelta& median_rtt);
107
108 // Returns the time when this cached entry was last updated.
109 base::TimeTicks last_update_time() const { return last_update_time_; }
110
111 private:
112 // Time when this cache entry was last updated.
113 base::TimeTicks last_update_time_;
114
115 // Quality of this cached network.
116 NetworkQuality network_quality_;
117
118 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
119 };
120
121 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> CachedQualities;
bengr 2015/06/11 00:02:28 Suggest renaming to CachedNetworkQualities.
tbansal1 2015/06/11 02:20:23 Done.
122
51 // Tiny transfer sizes may give inaccurate throughput results. 123 // Tiny transfer sizes may give inaccurate throughput results.
52 // Minimum size of the transfer over which the throughput is computed. 124 // Minimum size of the transfer over which the throughput is computed.
53 static const int kMinTransferSizeInBytes = 10000; 125 static const int kMinTransferSizeInBytes = 10000;
54 126
55 // Minimum duration (in microseconds) of the transfer over which the 127 // Minimum duration (in microseconds) of the transfer over which the
56 // throughput is computed. 128 // throughput is computed.
57 static const int kMinRequestDurationMicroseconds = 1000; 129 static const int kMinRequestDurationMicroseconds = 1000;
58 130
131 // Maximum size of the cache that holds network quality estimates.
132 // Smaller size may reduce the cache hit rate due to frequent evictions.
133 // Larger size may affect performance.
134 static const uint32_t kMaximumNetworkQualityCacheSize;
135
59 // Construct a NetworkQualityEstimator instance allowing for test 136 // Construct a NetworkQualityEstimator instance allowing for test
60 // configuration. 137 // configuration.
61 // Registers for network type change notifications so estimates can be kept 138 // Registers for network type change notifications so estimates can be kept
62 // network specific. 139 // network specific.
63 // |allow_local_host_requests_for_tests| should only be true when testing 140 // |allow_local_host_requests_for_tests| should only be true when testing
64 // against local HTTP server and allows the requests to local host to be 141 // against local HTTP server and allows the requests to local host to be
65 // used for network quality estimation. 142 // used for network quality estimation.
66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); 143 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests);
67 144
68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. 145 // Updates the current network name to:
69 void OnConnectionTypeChanged( 146 // WiFi SSID (if the user is connected to a WiFi access point and the SSID
70 NetworkChangeNotifier::ConnectionType type) override; 147 // name is available), or
148 // The MCC/MNC code of the cellular carrier if the device is connected to a
bengr 2015/06/11 00:02:28 The -> the
tbansal1 2015/06/11 02:20:23 Done.
149 // cellular network.
150 // Updates the current network name to an empty string in all other cases or
151 // if the network name is not exposed by platform APIs.
152 // Virtualized for testing.
153 virtual std::string GetCurrentNetworkName() const;
154
155 // Write the estimated quality of the current network to the cache.
bengr 2015/06/11 00:02:28 Write -> Writes
tbansal1 2015/06/11 02:20:23 Done.
156 void CacheNetworkQualityEstimate();
71 157
72 // Determines if the requests to local host can be used in estimating the 158 // Determines if the requests to local host can be used in estimating the
73 // network quality. Set to true only for tests. 159 // network quality. Set to true only for tests.
74 const bool allow_localhost_requests_; 160 const bool allow_localhost_requests_;
75 161
76 // Time when last connection change was observed. 162 // Time when last connection change was observed.
77 base::TimeTicks last_connection_change_; 163 base::TimeTicks last_connection_change_;
78 164
79 // Last value passed to |OnConnectionTypeChanged|. This indicates the
80 // current connection type.
81 NetworkChangeNotifier::ConnectionType current_connection_type_;
82
83 // Set if any network data has been received since last connectivity change. 165 // Set if any network data has been received since last connectivity change.
84 bool bytes_read_since_last_connection_change_; 166 bool bytes_read_since_last_connection_change_;
85 167
86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured 168 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured
87 // from URLRequest creation until first byte received. 169 // from URLRequest creation until first byte received.
88 base::TimeDelta fastest_RTT_since_last_connection_change_; 170 base::TimeDelta fastest_RTT_since_last_connection_change_;
89 171
172 // Cache to store quality of previously seen networks.
173 CachedQualities cached_network_quality_;
174
90 // Rough measurement of downlink peak Kbps witnessed since last connectivity 175 // Rough measurement of downlink peak Kbps witnessed since last connectivity
91 // change. The accuracy is decreased by ignoring these factors: 176 // change. The accuracy is decreased by ignoring these factors:
92 // 1) Multiple URLRequests can occur concurrently. 177 // 1) Multiple URLRequests can occur concurrently.
93 // 2) The transfer time includes at least one RTT while no bytes are read. 178 // 2) The transfer time includes at least one RTT while no bytes are read.
94 uint64_t peak_kbps_since_last_connection_change_; 179 uint64_t peak_kbps_since_last_connection_change_;
95 180
181 // NetworkID of the current network.
bengr 2015/06/11 00:02:28 NetworkID -> ID
tbansal1 2015/06/11 02:20:23 Done.
182 NetworkID current_network_id_;
183
96 base::ThreadChecker thread_checker_; 184 base::ThreadChecker thread_checker_;
97 185
98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); 186 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
99 }; 187 };
100 188
101 } // namespace net 189 } // namespace net
102 190
103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ 191 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698