| OLD | NEW |
| 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 #include "net/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/safe_sprintf.h" |
| 13 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 17 #include "net/base/network_quality.h" | 18 #include "net/base/network_quality.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 24 namespace { |
| 25 |
| 26 // Helps in setting the current network type and id. |
| 27 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
| 28 public: |
| 29 TestNetworkQualityEstimator() : NetworkQualityEstimator(true, true) {} |
| 30 |
| 31 ~TestNetworkQualityEstimator() override {} |
| 32 |
| 33 // Overrides the current network type and id. |
| 34 // Notifies network quality estimator of change in connection. |
| 35 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, |
| 36 std::string network_id) { |
| 37 current_network_type_ = type; |
| 38 current_network_id_ = network_id; |
| 39 OnConnectionTypeChanged(type); |
| 40 } |
| 41 |
| 42 using NetworkQualityEstimator::GetNetworkQualityCacheSizeForTests; |
| 43 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 44 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 45 |
| 46 private: |
| 47 // NetworkQualityEstimator implementation that returns the overridden network |
| 48 // id (instead of invoking platform APIs). |
| 49 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 50 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 51 current_network_id_); |
| 52 } |
| 53 |
| 54 net::NetworkChangeNotifier::ConnectionType current_network_type_; |
| 55 std::string current_network_id_; |
| 56 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 23 namespace net { | 60 namespace net { |
| 24 | 61 |
| 25 // http://crbug.com/492410 | 62 // http://crbug.com/492410 |
| 26 TEST(NetworkQualityEstimatorTest, DISABLED_TestPeakKbpsFastestRTTUpdates) { | 63 TEST(NetworkQualityEstimatorTest, DISABLED_TestPeakKbpsFastestRTTUpdates) { |
| 27 net::test_server::EmbeddedTestServer embedded_test_server; | 64 net::test_server::EmbeddedTestServer embedded_test_server; |
| 28 embedded_test_server.ServeFilesFromDirectory( | 65 embedded_test_server.ServeFilesFromDirectory( |
| 29 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 66 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 30 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | 67 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); |
| 31 | 68 |
| 32 // Enable requests to local host to be used for network quality estimation. | 69 // Enable requests to local host to be used for network quality estimation. |
| 33 NetworkQualityEstimator estimator(true, true); | 70 TestNetworkQualityEstimator estimator; |
| 34 { | 71 { |
| 35 NetworkQuality network_quality = estimator.GetPeakEstimate(); | 72 NetworkQuality network_quality = estimator.GetPeakEstimate(); |
| 36 EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max()); | 73 EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max()); |
| 37 EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0); | 74 EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0); |
| 38 } | 75 } |
| 39 | 76 |
| 40 TestDelegate test_delegate; | 77 TestDelegate test_delegate; |
| 41 TestURLRequestContext context(false); | 78 TestURLRequestContext context(false); |
| 42 | 79 |
| 43 scoped_ptr<URLRequest> request( | 80 scoped_ptr<URLRequest> request( |
| 44 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 81 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 45 DEFAULT_PRIORITY, &test_delegate)); | 82 DEFAULT_PRIORITY, &test_delegate)); |
| 46 request->Start(); | 83 request->Start(); |
| 47 | 84 |
| 48 base::RunLoop().Run(); | 85 base::RunLoop().Run(); |
| 49 | 86 |
| 50 // Both RTT and downstream throughput should be updated. | 87 // Both RTT and downstream throughput should be updated. |
| 51 estimator.NotifyDataReceived(*request, 1000, 1000); | 88 estimator.NotifyDataReceived(*request, 1000, 1000); |
| 52 { | 89 { |
| 53 NetworkQuality network_quality = estimator.GetPeakEstimate(); | 90 NetworkQuality network_quality = estimator.GetPeakEstimate(); |
| 54 EXPECT_GT(network_quality.rtt(), base::TimeDelta()); | |
| 55 EXPECT_LT(network_quality.rtt(), base::TimeDelta::Max()); | 91 EXPECT_LT(network_quality.rtt(), base::TimeDelta::Max()); |
| 56 EXPECT_GE(network_quality.downstream_throughput_kbps(), 1); | 92 EXPECT_GE(network_quality.downstream_throughput_kbps(), 1); |
| 57 } | 93 } |
| 58 | 94 |
| 59 // Check UMA histograms. | 95 // Check UMA histograms. |
| 60 base::HistogramTester histogram_tester; | 96 base::HistogramTester histogram_tester; |
| 61 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 97 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 62 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 98 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 63 | 99 |
| 64 estimator.OnConnectionTypeChanged( | 100 estimator.SimulateNetworkChangeTo( |
| 65 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 101 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 66 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 102 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 67 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 103 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 68 { | 104 { |
| 69 NetworkQuality network_quality = estimator.GetPeakEstimate(); | 105 NetworkQuality network_quality = estimator.GetPeakEstimate(); |
| 70 EXPECT_EQ(estimator.current_connection_type_, | |
| 71 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | |
| 72 EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max()); | 106 EXPECT_EQ(network_quality.rtt(), base::TimeDelta::Max()); |
| 73 EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0); | 107 EXPECT_EQ(network_quality.downstream_throughput_kbps(), 0); |
| 74 } | 108 } |
| 75 } | 109 } |
| 76 | 110 |
| 77 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 111 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 78 net::test_server::EmbeddedTestServer embedded_test_server; | 112 net::test_server::EmbeddedTestServer embedded_test_server; |
| 79 embedded_test_server.ServeFilesFromDirectory( | 113 embedded_test_server.ServeFilesFromDirectory( |
| 80 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 114 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 81 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | 115 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); |
| 82 | 116 |
| 83 NetworkQualityEstimator estimator(true, true); | 117 NetworkQualityEstimator estimator(true, true); |
| 84 TestDelegate test_delegate; | 118 TestDelegate test_delegate; |
| 85 TestURLRequestContext context(false); | 119 TestURLRequestContext context(false); |
| 86 | 120 |
| 87 // Push 10 more observations than the maximum buffer size. | 121 // Push 10 more observations than the maximum buffer size. |
| 88 for (size_t i = 0; | 122 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 10U; ++i) { |
| 89 i < estimator.GetMaximumObservationBufferSizeForTests() + 10U; ++i) { | |
| 90 scoped_ptr<URLRequest> request( | 123 scoped_ptr<URLRequest> request( |
| 91 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 124 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 92 DEFAULT_PRIORITY, &test_delegate)); | 125 DEFAULT_PRIORITY, &test_delegate)); |
| 93 request->Start(); | 126 request->Start(); |
| 94 base::RunLoop().Run(); | 127 base::RunLoop().Run(); |
| 95 | 128 |
| 96 estimator.NotifyDataReceived(*request, 1000, 1000); | 129 estimator.NotifyDataReceived(*request, 1000, 1000); |
| 97 } | 130 } |
| 98 | 131 |
| 99 EXPECT_TRUE(estimator.VerifyBufferSizeForTests( | 132 EXPECT_EQ(NetworkQualityEstimator::kMaximumObservationsBufferSize, |
| 100 estimator.GetMaximumObservationBufferSizeForTests())); | 133 estimator.GetKbpsObservationBufferSizeForTests()); |
| 134 EXPECT_EQ(NetworkQualityEstimator::kMaximumObservationsBufferSize, |
| 135 estimator.GetRTTObservationBufferSizeForTests()); |
| 101 | 136 |
| 102 // Verify that the stored observations are cleared on network change. | 137 // Verify that the stored observations are cleared on network change. |
| 103 estimator.OnConnectionTypeChanged( | 138 estimator.OnConnectionTypeChanged( |
| 104 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 139 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 105 EXPECT_TRUE(estimator.VerifyBufferSizeForTests(0U)); | 140 EXPECT_EQ(0U, estimator.GetKbpsObservationBufferSizeForTests()); |
| 141 EXPECT_EQ(0U, estimator.GetRTTObservationBufferSizeForTests()); |
| 106 | 142 |
| 107 scoped_ptr<URLRequest> request( | 143 scoped_ptr<URLRequest> request( |
| 108 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 144 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 109 DEFAULT_PRIORITY, &test_delegate)); | 145 DEFAULT_PRIORITY, &test_delegate)); |
| 110 | 146 |
| 111 // Verify that overflow protection works. | 147 // Verify that overflow protection works. |
| 112 request->Start(); | 148 request->Start(); |
| 113 base::RunLoop().Run(); | 149 base::RunLoop().Run(); |
| 114 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), | 150 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), |
| 115 std::numeric_limits<int64_t>::max()); | 151 std::numeric_limits<int64_t>::max()); |
| 116 { | 152 { |
| 117 NetworkQuality network_quality = estimator.GetPeakEstimate(); | 153 NetworkQuality network_quality = estimator.GetPeakEstimate(); |
| 118 EXPECT_EQ(std::numeric_limits<int32_t>::max() - 1, | 154 EXPECT_EQ(std::numeric_limits<int32_t>::max() - 1, |
| 119 network_quality.downstream_throughput_kbps()); | 155 network_quality.downstream_throughput_kbps()); |
| 120 } | 156 } |
| 121 } | 157 } |
| 122 | 158 |
| 159 // Test if the network estimates are cached when network change notification |
| 160 // is invoked. |
| 161 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 162 TestNetworkQualityEstimator estimator; |
| 163 size_t expected_cache_size = 0; |
| 164 EXPECT_EQ(expected_cache_size, |
| 165 estimator.GetNetworkQualityCacheSizeForTests()); |
| 166 |
| 167 // Cache entry will not be added for (NONE, ""). |
| 168 estimator.SimulateNetworkChangeTo( |
| 169 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 170 EXPECT_EQ(expected_cache_size, |
| 171 estimator.GetNetworkQualityCacheSizeForTests()); |
| 172 |
| 173 // Entry will be added for (2G, "test1"). |
| 174 // Also, set the network quality for (2G, "test1") so that it is stored in |
| 175 // the cache. |
| 176 estimator.peak_kbps_since_last_connection_change_ = 1; |
| 177 estimator.fastest_rtt_since_last_connection_change_ = |
| 178 base::TimeDelta::FromMilliseconds(1000); |
| 179 estimator.SimulateNetworkChangeTo( |
| 180 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); |
| 181 EXPECT_EQ(++expected_cache_size, |
| 182 estimator.GetNetworkQualityCacheSizeForTests()); |
| 183 |
| 184 // Entry will be added for (3G, "test1"). |
| 185 // Also, set the network quality for (3G, "test1") so that it is stored in |
| 186 // the |
| 187 // cache. |
| 188 estimator.peak_kbps_since_last_connection_change_ = 2; |
| 189 estimator.fastest_rtt_since_last_connection_change_ = |
| 190 base::TimeDelta::FromMilliseconds(500); |
| 191 estimator.SimulateNetworkChangeTo( |
| 192 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); |
| 193 EXPECT_EQ(++expected_cache_size, |
| 194 estimator.GetNetworkQualityCacheSizeForTests()); |
| 195 |
| 196 // Entry will be added for (3G, "test2"). |
| 197 estimator.SimulateNetworkChangeTo( |
| 198 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 199 EXPECT_EQ(++expected_cache_size, |
| 200 estimator.GetNetworkQualityCacheSizeForTests()); |
| 201 |
| 202 // Read the network quality for (2G, "test-1"). |
| 203 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); |
| 204 EXPECT_EQ(1, estimator.peak_kbps_since_last_connection_change_); |
| 205 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), |
| 206 estimator.fastest_rtt_since_last_connection_change_); |
| 207 // No new entry should be added for (2G, "test1") since it already exists |
| 208 // in the cache. |
| 209 estimator.SimulateNetworkChangeTo( |
| 210 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); |
| 211 EXPECT_EQ(expected_cache_size, |
| 212 estimator.GetNetworkQualityCacheSizeForTests()); |
| 213 |
| 214 // Read the network quality for (3G, "test1"). |
| 215 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); |
| 216 EXPECT_EQ(2, estimator.peak_kbps_since_last_connection_change_); |
| 217 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), |
| 218 estimator.fastest_rtt_since_last_connection_change_); |
| 219 // No new entry should be added for (3G, "test1") since it already exists |
| 220 // in the cache. |
| 221 estimator.SimulateNetworkChangeTo( |
| 222 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); |
| 223 EXPECT_EQ(expected_cache_size, |
| 224 estimator.GetNetworkQualityCacheSizeForTests()); |
| 225 |
| 226 // Reading quality of (3G, "test2") should return true. |
| 227 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); |
| 228 EXPECT_EQ(0, estimator.peak_kbps_since_last_connection_change_); |
| 229 EXPECT_EQ(base::TimeDelta::Max(), |
| 230 estimator.fastest_rtt_since_last_connection_change_); |
| 231 |
| 232 // Reading quality of (2G, "test-3") should return false. |
| 233 estimator.SimulateNetworkChangeTo( |
| 234 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); |
| 235 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); |
| 236 } |
| 237 |
| 238 // Tests if the cache size remains bounded. Also, ensure that the cache is |
| 239 // LRU. |
| 240 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { |
| 241 TestNetworkQualityEstimator estimator; |
| 242 estimator.SimulateNetworkChangeTo( |
| 243 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 244 std::string()); |
| 245 EXPECT_EQ(0U, estimator.GetNetworkQualityCacheSizeForTests()); |
| 246 |
| 247 char network_name[20]; |
| 248 // Add 100 more networks than the maximum size of the cache. |
| 249 size_t network_count = |
| 250 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; |
| 251 |
| 252 for (size_t i = 0; i < network_count; ++i) { |
| 253 base::strings::SafeSPrintf(network_name, "%d", i); |
| 254 estimator.SimulateNetworkChangeTo( |
| 255 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 256 network_name); |
| 257 EXPECT_LE(estimator.GetNetworkQualityCacheSizeForTests(), |
| 258 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) |
| 259 << i; |
| 260 } |
| 261 // One more call so that the last network is also written to cache. |
| 262 estimator.SimulateNetworkChangeTo( |
| 263 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 264 network_name); |
| 265 |
| 266 EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize, |
| 267 estimator.GetNetworkQualityCacheSizeForTests()); |
| 268 |
| 269 // Test that the cache is LRU by examining its contents. |
| 270 for (size_t i = 0; i < network_count; ++i) { |
| 271 // The first 100 networks should not be present in the cache. |
| 272 bool expect_present_in_cache = i >= 100; |
| 273 base::strings::SafeSPrintf(network_name, "%d", i); |
| 274 |
| 275 NetworkQualityEstimator::NetworkID network_id( |
| 276 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 277 |
| 278 bool found = estimator.cached_network_qualities_.find(network_id) != |
| 279 estimator.cached_network_qualities_.end(); |
| 280 EXPECT_EQ(expect_present_in_cache, found) << i; |
| 281 } |
| 282 } |
| 283 |
| 123 } // namespace net | 284 } // namespace net |
| OLD | NEW |