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

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: Using map instead of vector, added NetworkID struct 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);
49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); 62 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
50 63
64 // NetworkID is used to uniquely identify a network.
65 // For the purpose of network quality estimation and caching, a network is
66 // uniquely identified by a combination of |connection_type_| and
67 // |network_name_|. This approach is unable to distinguish networks with
68 // same name (e.g., different Wi-Fi networks with same SSID).
69 struct NetworkID {
70 NetworkChangeNotifier::ConnectionType type;
71 std::string id;
72
73 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
74 : type(type), id(id) {}
75
76 ~NetworkID() {}
77
78 NetworkID& operator=(const NetworkID& other) {
79 type = other.type;
80 id = other.id;
81 return *this;
82 }
83
84 // Overloaded because NetworkID is used as key in a map.
85 bool operator<(const NetworkID& other) const {
86 return type < other.type || (type == other.type && id < other.id);
87 }
88 };
89
90 // CachedNetworkQuality stores the quality of a previously seen network.
91 class CachedNetworkQuality {
92 public:
93 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
94
95 ~CachedNetworkQuality();
96
97 // Updates the network quality to the specified |median_kbps| and
98 // |median_rtt|.
99 void UpdateNetworkQuality(uint64_t median_kbps,
100 const base::TimeDelta& median_rtt);
101
102 // Returns the time when this cached entry was last updated.
103 base::TimeTicks last_update_time() const { return last_update_time_; }
104
105 private:
106 // Time when this cache entry was last updated.
107 base::TimeTicks last_update_time_;
108
109 // Quality of this cached network.
110 NetworkQuality network_quality_;
111
112 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
113 };
114
115 typedef std::map<NetworkID, scoped_ptr<CachedNetworkQuality>> CachedQualities;
116
51 // Tiny transfer sizes may give inaccurate throughput results. 117 // Tiny transfer sizes may give inaccurate throughput results.
52 // Minimum size of the transfer over which the throughput is computed. 118 // Minimum size of the transfer over which the throughput is computed.
53 static const int kMinTransferSizeInBytes = 10000; 119 static const int kMinTransferSizeInBytes = 10000;
54 120
55 // Minimum duration (in microseconds) of the transfer over which the 121 // Minimum duration (in microseconds) of the transfer over which the
56 // throughput is computed. 122 // throughput is computed.
57 static const int kMinRequestDurationMicroseconds = 1000; 123 static const int kMinRequestDurationMicroseconds = 1000;
58 124
59 // Construct a NetworkQualityEstimator instance allowing for test 125 // Construct a NetworkQualityEstimator instance allowing for test
60 // configuration. 126 // configuration.
61 // Registers for network type change notifications so estimates can be kept 127 // Registers for network type change notifications so estimates can be kept
62 // network specific. 128 // network specific.
63 // |allow_local_host_requests_for_tests| should only be true when testing 129 // |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 130 // against local HTTP server and allows the requests to local host to be
65 // used for network quality estimation. 131 // used for network quality estimation.
66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); 132 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests);
67 133
68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. 134 // Updates the current network name to:
69 void OnConnectionTypeChanged( 135 // WiFi SSID (if the user is connected to a WiFi access point and the SSID
70 NetworkChangeNotifier::ConnectionType type) override; 136 // name is available), or
137 // The MCC/MNC code of the cellular carrier if the device is connected to a
138 // cellular network.
139 // Updates the current network name to an empty string in all other cases or
140 // if the network name is not exposed by platform APIs.
141 // Virtualized for testing.
142 virtual std::string GetCurrentNetworkName() const;
143
144 // Write the estimated quality of the current network to the cache.
145 void CacheNetworkQualityEstimate();
71 146
72 // Determines if the requests to local host can be used in estimating the 147 // Determines if the requests to local host can be used in estimating the
73 // network quality. Set to true only for tests. 148 // network quality. Set to true only for tests.
74 const bool allow_localhost_requests_; 149 const bool allow_localhost_requests_;
75 150
76 // Time when last connection change was observed. 151 // Time when last connection change was observed.
77 base::TimeTicks last_connection_change_; 152 base::TimeTicks last_connection_change_;
78 153
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. 154 // Set if any network data has been received since last connectivity change.
84 bool bytes_read_since_last_connection_change_; 155 bool bytes_read_since_last_connection_change_;
85 156
86 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured 157 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured
87 // from URLRequest creation until first byte received. 158 // from URLRequest creation until first byte received.
88 base::TimeDelta fastest_RTT_since_last_connection_change_; 159 base::TimeDelta fastest_RTT_since_last_connection_change_;
89 160
161 // Cache to store quality of previously seen networks.
162 CachedQualities cached_network_quality_;
163
90 // Rough measurement of downlink peak Kbps witnessed since last connectivity 164 // Rough measurement of downlink peak Kbps witnessed since last connectivity
91 // change. The accuracy is decreased by ignoring these factors: 165 // change. The accuracy is decreased by ignoring these factors:
92 // 1) Multiple URLRequests can occur concurrently. 166 // 1) Multiple URLRequests can occur concurrently.
93 // 2) The transfer time includes at least one RTT while no bytes are read. 167 // 2) The transfer time includes at least one RTT while no bytes are read.
94 uint64_t peak_kbps_since_last_connection_change_; 168 uint64_t peak_kbps_since_last_connection_change_;
95 169
170 // NetworkID of the current network.
171 NetworkID current_network_id_;
172
96 base::ThreadChecker thread_checker_; 173 base::ThreadChecker thread_checker_;
97 174
98 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); 175 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
99 }; 176 };
100 177
101 } // namespace net 178 } // namespace net
102 179
103 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ 180 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698