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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: net/base/network_quality_estimator.h
diff --git a/net/base/network_quality_estimator.h b/net/base/network_quality_estimator.h
index 6f3a4674cfcf3509d3b0004eac010fcb5988f4ee..723d2cfda57f0ff1441b548f0e56ed4dcf199614 100644
--- a/net/base/network_quality_estimator.h
+++ b/net/base/network_quality_estimator.h
@@ -8,6 +8,8 @@
#include <stdint.h>
#include <deque>
+#include <map>
+#include <string>
#include "base/gtest_prod_util.h"
#include "base/macros.h"
@@ -15,11 +17,10 @@
#include "base/time/time.h"
#include "net/base/net_export.h"
#include "net/base/network_change_notifier.h"
+#include "net/base/network_quality.h"
namespace net {
-class NetworkQuality;
-
// NetworkQualityEstimator provides network quality estimates (quality of the
// full paths to all origins that have been connected to).
// The estimates are based on the observed organic traffic.
@@ -51,13 +52,87 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
int64_t cumulative_prefilter_bytes_read,
int64_t prefiltered_bytes_read);
+ protected:
+ // Returns true if the cached network quality estimate was successfully read.
+ bool ReadCachedNetworkQualityEstimate();
+
+ // NetworkChangeNotifier::ConnectionTypeObserver implementation.
+ void OnConnectionTypeChanged(
+ NetworkChangeNotifier::ConnectionType type) override;
+
+ // Returns the number of entries in the network quality cache.
+ // Used only for testing.
+ size_t GetNetworkQualityCacheSizeForTests() const;
+
private:
FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
TestPeakKbpsFastestRTTUpdates);
FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
+ FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
+ FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
+ TestLRUCacheMaximumSize);
FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
+ // NetworkID is used to uniquely identify a network.
+ // For the purpose of network quality estimation and caching, a network is
+ // uniquely identified by a combination of |type| and
+ // |id|. This approach is unable to distinguish networks with
+ // same name (e.g., different Wi-Fi networks with same SSID).
+ struct NetworkID {
+ NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
+ : type(type), id(id) {}
+
+ NetworkID(const NetworkID& other) : type(other.type), id(other.id) {}
+
+ ~NetworkID() {}
+
+ NetworkID& operator=(const NetworkID& other) {
+ type = other.type;
+ id = other.id;
+ return *this;
+ }
+
+ // Overloaded because NetworkID is used as key in a map.
+ bool operator<(const NetworkID& other) const {
+ return type < other.type || (type == other.type && id < other.id);
+ }
+
+ // Connection type of the network.
+ NetworkChangeNotifier::ConnectionType type;
+
+ // Name of this network as returned by |GetCurrentNetworkName| function.
+ std::string id;
+ };
+
+ // CachedNetworkQuality stores the quality of a previously seen network.
+ class CachedNetworkQuality {
+ public:
+ explicit CachedNetworkQuality(const NetworkQuality& network_quality);
+
+ ~CachedNetworkQuality();
+
+ // Returns the network quality associated with this cached entry.
+ const NetworkQuality network_quality() const { return network_quality_; }
+
+ // Updates the network quality to the specified |median_kbps| and
+ // |median_rtt|.
+ void UpdateNetworkQuality(int32_t median_kbps,
+ const base::TimeDelta& median_rtt);
+
+ // Returns the time when this cached entry was last updated.
+ 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.
+
+ private:
+ // Time when this cache entry was last updated.
+ base::TimeTicks last_update_time_;
+
+ // Quality of this cached network.
+ NetworkQuality network_quality_;
+
+ DISALLOW_COPY_AND_ASSIGN(CachedNetworkQuality);
+ };
+
// Records the round trip time or throughput observation, along with the time
// the observation was made.
struct Observation {
@@ -99,6 +174,9 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
};
+ 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.
+ CachedNetworkQualities;
+
// Tiny transfer sizes may give inaccurate throughput results.
// Minimum size of the transfer over which the throughput is computed.
static const int kMinTransferSizeInBytes = 10000;
@@ -107,6 +185,11 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// throughput is computed.
static const int kMinRequestDurationMicroseconds = 1000;
+ // Maximum size of the cache that holds network quality estimates.
+ // Smaller size may reduce the cache hit rate due to frequent evictions.
+ // Larger size may affect performance.
+ static const size_t kMaximumNetworkQualityCacheSize;
+
// Construct a NetworkQualityEstimator instance allowing for test
// configuration.
// Registers for network type change notifications so estimates can be kept
@@ -129,9 +212,18 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// |expected_size|. Used for testing.
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
- // NetworkChangeNotifier::ConnectionTypeObserver implementation.
- void OnConnectionTypeChanged(
- NetworkChangeNotifier::ConnectionType type) override;
+ // Updates the current network name to:
+ // 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.
+ // name is available), or
+ // the MCC/MNC code of the cellular carrier if the device is connected to a
+ // cellular network.
+ // Updates the current network name to an empty string in all other cases or
+ // if the network name is not exposed by platform APIs.
+ // Virtualized for testing.
+ virtual std::string GetCurrentNetworkName() const;
+
+ // Writes the estimated quality of the current network to the cache.
+ void CacheNetworkQualityEstimate();
// Determines if the requests to local host can be used in estimating the
// network quality. Set to true only for tests.
@@ -145,14 +237,16 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// Time when last connection change was observed.
base::TimeTicks last_connection_change_;
- // Last value passed to |OnConnectionTypeChanged|. This indicates the
- // current connection type.
- NetworkChangeNotifier::ConnectionType current_connection_type_;
+ // ID of the current network.
+ NetworkID current_network_id_;
// Fastest round-trip-time (RTT) since last connectivity change. RTT measured
// from URLRequest creation until first byte received.
base::TimeDelta fastest_rtt_since_last_connection_change_;
+ // Cache that stores quality of previously seen networks.
+ CachedNetworkQualities cached_network_qualities_;
+
// Rough measurement of downstream peak Kbps witnessed since last connectivity
// change. The accuracy is decreased by ignoring these factors:
// 1) Multiple URLRequests can occur concurrently.

Powered by Google App Engine
This is Rietveld 408576698