| 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/nqe/network_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 TestNetworkQualityEstimator( | 58 TestNetworkQualityEstimator( |
| 59 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider, | 59 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider, |
| 60 const std::map<std::string, std::string>& variation_params, | 60 const std::map<std::string, std::string>& variation_params, |
| 61 bool allow_local_host_requests_for_tests, | 61 bool allow_local_host_requests_for_tests, |
| 62 bool allow_smaller_responses_for_tests) | 62 bool allow_smaller_responses_for_tests) |
| 63 : NetworkQualityEstimator(std::move(external_estimate_provider), | 63 : NetworkQualityEstimator(std::move(external_estimate_provider), |
| 64 variation_params, | 64 variation_params, |
| 65 allow_local_host_requests_for_tests, | 65 allow_local_host_requests_for_tests, |
| 66 allow_smaller_responses_for_tests), | 66 allow_smaller_responses_for_tests), |
| 67 current_network_simulated_(false), |
| 67 url_rtt_set_(false), | 68 url_rtt_set_(false), |
| 68 downlink_throughput_kbps_set_(false) { | 69 downlink_throughput_kbps_set_(false) { |
| 69 // Set up embedded test server. | 70 // Set up embedded test server. |
| 70 embedded_test_server_.ServeFilesFromDirectory( | 71 embedded_test_server_.ServeFilesFromDirectory( |
| 71 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 72 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 72 EXPECT_TRUE(embedded_test_server_.Start()); | 73 EXPECT_TRUE(embedded_test_server_.Start()); |
| 73 embedded_test_server_.RegisterRequestHandler(base::Bind( | 74 embedded_test_server_.RegisterRequestHandler(base::Bind( |
| 74 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | 75 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); |
| 75 } | 76 } |
| 76 | 77 |
| 77 explicit TestNetworkQualityEstimator( | 78 explicit TestNetworkQualityEstimator( |
| 78 const std::map<std::string, std::string>& variation_params) | 79 const std::map<std::string, std::string>& variation_params) |
| 79 : TestNetworkQualityEstimator( | 80 : TestNetworkQualityEstimator( |
| 80 variation_params, | 81 variation_params, |
| 81 std::unique_ptr<ExternalEstimateProvider>()) {} | 82 std::unique_ptr<ExternalEstimateProvider>()) {} |
| 82 | 83 |
| 83 ~TestNetworkQualityEstimator() override {} | 84 ~TestNetworkQualityEstimator() override {} |
| 84 | 85 |
| 85 // Overrides the current network type and id. | 86 // Overrides the current network type and id. |
| 86 // Notifies network quality estimator of change in connection. | 87 // Notifies network quality estimator of change in connection. |
| 87 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, | 88 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, |
| 88 std::string network_id) { | 89 const std::string& network_id) { |
| 90 current_network_simulated_ = true; |
| 89 current_network_type_ = type; | 91 current_network_type_ = type; |
| 90 current_network_id_ = network_id; | 92 current_network_id_ = network_id; |
| 91 OnConnectionTypeChanged(type); | 93 OnConnectionTypeChanged(type); |
| 92 } | 94 } |
| 93 | 95 |
| 94 // Called by embedded server when a HTTP request is received. | 96 // Called by embedded server when a HTTP request is received. |
| 95 std::unique_ptr<test_server::HttpResponse> HandleRequest( | 97 std::unique_ptr<test_server::HttpResponse> HandleRequest( |
| 96 const test_server::HttpRequest& request) { | 98 const test_server::HttpRequest& request) { |
| 97 std::unique_ptr<test_server::BasicHttpResponse> http_response( | 99 std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 98 new test_server::BasicHttpResponse()); | 100 new test_server::BasicHttpResponse()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 *kbps = downlink_throughput_kbps_; | 132 *kbps = downlink_throughput_kbps_; |
| 131 return true; | 133 return true; |
| 132 } | 134 } |
| 133 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); | 135 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); |
| 134 } | 136 } |
| 135 | 137 |
| 136 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 138 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 137 using NetworkQualityEstimator::OnConnectionTypeChanged; | 139 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 138 | 140 |
| 139 private: | 141 private: |
| 142 // True if the network type and network id are currently simulated. This |
| 143 // ensures that the correctness of the test does not depend on the |
| 144 // actual network type of the device on which the test is running. |
| 145 bool current_network_simulated_; |
| 146 |
| 140 // NetworkQualityEstimator implementation that returns the overridden network | 147 // NetworkQualityEstimator implementation that returns the overridden network |
| 141 // id (instead of invoking platform APIs). | 148 // id (instead of invoking platform APIs). |
| 142 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 149 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 150 // GetCurrentNetworkID should be called only if the network type is |
| 151 // currently simulated. |
| 152 EXPECT_TRUE(current_network_simulated_); |
| 153 |
| 143 return NetworkQualityEstimator::NetworkID(current_network_type_, | 154 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 144 current_network_id_); | 155 current_network_id_); |
| 145 } | 156 } |
| 146 | 157 |
| 147 NetworkChangeNotifier::ConnectionType current_network_type_; | 158 NetworkChangeNotifier::ConnectionType current_network_type_; |
| 148 std::string current_network_id_; | 159 std::string current_network_id_; |
| 149 | 160 |
| 150 bool url_rtt_set_; | 161 bool url_rtt_set_; |
| 151 base::TimeDelta url_rtt_; | 162 base::TimeDelta url_rtt_; |
| 152 | 163 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 437 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 427 } | 438 } |
| 428 | 439 |
| 429 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 440 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 430 // no variation params are specified. | 441 // no variation params are specified. |
| 431 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { | 442 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { |
| 432 std::map<std::string, std::string> variation_params; | 443 std::map<std::string, std::string> variation_params; |
| 433 | 444 |
| 434 TestNetworkQualityEstimator estimator(variation_params); | 445 TestNetworkQualityEstimator estimator(variation_params); |
| 435 | 446 |
| 447 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 448 // does not return Offline if the device is offline. |
| 449 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 450 "test"); |
| 451 |
| 436 const struct { | 452 const struct { |
| 437 int32_t rtt_msec; | 453 int32_t rtt_msec; |
| 438 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 454 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 439 } tests[] = { | 455 } tests[] = { |
| 440 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 456 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 441 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 457 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 442 }; | 458 }; |
| 443 | 459 |
| 444 for (const auto& test : tests) { | 460 for (const auto& test : tests) { |
| 445 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 461 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 446 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 462 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 447 } | 463 } |
| 448 } | 464 } |
| 449 | 465 |
| 466 // Tests that |GetEffectiveConnectionType| returns |
| 467 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. |
| 468 TEST(NetworkQualityEstimatorTest, Offline) { |
| 469 std::map<std::string, std::string> variation_params; |
| 470 TestNetworkQualityEstimator estimator(variation_params); |
| 471 |
| 472 const struct { |
| 473 NetworkChangeNotifier::ConnectionType connection_type; |
| 474 NetworkQualityEstimator::EffectiveConnectionType expected_connection_type; |
| 475 } tests[] = { |
| 476 {NetworkChangeNotifier::CONNECTION_2G, |
| 477 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN}, |
| 478 {NetworkChangeNotifier::CONNECTION_NONE, |
| 479 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 480 {NetworkChangeNotifier::CONNECTION_3G, |
| 481 NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN}, |
| 482 }; |
| 483 |
| 484 for (const auto& test : tests) { |
| 485 estimator.SimulateNetworkChangeTo(test.connection_type, "test"); |
| 486 EXPECT_EQ(test.expected_connection_type, |
| 487 estimator.GetEffectiveConnectionType()); |
| 488 } |
| 489 } |
| 490 |
| 450 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 491 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 451 // only RTT thresholds are specified in the variation params. | 492 // only RTT thresholds are specified in the variation params. |
| 452 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyRTT) { | 493 TEST(NetworkQualityEstimatorTest, ObtainThresholdsOnlyRTT) { |
| 453 std::map<std::string, std::string> variation_params; | 494 std::map<std::string, std::string> variation_params; |
| 454 | 495 |
| 455 variation_params["Offline.ThresholdMedianURLRTTMsec"] = "4000"; | 496 variation_params["Offline.ThresholdMedianURLRTTMsec"] = "4000"; |
| 456 variation_params["Slow2G.ThresholdMedianURLRTTMsec"] = "2000"; | 497 variation_params["Slow2G.ThresholdMedianURLRTTMsec"] = "2000"; |
| 457 variation_params["2G.ThresholdMedianURLRTTMsec"] = "1000"; | 498 variation_params["2G.ThresholdMedianURLRTTMsec"] = "1000"; |
| 458 variation_params["3G.ThresholdMedianURLRTTMsec"] = "500"; | 499 variation_params["3G.ThresholdMedianURLRTTMsec"] = "500"; |
| 459 variation_params["4G.ThresholdMedianURLRTTMsec"] = "300"; | 500 variation_params["4G.ThresholdMedianURLRTTMsec"] = "300"; |
| 460 variation_params["Broadband.ThresholdMedianURLRTTMsec"] = "100"; | 501 variation_params["Broadband.ThresholdMedianURLRTTMsec"] = "100"; |
| 461 | 502 |
| 462 TestNetworkQualityEstimator estimator(variation_params); | 503 TestNetworkQualityEstimator estimator(variation_params); |
| 463 | 504 |
| 505 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 506 // does not return Offline if the device is offline. |
| 507 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 508 "test"); |
| 509 |
| 464 const struct { | 510 const struct { |
| 465 int32_t rtt_msec; | 511 int32_t rtt_msec; |
| 466 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 512 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 467 } tests[] = { | 513 } tests[] = { |
| 468 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 514 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 469 {4000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 515 {4000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 470 {3000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 516 {3000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 471 {2000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 517 {2000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 472 {1500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, | 518 {1500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, |
| 473 {1000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, | 519 {1000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_2G}, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 500 | 546 |
| 501 variation_params["Offline.ThresholdMedianKbps"] = "10"; | 547 variation_params["Offline.ThresholdMedianKbps"] = "10"; |
| 502 variation_params["Slow2G.ThresholdMedianKbps"] = "100"; | 548 variation_params["Slow2G.ThresholdMedianKbps"] = "100"; |
| 503 variation_params["2G.ThresholdMedianKbps"] = "300"; | 549 variation_params["2G.ThresholdMedianKbps"] = "300"; |
| 504 variation_params["3G.ThresholdMedianKbps"] = "500"; | 550 variation_params["3G.ThresholdMedianKbps"] = "500"; |
| 505 variation_params["4G.ThresholdMedianKbps"] = "1000"; | 551 variation_params["4G.ThresholdMedianKbps"] = "1000"; |
| 506 variation_params["Broadband.ThresholdMedianKbps"] = "2000"; | 552 variation_params["Broadband.ThresholdMedianKbps"] = "2000"; |
| 507 | 553 |
| 508 TestNetworkQualityEstimator estimator(variation_params); | 554 TestNetworkQualityEstimator estimator(variation_params); |
| 509 | 555 |
| 556 // Simulate the connection type as Wi-Fi so that GetEffectiveConnectionType |
| 557 // does not return Offline if the device is offline. |
| 558 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 559 "test"); |
| 560 |
| 510 const struct { | 561 const struct { |
| 511 int32_t rtt_msec; | 562 int32_t rtt_msec; |
| 512 int32_t downlink_throughput_kbps; | 563 int32_t downlink_throughput_kbps; |
| 513 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 564 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 514 } tests[] = { | 565 } tests[] = { |
| 515 // Set RTT to a very low value to observe the effect of throughput. | 566 // Set RTT to a very low value to observe the effect of throughput. |
| 516 // Throughput is the bottleneck. | 567 // Throughput is the bottleneck. |
| 517 {1, 5, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 568 {1, 5, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 518 {1, 10, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, | 569 {1, 10, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_OFFLINE}, |
| 519 {1, 50, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 570 {1, 50, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| (...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1310 histogram_tester.ExpectBucketCount("NQE.TransportRTT.Percentile50.Unknown", | 1361 histogram_tester.ExpectBucketCount("NQE.TransportRTT.Percentile50.Unknown", |
| 1311 rtt.InMilliseconds(), 1); | 1362 rtt.InMilliseconds(), 1); |
| 1312 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile10.Unknown", 1); | 1363 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile10.Unknown", 1); |
| 1313 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); | 1364 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); |
| 1314 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); | 1365 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); |
| 1315 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", | 1366 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", |
| 1316 1); | 1367 1); |
| 1317 } | 1368 } |
| 1318 | 1369 |
| 1319 } // namespace net | 1370 } // namespace net |
| OLD | NEW |