Chromium Code Reviews| 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(static_cast<size_t>( |
| 120 estimator.GetMaximumObservationBufferSizeForTests())); | 163 NetworkQualityEstimator::kMaximumObservationsBufferSize), |
| 164 estimator.kbps_observations_.Size()); | |
| 165 EXPECT_EQ(static_cast<size_t>( | |
| 166 NetworkQualityEstimator::kMaximumObservationsBufferSize), | |
| 167 estimator.rtt_msec_observations_.Size()); | |
| 121 | 168 |
| 122 // Verify that the stored observations are cleared on network change. | 169 // Verify that the stored observations are cleared on network change. |
| 123 estimator.OnConnectionTypeChanged( | 170 estimator.SimulateNetworkChangeTo( |
| 124 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 171 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2"); |
| 125 EXPECT_TRUE(estimator.VerifyBufferSizeForTests(0U)); | 172 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); |
| 173 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | |
| 126 | 174 |
| 127 scoped_ptr<URLRequest> request( | 175 scoped_ptr<URLRequest> request( |
| 128 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 176 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 129 DEFAULT_PRIORITY, &test_delegate)); | 177 DEFAULT_PRIORITY, &test_delegate)); |
| 130 | 178 |
| 131 // Verify that overflow protection works. | 179 // Verify that overflow protection works. |
| 132 request->Start(); | 180 request->Start(); |
| 133 base::RunLoop().Run(); | 181 base::RunLoop().Run(); |
| 134 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), | 182 estimator.NotifyDataReceived(*request, std::numeric_limits<int64_t>::max(), |
| 135 std::numeric_limits<int64_t>::max()); | 183 std::numeric_limits<int64_t>::max()); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 292 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 245 network_quality.downstream_throughput_kbps()); | 293 network_quality.downstream_throughput_kbps()); |
| 246 | 294 |
| 247 TestDelegate test_delegate; | 295 TestDelegate test_delegate; |
| 248 TestURLRequestContext context(false); | 296 TestURLRequestContext context(false); |
| 249 | 297 |
| 250 uint64 min_transfer_size_in_bytes = | 298 uint64 min_transfer_size_in_bytes = |
| 251 NetworkQualityEstimator::kMinTransferSizeInBytes; | 299 NetworkQualityEstimator::kMinTransferSizeInBytes; |
| 252 | 300 |
| 253 // Number of observations are more than the maximum buffer size. | 301 // Number of observations are more than the maximum buffer size. |
| 254 for (size_t i = 0; | 302 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 100U; ++i) { |
| 255 i < estimator.GetMaximumObservationBufferSizeForTests() + 100U; ++i) { | |
| 256 scoped_ptr<URLRequest> request( | 303 scoped_ptr<URLRequest> request( |
| 257 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 304 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), |
| 258 DEFAULT_PRIORITY, &test_delegate)); | 305 DEFAULT_PRIORITY, &test_delegate)); |
| 259 request->Start(); | 306 request->Start(); |
| 260 base::RunLoop().Run(); | 307 base::RunLoop().Run(); |
| 261 | 308 |
| 262 // Use different number of bytes to create variation. | 309 // Use different number of bytes to create variation. |
| 263 estimator.NotifyDataReceived(*request, min_transfer_size_in_bytes + i * 100, | 310 estimator.NotifyDataReceived(*request, min_transfer_size_in_bytes + i * 100, |
| 264 min_transfer_size_in_bytes + i * 100); | 311 min_transfer_size_in_bytes + i * 100); |
| 265 } | 312 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 285 std::map<std::string, std::string> variation_params; | 332 std::map<std::string, std::string> variation_params; |
| 286 variation_params["Unknown.DefaultMedianKbps"] = "100"; | 333 variation_params["Unknown.DefaultMedianKbps"] = "100"; |
| 287 variation_params["WiFi.DefaultMedianKbps"] = "200"; | 334 variation_params["WiFi.DefaultMedianKbps"] = "200"; |
| 288 variation_params["2G.DefaultMedianKbps"] = "300"; | 335 variation_params["2G.DefaultMedianKbps"] = "300"; |
| 289 | 336 |
| 290 variation_params["Unknown.DefaultMedianRTTMsec"] = "1000"; | 337 variation_params["Unknown.DefaultMedianRTTMsec"] = "1000"; |
| 291 variation_params["WiFi.DefaultMedianRTTMsec"] = "2000"; | 338 variation_params["WiFi.DefaultMedianRTTMsec"] = "2000"; |
| 292 // Negative variation value should not be used. | 339 // Negative variation value should not be used. |
| 293 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; | 340 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; |
| 294 | 341 |
| 295 NetworkQualityEstimator estimator(variation_params, true, true); | 342 TestNetworkQualityEstimator estimator(variation_params); |
| 296 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 343 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 297 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); | 344 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); |
| 298 NetworkQuality network_quality; | 345 NetworkQuality network_quality; |
| 299 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | 346 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 300 EXPECT_EQ(100, network_quality.downstream_throughput_kbps()); | 347 EXPECT_EQ(100, network_quality.downstream_throughput_kbps()); |
| 301 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); | 348 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); |
| 302 auto it = estimator.kbps_observations_.observations_.begin(); | 349 auto it = estimator.kbps_observations_.observations_.begin(); |
| 303 EXPECT_EQ(100, (*it).value); | 350 EXPECT_EQ(100, (*it).value); |
| 304 it = estimator.rtt_msec_observations_.observations_.begin(); | 351 it = estimator.rtt_msec_observations_.observations_.begin(); |
| 305 EXPECT_EQ(1000, (*it).value); | 352 EXPECT_EQ(1000, (*it).value); |
| 306 | 353 |
| 307 // Simulate network change to Wi-Fi. | 354 // Simulate network change to Wi-Fi. |
| 308 estimator.OnConnectionTypeChanged( | 355 estimator.SimulateNetworkChangeTo( |
| 309 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 356 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); |
| 310 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 357 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 311 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); | 358 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); |
| 312 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | 359 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); |
| 313 EXPECT_EQ(200, network_quality.downstream_throughput_kbps()); | 360 EXPECT_EQ(200, network_quality.downstream_throughput_kbps()); |
| 314 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), network_quality.rtt()); | 361 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), network_quality.rtt()); |
| 315 it = estimator.kbps_observations_.observations_.begin(); | 362 it = estimator.kbps_observations_.observations_.begin(); |
| 316 EXPECT_EQ(200, (*it).value); | 363 EXPECT_EQ(200, (*it).value); |
| 317 it = estimator.rtt_msec_observations_.observations_.begin(); | 364 it = estimator.rtt_msec_observations_.observations_.begin(); |
| 318 EXPECT_EQ(2000, (*it).value); | 365 EXPECT_EQ(2000, (*it).value); |
| 319 | 366 |
| 320 // Peak network quality should not be affected by the network quality | 367 // Peak network quality should not be affected by the network quality |
| 321 // estimator field trial. | 368 // estimator field trial. |
| 322 EXPECT_EQ(NetworkQuality::InvalidRTT(), | 369 EXPECT_EQ(NetworkQuality::InvalidRTT(), |
| 323 estimator.fastest_rtt_since_last_connection_change_); | 370 estimator.peak_network_quality_.rtt()); |
| 324 EXPECT_EQ(NetworkQuality::kInvalidThroughput, | 371 EXPECT_EQ(NetworkQuality::kInvalidThroughput, |
| 325 estimator.peak_kbps_since_last_connection_change_); | 372 estimator.peak_network_quality_.downstream_throughput_kbps()); |
| 326 | 373 |
| 327 // Simulate network change to 2G. Only the Kbps default estimate should be | 374 // Simulate network change to 2G. Only the Kbps default estimate should be |
| 328 // available. | 375 // available. |
| 329 estimator.OnConnectionTypeChanged( | 376 estimator.SimulateNetworkChangeTo( |
| 330 NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | 377 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); |
| 331 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); | 378 EXPECT_EQ(1U, estimator.kbps_observations_.Size()); |
| 332 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 379 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); |
| 333 // For GetEstimate() to return true, at least one observation must be | 380 // For GetEstimate() to return true, at least one observation must be |
| 334 // available for both RTT and downstream throughput. | 381 // available for both RTT and downstream throughput. |
| 335 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); | 382 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); |
| 336 it = estimator.kbps_observations_.observations_.begin(); | 383 it = estimator.kbps_observations_.observations_.begin(); |
| 337 EXPECT_EQ(300, (*it).value); | 384 EXPECT_EQ(300, (*it).value); |
| 338 | 385 |
| 339 // Simulate network change to 3G. Default estimates should be unavailable. | 386 // Simulate network change to 3G. Default estimates should be unavailable. |
| 340 estimator.OnConnectionTypeChanged( | 387 estimator.SimulateNetworkChangeTo( |
| 341 NetworkChangeNotifier::ConnectionType::CONNECTION_3G); | 388 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); |
| 342 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); | 389 EXPECT_FALSE(estimator.GetEstimate(&network_quality)); |
| 343 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); | 390 EXPECT_EQ(0U, estimator.kbps_observations_.Size()); |
| 344 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 391 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); |
| 345 } | 392 } |
| 346 | 393 |
| 347 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 394 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 348 // Verifies if |weight_multiplier_per_second_| is set to correct value for | 395 // Verifies if |weight_multiplier_per_second_| is set to correct value for |
| 349 // various values of half life parameter. | 396 // various values of half life parameter. |
| 350 std::map<std::string, std::string> variation_params; | 397 std::map<std::string, std::string> variation_params; |
| 351 { | 398 { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 380 variation_params["HalfLifeSeconds"] = "10"; | 427 variation_params["HalfLifeSeconds"] = "10"; |
| 381 { | 428 { |
| 382 // Half life parameter is set to a valid value. | 429 // Half life parameter is set to a valid value. |
| 383 NetworkQualityEstimator estimator(variation_params, true, true); | 430 NetworkQualityEstimator estimator(variation_params, true, true); |
| 384 EXPECT_NEAR(0.933, | 431 EXPECT_NEAR(0.933, |
| 385 estimator.kbps_observations_.weight_multiplier_per_second_, | 432 estimator.kbps_observations_.weight_multiplier_per_second_, |
| 386 0.001); | 433 0.001); |
| 387 } | 434 } |
| 388 } | 435 } |
| 389 | 436 |
| 437 // Test if the network estimates are cached when network change notification | |
| 438 // is invoked. | |
| 439 TEST(NetworkQualityEstimatorTest, TestCaching) { | |
| 440 std::map<std::string, std::string> variation_params; | |
| 441 TestNetworkQualityEstimator estimator(variation_params); | |
| 442 size_t expected_cache_size = 0; | |
| 443 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 444 | |
| 445 // Cache entry will not be added for (NONE, ""). | |
| 446 estimator.kbps_observations_.AddObservation( | |
| 447 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); | |
| 448 estimator.rtt_msec_observations_.AddObservation( | |
| 449 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); | |
| 450 estimator.SimulateNetworkChangeTo( | |
| 451 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 452 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 453 | |
| 454 // Entry will be added for (2G, "test1"). | |
| 455 // Also, set the network quality for (2G, "test1") so that it is stored in | |
| 456 // the cache. | |
| 457 estimator.kbps_observations_.AddObservation( | |
| 458 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); | |
| 459 estimator.rtt_msec_observations_.AddObservation( | |
| 460 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); | |
| 461 | |
| 462 estimator.SimulateNetworkChangeTo( | |
| 463 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); | |
| 464 EXPECT_EQ(++expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 465 | |
| 466 // Entry will be added for (3G, "test1"). | |
| 467 // Also, set the network quality for (3G, "test1") so that it is stored in | |
| 468 // the cache. | |
| 469 estimator.kbps_observations_.AddObservation( | |
| 470 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | |
| 471 estimator.rtt_msec_observations_.AddObservation( | |
| 472 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | |
| 473 estimator.SimulateNetworkChangeTo( | |
| 474 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); | |
| 475 EXPECT_EQ(++expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 476 | |
| 477 // Entry will not be added for (3G, "test2"). | |
| 478 estimator.SimulateNetworkChangeTo( | |
| 479 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 480 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 481 | |
| 482 // Read the network quality for (2G, "test-1"). | |
| 483 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 484 NetworkQuality network_quality; | |
| 485 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | |
| 486 EXPECT_EQ(1, network_quality.downstream_throughput_kbps()); | |
| 487 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), network_quality.rtt()); | |
| 488 // No new entry should be added for (2G, "test1") since it already exists | |
|
pauljensen
2015/07/15 15:12:45
test1->test-1
tbansal1
2015/07/15 17:33:48
Done.
| |
| 489 // in the cache. | |
| 490 estimator.SimulateNetworkChangeTo( | |
| 491 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); | |
| 492 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 493 | |
| 494 // Read the network quality for (3G, "test1"). | |
| 495 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 496 EXPECT_TRUE(estimator.GetEstimate(&network_quality)); | |
| 497 EXPECT_EQ(2, network_quality.downstream_throughput_kbps()); | |
| 498 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), network_quality.rtt()); | |
| 499 // No new entry should be added for (3G, "test1") since it already exists | |
| 500 // in the cache. | |
| 501 estimator.SimulateNetworkChangeTo( | |
| 502 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); | |
| 503 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | |
| 504 | |
| 505 // Reading quality of (3G, "test2") should return false. | |
|
pauljensen
2015/07/15 15:12:45
test2->test-2
tbansal1
2015/07/15 17:33:47
Done.
| |
| 506 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 507 | |
| 508 // Reading quality of (2G, "test-3") should return false. | |
| 509 estimator.SimulateNetworkChangeTo( | |
| 510 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); | |
| 511 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 512 } | |
| 513 | |
| 514 // Tests if the cache size remains bounded. Also, ensure that the cache is | |
| 515 // LRU. | |
| 516 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { | |
| 517 std::map<std::string, std::string> variation_params; | |
| 518 TestNetworkQualityEstimator estimator(variation_params); | |
| 519 estimator.SimulateNetworkChangeTo( | |
| 520 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | |
| 521 std::string()); | |
| 522 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); | |
| 523 | |
| 524 char network_name[20]; | |
| 525 // Add 100 more networks than the maximum size of the cache. | |
| 526 size_t network_count = | |
| 527 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; | |
| 528 | |
| 529 for (size_t i = 0; i < network_count; ++i) { | |
| 530 base::strings::SafeSPrintf(network_name, "%d", i); | |
| 531 estimator.kbps_observations_.AddObservation( | |
| 532 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | |
| 533 estimator.rtt_msec_observations_.AddObservation( | |
| 534 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | |
| 535 estimator.SimulateNetworkChangeTo( | |
| 536 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | |
| 537 network_name); | |
|
pauljensen
2015/07/15 15:12:45
network_name->IntToString(i)
tbansal1
2015/07/15 17:33:48
Done.
| |
| 538 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) | |
| 539 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); | |
| 540 EXPECT_LE(estimator.cached_network_qualities_.size(), | |
| 541 static_cast<size_t>( | |
| 542 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); | |
| 543 } | |
| 544 // One more call so that the last network is also written to cache. | |
| 545 estimator.kbps_observations_.AddObservation( | |
| 546 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | |
| 547 estimator.rtt_msec_observations_.AddObservation( | |
| 548 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | |
| 549 estimator.SimulateNetworkChangeTo( | |
| 550 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | |
| 551 network_name); | |
| 552 EXPECT_EQ(static_cast<size_t>( | |
| 553 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), | |
| 554 estimator.cached_network_qualities_.size()); | |
| 555 | |
| 556 // Test that the cache is LRU by examining its contents. | |
| 557 for (size_t i = 0; i < network_count; ++i) { | |
| 558 // The first 100 networks should not be present in the cache. | |
| 559 bool expect_present_in_cache = i >= 100; | |
| 560 base::strings::SafeSPrintf(network_name, "%d", i); | |
| 561 | |
| 562 NetworkQualityEstimator::NetworkID network_id( | |
| 563 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); | |
|
pauljensen
2015/07/15 15:12:46
network_name->IntToString(i)
tbansal1
2015/07/15 17:33:48
Done.
| |
| 564 | |
| 565 bool found = estimator.cached_network_qualities_.find(network_id) != | |
| 566 estimator.cached_network_qualities_.end(); | |
| 567 EXPECT_EQ(expect_present_in_cache, found) << i << "\n" << network_count; | |
|
pauljensen
2015/07/15 15:12:46
can we elaborate on these "i << "\n" << network_co
tbansal1
2015/07/15 17:33:48
Done.
| |
| 568 } | |
| 569 } | |
| 570 | |
| 390 } // namespace net | 571 } // namespace net |
| OLD | NEW |