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

Unified Diff: net/base/network_quality_estimator_unittest.cc

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 side-by-side diff with in-line comments
Download patch
Index: net/base/network_quality_estimator_unittest.cc
diff --git a/net/base/network_quality_estimator_unittest.cc b/net/base/network_quality_estimator_unittest.cc
index 3e18657197eaea55b45cfa074d4b9e5c7f2f4aa7..48552bbd8c3d36e49aec54256aa07b7fdfea77da 100644
--- a/net/base/network_quality_estimator_unittest.cc
+++ b/net/base/network_quality_estimator_unittest.cc
@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
+#include "base/strings/safe_sprintf.h"
#include "base/test/histogram_tester.h"
#include "base/time/time.h"
#include "build/build_config.h"
@@ -20,6 +21,42 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
+namespace {
+
+// Helps in setting the current network type and id.
+class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
+ public:
+ TestNetworkQualityEstimator() : NetworkQualityEstimator(true, true) {}
+
+ ~TestNetworkQualityEstimator() override {}
+
+ // Overrides the current network type and id.
+ // Notifies network quality estimator of change in connection.
+ void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type,
+ std::string network_id) {
+ current_network_type_ = type;
+ current_network_id_ = network_id;
+ OnConnectionTypeChanged(type);
+ }
+
+ using NetworkQualityEstimator::GetNetworkQualityCacheSizeForTests;
+ using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
+ using NetworkQualityEstimator::OnConnectionTypeChanged;
+
+ private:
+ // NetworkQualityEstimator implementation that returns the overridden network
+ // id (instead of invoking platform APIs).
+ NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override {
+ return NetworkQualityEstimator::NetworkID(current_network_type_,
+ current_network_id_);
+ }
+
+ net::NetworkChangeNotifier::ConnectionType current_network_type_;
+ std::string current_network_id_;
+};
+
+} // namespace
+
namespace net {
TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
@@ -29,7 +66,7 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady());
// Enable requests to local host to be used for network quality estimation.
- NetworkQualityEstimator estimator(true, true);
+ TestNetworkQualityEstimator estimator;
{
NetworkQuality network_quality = estimator.GetPeakEstimate();
EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max());
@@ -50,7 +87,6 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
estimator.NotifyDataReceived(*request, 1000, 1000);
{
NetworkQuality network_quality = estimator.GetPeakEstimate();
- EXPECT_GT(network_quality.rtt(), base::TimeDelta());
EXPECT_LT(network_quality.rtt(), base::TimeDelta::Max());
EXPECT_GE(network_quality.downstream_throughput_kbps(), 1);
}
@@ -60,14 +96,12 @@ TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) {
histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0);
- estimator.OnConnectionTypeChanged(
- NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string());
histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1);
histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
{
NetworkQuality network_quality = estimator.GetPeakEstimate();
- EXPECT_EQ(estimator.current_connection_type_,
- NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max());
EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0);
}
@@ -95,13 +129,16 @@ TEST(NetworkQualityEstimatorTest, StoreObservations) {
estimator.NotifyDataReceived(*request, 1000, 1000);
}
- EXPECT_TRUE(estimator.VerifyBufferSizeForTests(
- estimator.GetMaximumObservationBufferSizeForTests()));
+ EXPECT_EQ(estimator.GetMaximumObservationBufferSizeForTests(),
+ estimator.GetKbpsObservationBufferSizeForTests());
+ EXPECT_EQ(estimator.GetMaximumObservationBufferSizeForTests(),
+ estimator.GetRTTObservationBufferSizeForTests());
// Verify that the stored observations are cleared on network change.
estimator.OnConnectionTypeChanged(
NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
- EXPECT_TRUE(estimator.VerifyBufferSizeForTests(0U));
+ EXPECT_EQ(0U, estimator.GetKbpsObservationBufferSizeForTests());
+ EXPECT_EQ(0U, estimator.GetRTTObservationBufferSizeForTests());
scoped_ptr<URLRequest> request(
context.CreateRequest(embedded_test_server.GetURL("/echo.html"),
@@ -119,4 +156,129 @@ TEST(NetworkQualityEstimatorTest, StoreObservations) {
}
}
+// Test if the network estimates are cached when network change notification
+// is invoked.
+TEST(NetworkQualityEstimatorTest, TestCaching) {
+ TestNetworkQualityEstimator estimator;
+ size_t expected_cache_size = 0;
+ EXPECT_EQ(expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Cache entry will be added for (NONE, "").
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
+ EXPECT_EQ(++expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Entry will be added for (2G, "test1").
+ // Also, set the network quality for (2G, "test1") so that it is stored in
+ // the cache.
+ estimator.peak_kbps_since_last_connection_change_ = 1;
+ estimator.fastest_rtt_since_last_connection_change_ =
+ base::TimeDelta::FromMilliseconds(1000);
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1");
+ EXPECT_EQ(++expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Entry will be added for (3G, "test1").
+ // Also, set the network quality for (3G, "test1") so that it is stored in
+ // the
+ // cache.
+ estimator.peak_kbps_since_last_connection_change_ = 2;
+ estimator.fastest_rtt_since_last_connection_change_ =
+ base::TimeDelta::FromMilliseconds(500);
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2");
+ EXPECT_EQ(++expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Entry will be added for (3G, "test2").
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
+ EXPECT_EQ(++expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Read the network quality for (2G, "test-1").
+ EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
+ EXPECT_EQ(1, estimator.peak_kbps_since_last_connection_change_);
+ EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000),
+ estimator.fastest_rtt_since_last_connection_change_);
+ // No new entry should be added for (2G, "test1") since it already exists
+ // in the cache.
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1");
+ EXPECT_EQ(expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Read the network quality for (3G, "test1").
+ EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
+ EXPECT_EQ(2, estimator.peak_kbps_since_last_connection_change_);
+ EXPECT_EQ(base::TimeDelta::FromMilliseconds(500),
+ estimator.fastest_rtt_since_last_connection_change_);
+ // No new entry should be added for (3G, "test1") since it already exists
+ // in the cache.
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2");
+ EXPECT_EQ(expected_cache_size,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Reading quality of (3G, "test2") should return true.
+ EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
+ EXPECT_EQ(0, estimator.peak_kbps_since_last_connection_change_);
+ EXPECT_EQ(base::TimeDelta::Max(),
+ estimator.fastest_rtt_since_last_connection_change_);
+
+ // Reading quality of (2G, "test-3") should return false.
+ estimator.SimulateNetworkChangeTo(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3");
+ EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate());
+}
+
+// Tests if the cache size remains bounded. Also, ensure that the cache is
+// LRU.
+TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) {
+ TestNetworkQualityEstimator estimator;
+ estimator.SimulateNetworkChangeTo(
+ net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
+ std::string());
+ EXPECT_EQ(1U, estimator.GetNetworkQualityCacheSizeForTests());
+
+ char network_name[20];
+ // Add 100 more networks than the maximum size of the cache.
+ size_t network_count =
+ NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100;
+
+ for (size_t i = 0; i < network_count; ++i) {
+ base::strings::SafeSPrintf(network_name, "%d", i);
+ estimator.SimulateNetworkChangeTo(
+ net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
+ network_name);
+ EXPECT_LE(estimator.GetNetworkQualityCacheSizeForTests(),
+ NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)
+ << i;
+ }
+ // One more call so that the last network is also written to cache.
+ estimator.SimulateNetworkChangeTo(
+ net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
+ network_name);
+
+ EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize,
+ estimator.GetNetworkQualityCacheSizeForTests());
+
+ // Test that the cache is LRU by examining its contents.
+ for (size_t i = 0; i < network_count; ++i) {
+ // The first 100 networks should not be present in the cache.
+ bool expect_present_in_cache = i >= 100;
+ base::strings::SafeSPrintf(network_name, "%d", i);
+
+ NetworkQualityEstimator::NetworkID network_id(
+ NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name);
+
+ bool found = estimator.cached_network_qualities_.find(network_id) !=
+ estimator.cached_network_qualities_.end();
+ EXPECT_EQ(expect_present_in_cache, found) << i;
+ }
+}
+
} // namespace net
« net/base/network_quality_estimator.cc ('K') | « net/base/network_quality_estimator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698