| 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 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/metrics/histogram_samples.h" | 13 #include "base/metrics/histogram_samples.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/safe_sprintf.h" |
| 15 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
| 19 #include "net/base/network_change_notifier.h" | 20 #include "net/base/network_change_notifier.h" |
| 20 #include "net/base/network_quality.h" | 21 #include "net/base/network_quality.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 27 namespace { |
| 28 |
| 29 // Helps in setting the current network type and id. |
| 30 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
| 31 public: |
| 32 TestNetworkQualityEstimator( |
| 33 const std::map<std::string, std::string>& variation_params) |
| 34 : NetworkQualityEstimator(variation_params, true, true) {} |
| 35 |
| 36 ~TestNetworkQualityEstimator() override {} |
| 37 |
| 38 // Overrides the current network type and id. |
| 39 // Notifies network quality estimator of change in connection. |
| 40 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, |
| 41 std::string network_id) { |
| 42 current_network_type_ = type; |
| 43 current_network_id_ = network_id; |
| 44 OnConnectionTypeChanged(type); |
| 45 } |
| 46 |
| 47 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 48 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 49 |
| 50 private: |
| 51 // NetworkQualityEstimator implementation that returns the overridden network |
| 52 // id (instead of invoking platform APIs). |
| 53 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 54 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 55 current_network_id_); |
| 56 } |
| 57 |
| 58 net::NetworkChangeNotifier::ConnectionType current_network_type_; |
| 59 std::string current_network_id_; |
| 60 }; |
| 61 |
| 62 } // namespace |
| 63 |
| 26 namespace net { | 64 namespace net { |
| 27 | 65 |
| 28 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 66 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 29 net::test_server::EmbeddedTestServer embedded_test_server; | 67 net::test_server::EmbeddedTestServer embedded_test_server; |
| 30 embedded_test_server.ServeFilesFromDirectory( | 68 embedded_test_server.ServeFilesFromDirectory( |
| 31 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 69 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 32 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | 70 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); |
| 33 | 71 |
| 34 base::HistogramTester histogram_tester; | 72 base::HistogramTester histogram_tester; |
| 35 // Enable requests to local host to be used for network quality estimation. | 73 // Enable requests to local host to be used for network quality estimation. |
| 36 std::map<std::string, std::string> variation_params; | 74 std::map<std::string, std::string> variation_params; |
| 37 NetworkQualityEstimator estimator(variation_params, true, true); | 75 TestNetworkQualityEstimator estimator(variation_params); |
| 38 { | 76 { |
| 39 NetworkQuality network_quality = estimator.GetPeakEstimate(); | 77 NetworkQuality network_quality = estimator.GetPeakEstimate(); |
| 40 EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt()); | 78 EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt()); |
| 41 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 79 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 42 network_quality.downstream_throughput_kbps()); | 80 network_quality.downstream_throughput_kbps()); |
| 43 } | 81 } |
| 44 | 82 |
| 45 TestDelegate test_delegate; | 83 TestDelegate test_delegate; |
| 46 TestURLRequestContext context(false); | 84 TestURLRequestContext context(false); |
| 47 | 85 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 64 estimator.GetEstimate(100).rtt().InMilliseconds(), 1); | 102 estimator.GetEstimate(100).rtt().InMilliseconds(), 1); |
| 65 | 103 |
| 66 // Check UMA histograms. | 104 // Check UMA histograms. |
| 67 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 105 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 68 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 106 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 69 | 107 |
| 70 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0); | 108 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0); |
| 71 | 109 |
| 72 estimator.NotifyDataReceived(*request, 1000, 1000); | 110 estimator.NotifyDataReceived(*request, 1000, 1000); |
| 73 histogram_tester.ExpectTotalCount("NQE.RTTObservations.Unknown", 1); | 111 histogram_tester.ExpectTotalCount("NQE.RTTObservations.Unknown", 1); |
| 74 | 112 estimator.SimulateNetworkChangeTo( |
| 75 estimator.OnConnectionTypeChanged( | 113 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); |
| 76 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | |
| 77 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 114 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 78 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 115 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 79 | 116 |
| 80 histogram_tester.ExpectTotalCount("NQE.RatioMedianRTT.WiFi", 0); | 117 histogram_tester.ExpectTotalCount("NQE.RatioMedianRTT.WiFi", 0); |
| 81 | 118 |
| 82 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile0.Unknown", 1); | 119 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile0.Unknown", 1); |
| 83 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile10.Unknown", 1); | 120 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile10.Unknown", 1); |
| 84 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile50.Unknown", 1); | 121 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile50.Unknown", 1); |
| 85 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile90.Unknown", 1); | 122 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile90.Unknown", 1); |
| 86 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile100.Unknown", 1); | 123 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile100.Unknown", 1); |
| 87 | 124 |
| 88 network_quality = estimator.GetPeakEstimate(); | 125 network_quality = estimator.GetPeakEstimate(); |
| 89 EXPECT_EQ(estimator.current_connection_type_, | |
| 90 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | |
| 91 EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt()); | 126 EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt()); |
| 92 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 127 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 93 network_quality.downstream_throughput_kbps()); | 128 network_quality.downstream_throughput_kbps()); |
| 129 |
| 130 estimator.SimulateNetworkChangeTo( |
| 131 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 132 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 133 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 134 network_quality = estimator.GetPeakEstimate(); |
| 135 EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt()); |
| 136 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 137 network_quality.downstream_throughput_kbps()); |
| 94 } | 138 } |
| 95 | 139 |
| 96 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 140 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 97 net::test_server::EmbeddedTestServer embedded_test_server; | 141 net::test_server::EmbeddedTestServer embedded_test_server; |
| 98 embedded_test_server.ServeFilesFromDirectory( | 142 embedded_test_server.ServeFilesFromDirectory( |
| 99 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 143 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 100 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | 144 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); |
| 101 | 145 |
| 102 std::map<std::string, std::string> variation_params; | 146 std::map<std::string, std::string> variation_params; |
| 103 NetworkQualityEstimator estimator(variation_params, true, true); | 147 TestNetworkQualityEstimator estimator(variation_params); |
| 104 TestDelegate test_delegate; | 148 TestDelegate test_delegate; |
| 105 TestURLRequestContext context(false); | 149 TestURLRequestContext context(false); |
| 106 | 150 |
| 107 // Push 10 more observations than the maximum buffer size. | 151 // Push 10 more observations than the maximum buffer size. |
| 108 for (size_t i = 0; | 152 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 10U; ++i) { |
| 109 i < estimator.GetMaximumObservationBufferSizeForTests() + 10U; ++i) { | |
| 110 scoped_ptr<URLRequest> request( | 153 scoped_ptr<URLRequest> request( |
| 111 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 154 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 112 DEFAULT_PRIORITY, &test_delegate)); | 155 DEFAULT_PRIORITY, &test_delegate)); |
| 113 request->Start(); | 156 request->Start(); |
| 114 base::RunLoop().Run(); | 157 base::RunLoop().Run(); |
| 115 | 158 |
| 116 estimator.NotifyDataReceived(*request, 1000, 1000); | 159 estimator.NotifyDataReceived(*request, 1000, 1000); |
| 117 } | 160 } |
| 118 | 161 |
| 119 EXPECT_TRUE(estimator.VerifyBufferSizeForTests( | 162 EXPECT_EQ(NetworkQualityEstimator::kMaximumObservationsBufferSize, |
| 120 estimator.GetMaximumObservationBufferSizeForTests())); | 163 estimator.kbps_observations_.Size()); |
| 164 EXPECT_EQ(NetworkQualityEstimator::kMaximumObservationsBufferSize, |
| 165 estimator.rtt_msec_observations_.Size()); |
| 121 | 166 |
| 122 // Verify that the stored observations are cleared on network change. | 167 // Verify that the stored observations are cleared on network change. |
| 123 estimator.OnConnectionTypeChanged( | 168 estimator.SimulateNetworkChangeTo( |
| 124 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 169 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2"); |
| 125 EXPECT_TRUE(estimator.VerifyBufferSizeForTests(0U)); | 170 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); |
| 171 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); |
| 126 | 172 |
| 127 scoped_ptr<URLRequest> request( | 173 scoped_ptr<URLRequest> request( |
| 128 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 174 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 129 DEFAULT_PRIORITY, &test_delegate)); | 175 DEFAULT_PRIORITY, &test_delegate)); |
| 130 | 176 |
| 131 // Verify that overflow protection works. | 177 // Verify that overflow protection works. |
| 132 request->Start(); | 178 request->Start(); |
| 133 base::RunLoop().Run(); | 179 base::RunLoop().Run(); |
| 134 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), | 180 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), |
| 135 std::numeric_limits<int64_t>::max()); | 181 std::numeric_limits<int64_t>::max()); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 290 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 245 network_quality.downstream_throughput_kbps()); | 291 network_quality.downstream_throughput_kbps()); |
| 246 | 292 |
| 247 TestDelegate test_delegate; | 293 TestDelegate test_delegate; |
| 248 TestURLRequestContext context(false); | 294 TestURLRequestContext context(false); |
| 249 | 295 |
| 250 uint64 min_transfer_size_in_bytes = | 296 uint64 min_transfer_size_in_bytes = |
| 251 NetworkQualityEstimator::kMinTransferSizeInBytes; | 297 NetworkQualityEstimator::kMinTransferSizeInBytes; |
| 252 | 298 |
| 253 // Number of observations are more than the maximum buffer size. | 299 // Number of observations are more than the maximum buffer size. |
| 254 for (size_t i = 0; | 300 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 100U; ++i) { |
| 255 i < estimator.GetMaximumObservationBufferSizeForTests() + 100U; ++i) { | |
| 256 scoped_ptr<URLRequest> request( | 301 scoped_ptr<URLRequest> request( |
| 257 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 302 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 258 DEFAULT_PRIORITY, &test_delegate)); | 303 DEFAULT_PRIORITY, &test_delegate)); |
| 259 request->Start(); | 304 request->Start(); |
| 260 base::RunLoop().Run(); | 305 base::RunLoop().Run(); |
| 261 | 306 |
| 262 // Use different number of bytes to create variation. | 307 // Use different number of bytes to create variation. |
| 263 estimator.NotifyDataReceived(*request, min_transfer_size_in_bytes + i * 100, | 308 estimator.NotifyDataReceived(*request, min_transfer_size_in_bytes + i * 100, |
| 264 min_transfer_size_in_bytes + i * 100); | 309 min_transfer_size_in_bytes + i * 100); |
| 265 } | 310 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 285 std::map<std::string, std::string> variation_params; | 330 std::map<std::string, std::string> variation_params; |
| 286 variation_params["Unknown.DefaultMedianKbps"] = "100"; | 331 variation_params["Unknown.DefaultMedianKbps"] = "100"; |
| 287 variation_params["WiFi.DefaultMedianKbps"] = "200"; | 332 variation_params["WiFi.DefaultMedianKbps"] = "200"; |
| 288 variation_params["2G.DefaultMedianKbps"] = "300"; | 333 variation_params["2G.DefaultMedianKbps"] = "300"; |
| 289 | 334 |
| 290 variation_params["Unknown.DefaultMedianRTTMsec"] = "1000"; | 335 variation_params["Unknown.DefaultMedianRTTMsec"] = "1000"; |
| 291 variation_params["WiFi.DefaultMedianRTTMsec"] = "2000"; | 336 variation_params["WiFi.DefaultMedianRTTMsec"] = "2000"; |
| 292 // Negative variation value should not be used. | 337 // Negative variation value should not be used. |
| 293 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; | 338 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; |
| 294 | 339 |
| 295 NetworkQualityEstimator estimator(variation_params, true, true); | 340 TestNetworkQualityEstimator estimator(variation_params); |
| 296 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 341 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 297 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); | 342 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); |
| 298 NetworkQuality network_quality; | 343 NetworkQuality network_quality; |
| 299 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | 344 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 300 EXPECT_EQ(100, network_quality.downstream_throughput_kbps()); | 345 EXPECT_EQ(100, network_quality.downstream_throughput_kbps()); |
| 301 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); | 346 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); |
| 302 auto it = estimator.kbps_observations_.observations_.begin(); | 347 auto it = estimator.kbps_observations_.observations_.begin(); |
| 303 EXPECT_EQ(100, (*it).value); | 348 EXPECT_EQ(100, (*it).value); |
| 304 it = estimator.rtt_msec_observations_.observations_.begin(); | 349 it = estimator.rtt_msec_observations_.observations_.begin(); |
| 305 EXPECT_EQ(1000, (*it).value); | 350 EXPECT_EQ(1000, (*it).value); |
| 306 | 351 |
| 307 // Simulate network change to Wi-Fi. | 352 // Simulate network change to Wi-Fi. |
| 308 estimator.OnConnectionTypeChanged( | 353 estimator.SimulateNetworkChangeTo( |
| 309 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 354 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); |
| 310 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 355 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 311 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); | 356 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); |
| 312 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | 357 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 313 EXPECT_EQ(200, network_quality.downstream_throughput_kbps()); | 358 EXPECT_EQ(200, network_quality.downstream_throughput_kbps()); |
| 314 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), network_quality.rtt()); | 359 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), network_quality.rtt()); |
| 315 it = estimator.kbps_observations_.observations_.begin(); | 360 it = estimator.kbps_observations_.observations_.begin(); |
| 316 EXPECT_EQ(200, (*it).value); | 361 EXPECT_EQ(200, (*it).value); |
| 317 it = estimator.rtt_msec_observations_.observations_.begin(); | 362 it = estimator.rtt_msec_observations_.observations_.begin(); |
| 318 EXPECT_EQ(2000, (*it).value); | 363 EXPECT_EQ(2000, (*it).value); |
| 319 | 364 |
| 320 // Peak network quality should not be affected by the network quality | 365 // Peak network quality should not be affected by the network quality |
| 321 // estimator field trial. | 366 // estimator field trial. |
| 322 EXPECT_EQ(NetworkQuality::InvalidRTT(), | 367 EXPECT_EQ(NetworkQuality::InvalidRTT(), |
| 323 estimator.fastest_rtt_since_last_connection_change_); | 368 estimator.peak_network_quality_.rtt()); |
| 324 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 369 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 325 estimator.peak_kbps_since_last_connection_change_); | 370 estimator.peak_network_quality_.downstream_throughput_kbps()); |
| 326 | 371 |
| 327 // Simulate network change to 2G. Only the Kbps default estimate should be | 372 // Simulate network change to 2G. Only the Kbps default estimate should be |
| 328 // available. | 373 // available. |
| 329 estimator.OnConnectionTypeChanged( | 374 estimator.SimulateNetworkChangeTo( |
| 330 NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | 375 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); |
| 331 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 376 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 332 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 377 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); |
| 333 // For GetEstimate() to return true, at least one observation must be | 378 // For GetEstimate() to return true, at least one observation must be |
| 334 // available for both RTT and downstream throughput. | 379 // available for both RTT and downstream throughput. |
| 335 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); | 380 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); |
| 336 it = estimator.kbps_observations_.observations_.begin(); | 381 it = estimator.kbps_observations_.observations_.begin(); |
| 337 EXPECT_EQ(300, (*it).value); | 382 EXPECT_EQ(300, (*it).value); |
| 338 | 383 |
| 339 // Simulate network change to 3G. Default estimates should be unavailable. | 384 // Simulate network change to 3G. Default estimates should be unavailable. |
| 340 estimator.OnConnectionTypeChanged( | 385 estimator.SimulateNetworkChangeTo( |
| 341 NetworkChangeNotifier::ConnectionType::CONNECTION_3G); | 386 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); |
| 342 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); | 387 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); |
| 343 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); | 388 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); |
| 344 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 389 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); |
| 345 } | 390 } |
| 346 | 391 |
| 347 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 392 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 348 // Verifies if |weight_multiplier_per_second_| is set to correct value for | 393 // Verifies if |weight_multiplier_per_second_| is set to correct value for |
| 349 // various values of half life parameter. | 394 // various values of half life parameter. |
| 350 std::map<std::string, std::string> variation_params; | 395 std::map<std::string, std::string> variation_params; |
| 351 { | 396 { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 380 variation_params["HalfLifeSeconds"] = "10"; | 425 variation_params["HalfLifeSeconds"] = "10"; |
| 381 { | 426 { |
| 382 // Half life parameter is set to a valid value. | 427 // Half life parameter is set to a valid value. |
| 383 NetworkQualityEstimator estimator(variation_params, true, true); | 428 NetworkQualityEstimator estimator(variation_params, true, true); |
| 384 EXPECT_NEAR(0.933, | 429 EXPECT_NEAR(0.933, |
| 385 estimator.kbps_observations_.weight_multiplier_per_second_, | 430 estimator.kbps_observations_.weight_multiplier_per_second_, |
| 386 0.001); | 431 0.001); |
| 387 } | 432 } |
| 388 } | 433 } |
| 389 | 434 |
| 435 // Test if the network estimates are cached when network change notification |
| 436 // is invoked. |
| 437 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 438 std::map<std::string, std::string> variation_params; |
| 439 TestNetworkQualityEstimator estimator(variation_params); |
| 440 size_t expected_cache_size = 0; |
| 441 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 442 |
| 443 // Cache entry will not be added for (NONE, ""). |
| 444 estimator.kbps_observations_.AddObservation( |
| 445 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); |
| 446 estimator.rtt_msec_observations_.AddObservation( |
| 447 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); |
| 448 estimator.SimulateNetworkChangeTo( |
| 449 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 450 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 451 |
| 452 // Entry will be added for (2G, "test1"). |
| 453 // Also, set the network quality for (2G, "test1") so that it is stored in |
| 454 // the cache. |
| 455 estimator.kbps_observations_.AddObservation( |
| 456 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); |
| 457 estimator.rtt_msec_observations_.AddObservation( |
| 458 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); |
| 459 |
| 460 estimator.SimulateNetworkChangeTo( |
| 461 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); |
| 462 EXPECT_EQ(++expected_cache_size, estimator.cached_network_qualities_.size()); |
| 463 |
| 464 // Entry will be added for (3G, "test1"). |
| 465 // Also, set the network quality for (3G, "test1") so that it is stored in |
| 466 // the cache. |
| 467 estimator.kbps_observations_.AddObservation( |
| 468 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); |
| 469 estimator.rtt_msec_observations_.AddObservation( |
| 470 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); |
| 471 estimator.SimulateNetworkChangeTo( |
| 472 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); |
| 473 EXPECT_EQ(++expected_cache_size, estimator.cached_network_qualities_.size()); |
| 474 |
| 475 // Entry will not be added for (3G, "test2"). |
| 476 estimator.SimulateNetworkChangeTo( |
| 477 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 478 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 479 |
| 480 // Read the network quality for (2G, "test-1"). |
| 481 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); |
| 482 NetworkQuality network_quality; |
| 483 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 484 EXPECT_EQ(1, network_quality.downstream_throughput_kbps()); |
| 485 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); |
| 486 // No new entry should be added for (2G, "test1") since it already exists |
| 487 // in the cache. |
| 488 estimator.SimulateNetworkChangeTo( |
| 489 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); |
| 490 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 491 |
| 492 // Read the network quality for (3G, "test1"). |
| 493 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); |
| 494 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 495 EXPECT_EQ(2, network_quality.downstream_throughput_kbps()); |
| 496 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), network_quality.rtt()); |
| 497 // No new entry should be added for (3G, "test1") since it already exists |
| 498 // in the cache. |
| 499 estimator.SimulateNetworkChangeTo( |
| 500 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); |
| 501 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 502 |
| 503 // Reading quality of (3G, "test2") should return false. |
| 504 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); |
| 505 |
| 506 // Reading quality of (2G, "test-3") should return false. |
| 507 estimator.SimulateNetworkChangeTo( |
| 508 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); |
| 509 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); |
| 510 } |
| 511 |
| 512 // Tests if the cache size remains bounded. Also, ensure that the cache is |
| 513 // LRU. |
| 514 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { |
| 515 std::map<std::string, std::string> variation_params; |
| 516 TestNetworkQualityEstimator estimator(variation_params); |
| 517 estimator.SimulateNetworkChangeTo( |
| 518 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 519 std::string()); |
| 520 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); |
| 521 |
| 522 char network_name[20]; |
| 523 // Add 100 more networks than the maximum size of the cache. |
| 524 size_t network_count = |
| 525 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; |
| 526 |
| 527 for (size_t i = 0; i < network_count; ++i) { |
| 528 base::strings::SafeSPrintf(network_name, "%d", i); |
| 529 estimator.kbps_observations_.AddObservation( |
| 530 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); |
| 531 estimator.rtt_msec_observations_.AddObservation( |
| 532 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); |
| 533 estimator.SimulateNetworkChangeTo( |
| 534 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 535 network_name); |
| 536 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) |
| 537 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); |
| 538 EXPECT_LE(estimator.cached_network_qualities_.size(), |
| 539 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize); |
| 540 } |
| 541 // One more call so that the last network is also written to cache. |
| 542 estimator.kbps_observations_.AddObservation( |
| 543 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); |
| 544 estimator.rtt_msec_observations_.AddObservation( |
| 545 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); |
| 546 estimator.SimulateNetworkChangeTo( |
| 547 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 548 network_name); |
| 549 EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize, |
| 550 estimator.cached_network_qualities_.size()); |
| 551 |
| 552 // Test that the cache is LRU by examining its contents. |
| 553 for (size_t i = 0; i < network_count; ++i) { |
| 554 // The first 100 networks should not be present in the cache. |
| 555 bool expect_present_in_cache = i >= 100; |
| 556 base::strings::SafeSPrintf(network_name, "%d", i); |
| 557 |
| 558 NetworkQualityEstimator::NetworkID network_id( |
| 559 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 560 |
| 561 bool found = estimator.cached_network_qualities_.find(network_id) != |
| 562 estimator.cached_network_qualities_.end(); |
| 563 EXPECT_EQ(expect_present_in_cache, found) << i << "\n" << network_count; |
| 564 } |
| 565 } |
| 566 |
| 390 } // namespace net | 567 } // namespace net |
| OLD | NEW |