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

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 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 <string>
11 #include <vector>
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"
15 #include "base/memory/scoped_vector.h"
12 #include "base/threading/thread_checker.h" 16 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h" 17 #include "base/time/time.h"
14 #include "net/base/network_change_notifier.h" 18 #include "net/base/network_change_notifier.h"
15 19
16 namespace net { 20 namespace net {
17 21
18 struct NetworkQuality; 22 struct NetworkQuality;
19 23
20 // NetworkQualityEstimator provides network quality estimates (quality of the 24 // NetworkQualityEstimator provides network quality estimates (quality of the
21 // full paths to all origins that have been connected to). 25 // full paths to all origins that have been connected to).
(...skipping 14 matching lines...) Expand all
36 // Returns an estimate of the current network quality. 40 // Returns an estimate of the current network quality.
37 NetworkQuality GetEstimate() const; 41 NetworkQuality GetEstimate() const;
38 42
39 // Notifies NetworkQualityEstimator that a response has been received. 43 // Notifies NetworkQualityEstimator that a response has been received.
40 // |prefilter_bytes_read| is the count of the bytes received prior to 44 // |prefilter_bytes_read| is the count of the bytes received prior to
41 // applying filters (e.g. decompression, SDCH) from request creation time 45 // applying filters (e.g. decompression, SDCH) from request creation time
42 // until now. 46 // until now.
43 void NotifyDataReceived(const URLRequest& request, 47 void NotifyDataReceived(const URLRequest& request,
44 int64_t prefilter_bytes_read); 48 int64_t prefilter_bytes_read);
45 49
50 protected:
51 // Returns true if the cached network quality estimate was successfully read.
52 bool ReadCachedNetworkQualityEstimate();
53
54 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
55 void OnConnectionTypeChanged(
56 NetworkChangeNotifier::ConnectionType type) override;
57
58 // Set the current network name for testing.
59 void SetCurrentNetworkNameForTests(const std::string& network_name);
60
61 // Returns the number of entries in the cache. Used only for testing.
62 size_t GetCacheSizeForTests() const;
63
46 private: 64 private:
47 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, 65 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
48 TestPeakKbpsFastestRTTUpdates); 66 TestPeakKbpsFastestRTTUpdates);
49 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); 67 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
50 68
69 // CachedNetworkQuality stores the quality of a previously seen network.
70 // A network is uniquely identified by a combination of |connection_type_|
71 // and |network_name_|.
mmenke 2015/06/08 20:34:21 SSIDs are user-assigned, so unless GetWifiSSID() r
tbansal1 2015/06/09 02:23:13 Agreed. Added more comments to make it clearer tha
72 class CachedNetworkQuality {
73 public:
74 CachedNetworkQuality(NetworkChangeNotifier::ConnectionType connection_type,
75 const std::string& network_name,
76 uint64_t median_kbps,
77 const base::TimeDelta& median_rtt);
78
79 ~CachedNetworkQuality();
80
81 // Updates the network quality to the specified |median_kbps| and
82 // |median_rtt|.
83 void UpdateNetworkQuality(uint64_t median_kbps,
84 const base::TimeDelta& median_rtt);
85
86 // Returns true iff the cached network matches the specified network.
87 bool MatchesNetwork(NetworkChangeNotifier::ConnectionType connection_type,
88 const std::string& network_name) const;
89
90 // Returns the time when this cached entry was last updated.
91 base::TimeTicks last_update_time() const { return last_update_time_; }
92
93 private:
94 // Median Kbps of this cached network.
95 uint64_t median_kbps_;
96
97 // Median RTT of this cached network.
98 base::TimeDelta median_rtt_;
mmenke 2015/06/08 20:34:21 Seems like we should just have a NetworkQuality ob
tbansal1 2015/06/09 02:23:13 Done.
99
100 // Time when this cache entry was last updated.
101 base::TimeTicks last_update_time_;
102
103 // Connection type of this cached network.
104 const NetworkChangeNotifier::ConnectionType connection_type_;
105
106 // Name of this cached network.
107 const std::string network_name_;
108
109 DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
110 };
111
51 // Tiny transfer sizes may give inaccurate throughput results. 112 // Tiny transfer sizes may give inaccurate throughput results.
52 // Minimum size of the transfer over which the throughput is computed. 113 // Minimum size of the transfer over which the throughput is computed.
53 static const int kMinTransferSizeInBytes = 10000; 114 static const int kMinTransferSizeInBytes = 10000;
54 115
55 // Minimum duration (in microseconds) of the transfer over which the 116 // Minimum duration (in microseconds) of the transfer over which the
56 // throughput is computed. 117 // throughput is computed.
57 static const int kMinRequestDurationMicroseconds = 1000; 118 static const int kMinRequestDurationMicroseconds = 1000;
58 119
59 // Construct a NetworkQualityEstimator instance allowing for test 120 // Construct a NetworkQualityEstimator instance allowing for test
60 // configuration. 121 // configuration.
61 // Registers for network type change notifications so estimates can be kept 122 // Registers for network type change notifications so estimates can be kept
62 // network specific. 123 // network specific.
63 // |allow_local_host_requests_for_tests| should only be true when testing 124 // |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 125 // against local HTTP server and allows the requests to local host to be
65 // used for network quality estimation. 126 // used for network quality estimation.
66 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests); 127 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests);
67 128
68 // NetworkChangeNotifier::ConnectionTypeObserver implementation. 129 // Updates the current network name to:
69 void OnConnectionTypeChanged( 130 // WiFi SSID (if the user is connected to a WiFi access point and the SSID
70 NetworkChangeNotifier::ConnectionType type) override; 131 // name is available), or
132 // The MCC/MNC code of the cellular carrier if the device is connected to a
133 // cellular network, or
134 // "ethernet" if the device is connected to an Ethernet network.
135 // Updates the current network name to an empty string in all other cases or
136 // if the network name is not exposed by platform APIs.
137 // Virtualized for testing.
138 virtual void UpdateCurrentNetworkName();
139
140 // Write the estimated quality of the current network to the cache.
141 void CacheNetworkQualityEstimate();
71 142
72 // Determines if the requests to local host can be used in estimating the 143 // Determines if the requests to local host can be used in estimating the
73 // network quality. Set to true only for tests. 144 // network quality. Set to true only for tests.
74 const bool allow_localhost_requests_; 145 const bool allow_localhost_requests_;
75 146
76 // Time when last connection change was observed. 147 // Time when last connection change was observed.
77 base::TimeTicks last_connection_change_; 148 base::TimeTicks last_connection_change_;
78 149
79 // Last value passed to |OnConnectionTypeChanged|. This indicates the 150 // Last value passed to |OnConnectionTypeChanged|. This indicates the
80 // current connection type. 151 // current connection type.
81 NetworkChangeNotifier::ConnectionType current_connection_type_; 152 NetworkChangeNotifier::ConnectionType current_connection_type_;
82 153
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 ScopedVector<CachedNetworkQuality> 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 // Name of the current network.
171 std::string current_network_name_;
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
« no previous file with comments | « no previous file | net/base/network_quality_estimator.cc » ('j') | net/base/network_quality_estimator.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698