| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "net/base/socket_performance_watcher_factory.h" | 30 #include "net/base/socket_performance_watcher_factory.h" |
| 31 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
| 32 #include "net/test/embedded_test_server/embedded_test_server.h" | 32 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 33 #include "net/test/embedded_test_server/http_request.h" | 33 #include "net/test/embedded_test_server/http_request.h" |
| 34 #include "net/test/embedded_test_server/http_response.h" | 34 #include "net/test/embedded_test_server/http_response.h" |
| 35 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 36 #include "net/url_request/url_request_test_util.h" | 36 #include "net/url_request/url_request_test_util.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 39 | 39 |
| 40 namespace net { |
| 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 // Helps in setting the current network type and id. | 44 // Helps in setting the current network type and id. |
| 43 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | 45 class TestNetworkQualityEstimator : public NetworkQualityEstimator { |
| 44 public: | 46 public: |
| 45 TestNetworkQualityEstimator( | 47 TestNetworkQualityEstimator( |
| 46 const std::map<std::string, std::string>& variation_params, | 48 const std::map<std::string, std::string>& variation_params, |
| 47 std::unique_ptr<net::ExternalEstimateProvider> external_estimate_provider) | 49 std::unique_ptr<ExternalEstimateProvider> external_estimate_provider) |
| 48 : NetworkQualityEstimator(std::move(external_estimate_provider), | 50 : NetworkQualityEstimator(std::move(external_estimate_provider), |
| 49 variation_params, | 51 variation_params, |
| 50 true, | 52 true, |
| 51 true), | 53 true), |
| 52 url_rtt_set_(false), | 54 url_rtt_set_(false), |
| 53 downlink_throughput_kbps_set_(false) { | 55 downlink_throughput_kbps_set_(false) { |
| 54 // Set up embedded test server. | 56 // Set up embedded test server. |
| 55 embedded_test_server_.ServeFilesFromDirectory( | 57 embedded_test_server_.ServeFilesFromDirectory( |
| 56 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 58 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 57 EXPECT_TRUE(embedded_test_server_.Start()); | 59 EXPECT_TRUE(embedded_test_server_.Start()); |
| 58 embedded_test_server_.RegisterRequestHandler(base::Bind( | 60 embedded_test_server_.RegisterRequestHandler(base::Bind( |
| 59 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | 61 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); |
| 60 } | 62 } |
| 61 | 63 |
| 62 explicit TestNetworkQualityEstimator( | 64 explicit TestNetworkQualityEstimator( |
| 63 const std::map<std::string, std::string>& variation_params) | 65 const std::map<std::string, std::string>& variation_params) |
| 64 : TestNetworkQualityEstimator( | 66 : TestNetworkQualityEstimator( |
| 65 variation_params, | 67 variation_params, |
| 66 std::unique_ptr<net::ExternalEstimateProvider>()) {} | 68 std::unique_ptr<ExternalEstimateProvider>()) {} |
| 67 | 69 |
| 68 ~TestNetworkQualityEstimator() override {} | 70 ~TestNetworkQualityEstimator() override {} |
| 69 | 71 |
| 70 // Overrides the current network type and id. | 72 // Overrides the current network type and id. |
| 71 // Notifies network quality estimator of change in connection. | 73 // Notifies network quality estimator of change in connection. |
| 72 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 74 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, |
| 73 std::string network_id) { | 75 std::string network_id) { |
| 74 current_network_type_ = type; | 76 current_network_type_ = type; |
| 75 current_network_id_ = network_id; | 77 current_network_id_ = network_id; |
| 76 OnConnectionTypeChanged(type); | 78 OnConnectionTypeChanged(type); |
| 77 } | 79 } |
| 78 | 80 |
| 79 // Called by embedded server when a HTTP request is received. | 81 // Called by embedded server when a HTTP request is received. |
| 80 std::unique_ptr<net::test_server::HttpResponse> HandleRequest( | 82 std::unique_ptr<test_server::HttpResponse> HandleRequest( |
| 81 const net::test_server::HttpRequest& request) { | 83 const test_server::HttpRequest& request) { |
| 82 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( | 84 std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 83 new net::test_server::BasicHttpResponse()); | 85 new test_server::BasicHttpResponse()); |
| 84 http_response->set_code(net::HTTP_OK); | 86 http_response->set_code(HTTP_OK); |
| 85 http_response->set_content("hello"); | 87 http_response->set_content("hello"); |
| 86 http_response->set_content_type("text/plain"); | 88 http_response->set_content_type("text/plain"); |
| 87 return std::move(http_response); | 89 return std::move(http_response); |
| 88 } | 90 } |
| 89 | 91 |
| 90 // Returns a GURL hosted at embedded test server. | 92 // Returns a GURL hosted at embedded test server. |
| 91 const GURL GetEchoURL() const { | 93 const GURL GetEchoURL() const { |
| 92 return embedded_test_server_.GetURL("/echo.html"); | 94 return embedded_test_server_.GetURL("/echo.html"); |
| 93 } | 95 } |
| 94 | 96 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 122 using NetworkQualityEstimator::OnConnectionTypeChanged; | 124 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 123 | 125 |
| 124 private: | 126 private: |
| 125 // NetworkQualityEstimator implementation that returns the overridden network | 127 // NetworkQualityEstimator implementation that returns the overridden network |
| 126 // id (instead of invoking platform APIs). | 128 // id (instead of invoking platform APIs). |
| 127 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 129 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 128 return NetworkQualityEstimator::NetworkID(current_network_type_, | 130 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 129 current_network_id_); | 131 current_network_id_); |
| 130 } | 132 } |
| 131 | 133 |
| 132 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 134 NetworkChangeNotifier::ConnectionType current_network_type_; |
| 133 std::string current_network_id_; | 135 std::string current_network_id_; |
| 134 | 136 |
| 135 bool url_rtt_set_; | 137 bool url_rtt_set_; |
| 136 base::TimeDelta url_rtt_; | 138 base::TimeDelta url_rtt_; |
| 137 | 139 |
| 138 bool downlink_throughput_kbps_set_; | 140 bool downlink_throughput_kbps_set_; |
| 139 int32_t downlink_throughput_kbps_; | 141 int32_t downlink_throughput_kbps_; |
| 140 | 142 |
| 141 // Embedded server used for testing. | 143 // Embedded server used for testing. |
| 142 net::EmbeddedTestServer embedded_test_server_; | 144 EmbeddedTestServer embedded_test_server_; |
| 143 | 145 |
| 144 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | 146 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| 145 }; | 147 }; |
| 146 | 148 |
| 147 class TestRTTObserver : public net::NetworkQualityEstimator::RTTObserver { | 149 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { |
| 148 public: | 150 public: |
| 149 struct Observation { | 151 struct Observation { |
| 150 Observation(int32_t ms, | 152 Observation(int32_t ms, |
| 151 const base::TimeTicks& ts, | 153 const base::TimeTicks& ts, |
| 152 net::NetworkQualityEstimator::ObservationSource src) | 154 NetworkQualityEstimator::ObservationSource src) |
| 153 : rtt_ms(ms), timestamp(ts), source(src) {} | 155 : rtt_ms(ms), timestamp(ts), source(src) {} |
| 154 int32_t rtt_ms; | 156 int32_t rtt_ms; |
| 155 base::TimeTicks timestamp; | 157 base::TimeTicks timestamp; |
| 156 net::NetworkQualityEstimator::ObservationSource source; | 158 NetworkQualityEstimator::ObservationSource source; |
| 157 }; | 159 }; |
| 158 | 160 |
| 159 std::vector<Observation>& observations() { return observations_; } | 161 std::vector<Observation>& observations() { return observations_; } |
| 160 | 162 |
| 161 // RttObserver implementation: | 163 // RttObserver implementation: |
| 162 void OnRTTObservation( | 164 void OnRTTObservation( |
| 163 int32_t rtt_ms, | 165 int32_t rtt_ms, |
| 164 const base::TimeTicks& timestamp, | 166 const base::TimeTicks& timestamp, |
| 165 net::NetworkQualityEstimator::ObservationSource source) override { | 167 NetworkQualityEstimator::ObservationSource source) override { |
| 166 observations_.push_back(Observation(rtt_ms, timestamp, source)); | 168 observations_.push_back(Observation(rtt_ms, timestamp, source)); |
| 167 } | 169 } |
| 168 | 170 |
| 169 private: | 171 private: |
| 170 std::vector<Observation> observations_; | 172 std::vector<Observation> observations_; |
| 171 }; | 173 }; |
| 172 | 174 |
| 173 class TestThroughputObserver | 175 class TestThroughputObserver |
| 174 : public net::NetworkQualityEstimator::ThroughputObserver { | 176 : public NetworkQualityEstimator::ThroughputObserver { |
| 175 public: | 177 public: |
| 176 struct Observation { | 178 struct Observation { |
| 177 Observation(int32_t kbps, | 179 Observation(int32_t kbps, |
| 178 const base::TimeTicks& ts, | 180 const base::TimeTicks& ts, |
| 179 net::NetworkQualityEstimator::ObservationSource src) | 181 NetworkQualityEstimator::ObservationSource src) |
| 180 : throughput_kbps(kbps), timestamp(ts), source(src) {} | 182 : throughput_kbps(kbps), timestamp(ts), source(src) {} |
| 181 int32_t throughput_kbps; | 183 int32_t throughput_kbps; |
| 182 base::TimeTicks timestamp; | 184 base::TimeTicks timestamp; |
| 183 net::NetworkQualityEstimator::ObservationSource source; | 185 NetworkQualityEstimator::ObservationSource source; |
| 184 }; | 186 }; |
| 185 | 187 |
| 186 std::vector<Observation>& observations() { return observations_; } | 188 std::vector<Observation>& observations() { return observations_; } |
| 187 | 189 |
| 188 // ThroughputObserver implementation: | 190 // ThroughputObserver implementation: |
| 189 void OnThroughputObservation( | 191 void OnThroughputObservation( |
| 190 int32_t throughput_kbps, | 192 int32_t throughput_kbps, |
| 191 const base::TimeTicks& timestamp, | 193 const base::TimeTicks& timestamp, |
| 192 net::NetworkQualityEstimator::ObservationSource source) override { | 194 NetworkQualityEstimator::ObservationSource source) override { |
| 193 observations_.push_back(Observation(throughput_kbps, timestamp, source)); | 195 observations_.push_back(Observation(throughput_kbps, timestamp, source)); |
| 194 } | 196 } |
| 195 | 197 |
| 196 private: | 198 private: |
| 197 std::vector<Observation> observations_; | 199 std::vector<Observation> observations_; |
| 198 }; | 200 }; |
| 199 | 201 |
| 200 } // namespace | 202 } // namespace |
| 201 | 203 |
| 202 namespace net { | |
| 203 | |
| 204 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 204 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 205 base::HistogramTester histogram_tester; | 205 base::HistogramTester histogram_tester; |
| 206 // Enable requests to local host to be used for network quality estimation. | 206 // Enable requests to local host to be used for network quality estimation. |
| 207 std::map<std::string, std::string> variation_params; | 207 std::map<std::string, std::string> variation_params; |
| 208 TestNetworkQualityEstimator estimator(variation_params); | 208 TestNetworkQualityEstimator estimator(variation_params); |
| 209 | 209 |
| 210 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 210 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 211 estimator.GetURLRequestRTTEstimateInternal(base::TimeTicks(), 100)); | 211 estimator.GetURLRequestRTTEstimateInternal(base::TimeTicks(), 100)); |
| 212 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 212 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 213 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 213 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 525 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 526 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); | 526 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); |
| 527 | 527 |
| 528 base::TimeDelta rtt; | 528 base::TimeDelta rtt; |
| 529 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); | 529 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 530 int32_t kbps; | 530 int32_t kbps; |
| 531 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 531 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 532 | 532 |
| 533 EXPECT_EQ(100, kbps); | 533 EXPECT_EQ(100, kbps); |
| 534 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); | 534 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); |
| 535 auto throughput_iterator = | |
| 536 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 537 EXPECT_EQ(100, (*throughput_iterator).value); | |
| 538 auto rtt_iterator = estimator.rtt_observations_.observations_.begin(); | |
| 539 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), (*rtt_iterator).value); | |
| 540 | 535 |
| 541 // Simulate network change to Wi-Fi. | 536 // Simulate network change to Wi-Fi. |
| 542 estimator.SimulateNetworkChangeTo( | 537 estimator.SimulateNetworkChangeTo( |
| 543 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | 538 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); |
| 544 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 539 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 545 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); | 540 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); |
| 546 | 541 |
| 547 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); | 542 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 548 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 543 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 549 EXPECT_EQ(200, kbps); | 544 EXPECT_EQ(200, kbps); |
| 550 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt); | 545 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt); |
| 551 | 546 |
| 552 throughput_iterator = | |
| 553 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 554 EXPECT_EQ(200, (*throughput_iterator).value); | |
| 555 rtt_iterator = estimator.rtt_observations_.observations_.begin(); | |
| 556 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), (*rtt_iterator).value); | |
| 557 | |
| 558 // Peak network quality should not be affected by the network quality | 547 // Peak network quality should not be affected by the network quality |
| 559 // estimator field trial. | 548 // estimator field trial. |
| 560 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 549 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 561 estimator.peak_network_quality_.rtt()); | 550 estimator.peak_network_quality_.rtt()); |
| 562 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 551 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 563 estimator.peak_network_quality_.downstream_throughput_kbps()); | 552 estimator.peak_network_quality_.downstream_throughput_kbps()); |
| 564 | 553 |
| 565 // Simulate network change to 2G. Only the Kbps default estimate should be | 554 // Simulate network change to 2G. Only the Kbps default estimate should be |
| 566 // available. | 555 // available. |
| 567 estimator.SimulateNetworkChangeTo( | 556 estimator.SimulateNetworkChangeTo( |
| 568 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); | 557 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); |
| 569 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 558 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 570 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); | 559 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); |
| 571 | 560 |
| 572 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); | 561 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 573 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 562 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 574 | 563 EXPECT_EQ(300, kbps); |
| 575 throughput_iterator = | |
| 576 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 577 EXPECT_EQ(300, (*throughput_iterator).value); | |
| 578 | 564 |
| 579 // Simulate network change to 3G. Default estimates should be unavailable. | 565 // Simulate network change to 3G. Default estimates should be unavailable. |
| 580 estimator.SimulateNetworkChangeTo( | 566 estimator.SimulateNetworkChangeTo( |
| 581 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); | 567 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); |
| 582 | 568 |
| 583 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); | 569 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 584 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 570 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 585 EXPECT_EQ(0U, estimator.downstream_throughput_kbps_observations_.Size()); | |
| 586 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); | |
| 587 } | 571 } |
| 588 | 572 |
| 589 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 573 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 590 // no variation params are specified. | 574 // no variation params are specified. |
| 591 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { | 575 TEST(NetworkQualityEstimatorTest, ObtainThresholdsNone) { |
| 592 std::map<std::string, std::string> variation_params; | 576 std::map<std::string, std::string> variation_params; |
| 593 | 577 |
| 594 TestNetworkQualityEstimator estimator(variation_params); | 578 TestNetworkQualityEstimator estimator(variation_params); |
| 595 | 579 |
| 596 const struct { | 580 const struct { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 678 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 695 }; | 679 }; |
| 696 | 680 |
| 697 for (const auto& test : tests) { | 681 for (const auto& test : tests) { |
| 698 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 682 estimator.set_url_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 699 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); | 683 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); |
| 700 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 684 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 701 } | 685 } |
| 702 } | 686 } |
| 703 | 687 |
| 688 // Tests if |weight_multiplier_per_second_| is set to correct value for various |
| 689 // values of half life parameter. |
| 704 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 690 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 705 // Verifies if |weight_multiplier_per_second_| is set to correct value for | |
| 706 // various values of half life parameter. | |
| 707 std::map<std::string, std::string> variation_params; | 691 std::map<std::string, std::string> variation_params; |
| 708 { | 692 |
| 709 // Half life parameter is not set. Default value of | 693 const struct { |
| 710 // |weight_multiplier_per_second_| should be used. | 694 std::string description; |
| 695 std::string variation_params_value; |
| 696 double expected_weight_multiplier; |
| 697 } tests[] = { |
| 698 {"Half life parameter is not set, default value should be used", |
| 699 std::string(), 0.988}, |
| 700 {"Half life parameter is set to negative, default value should be used", |
| 701 "-100", 0.988}, |
| 702 {"Half life parameter is set to zero, default value should be used", "0", |
| 703 0.988}, |
| 704 {"Half life parameter is set correctly", "10", 0.933}, |
| 705 }; |
| 706 |
| 707 for (const auto& test : tests) { |
| 708 variation_params["HalfLifeSeconds"] = test.variation_params_value; |
| 711 TestNetworkQualityEstimator estimator(variation_params); | 709 TestNetworkQualityEstimator estimator(variation_params); |
| 712 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | 710 EXPECT_NEAR(test.expected_weight_multiplier, |
| 713 .weight_multiplier_per_second_, | 711 estimator.weight_multiplier_per_second_, 0.001) |
| 714 0.001); | 712 << test.description; |
| 715 } | |
| 716 | |
| 717 variation_params["HalfLifeSeconds"] = "-100"; | |
| 718 { | |
| 719 // Half life parameter is set to a negative value. Default value of | |
| 720 // |weight_multiplier_per_second_| should be used. | |
| 721 TestNetworkQualityEstimator estimator(variation_params); | |
| 722 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | |
| 723 .weight_multiplier_per_second_, | |
| 724 0.001); | |
| 725 } | |
| 726 | |
| 727 variation_params["HalfLifeSeconds"] = "0"; | |
| 728 { | |
| 729 // Half life parameter is set to zero. Default value of | |
| 730 // |weight_multiplier_per_second_| should be used. | |
| 731 TestNetworkQualityEstimator estimator(variation_params); | |
| 732 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | |
| 733 .weight_multiplier_per_second_, | |
| 734 0.001); | |
| 735 } | |
| 736 | |
| 737 variation_params["HalfLifeSeconds"] = "10"; | |
| 738 { | |
| 739 // Half life parameter is set to a valid value. | |
| 740 TestNetworkQualityEstimator estimator(variation_params); | |
| 741 EXPECT_NEAR(0.933, estimator.downstream_throughput_kbps_observations_ | |
| 742 .weight_multiplier_per_second_, | |
| 743 0.001); | |
| 744 } | 713 } |
| 745 } | 714 } |
| 746 | 715 |
| 747 // Test if the network estimates are cached when network change notification | 716 // Test if the network estimates are cached when network change notification |
| 748 // is invoked. | 717 // is invoked. |
| 749 TEST(NetworkQualityEstimatorTest, TestCaching) { | 718 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 750 std::map<std::string, std::string> variation_params; | 719 std::map<std::string, std::string> variation_params; |
| 751 TestNetworkQualityEstimator estimator(variation_params); | 720 TestNetworkQualityEstimator estimator(variation_params); |
| 752 size_t expected_cache_size = 0; | 721 size_t expected_cache_size = 0; |
| 753 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 722 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 835 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); | 804 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); |
| 836 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); | 805 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); |
| 837 } | 806 } |
| 838 | 807 |
| 839 // Tests if the cache size remains bounded. Also, ensure that the cache is | 808 // Tests if the cache size remains bounded. Also, ensure that the cache is |
| 840 // LRU. | 809 // LRU. |
| 841 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { | 810 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { |
| 842 std::map<std::string, std::string> variation_params; | 811 std::map<std::string, std::string> variation_params; |
| 843 TestNetworkQualityEstimator estimator(variation_params); | 812 TestNetworkQualityEstimator estimator(variation_params); |
| 844 estimator.SimulateNetworkChangeTo( | 813 estimator.SimulateNetworkChangeTo( |
| 845 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 814 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 846 std::string()); | |
| 847 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); | 815 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); |
| 848 | 816 |
| 849 // Add 100 more networks than the maximum size of the cache. | 817 // Add 100 more networks than the maximum size of the cache. |
| 850 size_t network_count = | 818 size_t network_count = |
| 851 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; | 819 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; |
| 852 | 820 |
| 853 base::TimeTicks update_time_of_network_100; | 821 base::TimeTicks update_time_of_network_100; |
| 854 for (size_t i = 0; i < network_count; ++i) { | 822 for (size_t i = 0; i < network_count; ++i) { |
| 855 estimator.downstream_throughput_kbps_observations_.AddObservation( | 823 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 856 NetworkQualityEstimator::ThroughputObservation( | 824 NetworkQualityEstimator::ThroughputObservation( |
| 857 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); | 825 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 858 estimator.rtt_observations_.AddObservation( | 826 estimator.rtt_observations_.AddObservation( |
| 859 NetworkQualityEstimator::RttObservation( | 827 NetworkQualityEstimator::RttObservation( |
| 860 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), | 828 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), |
| 861 NetworkQualityEstimator::URL_REQUEST)); | 829 NetworkQualityEstimator::URL_REQUEST)); |
| 862 | 830 |
| 863 if (i == 100) | 831 if (i == 100) |
| 864 update_time_of_network_100 = base::TimeTicks::Now(); | 832 update_time_of_network_100 = base::TimeTicks::Now(); |
| 865 | 833 |
| 866 estimator.SimulateNetworkChangeTo( | 834 estimator.SimulateNetworkChangeTo( |
| 867 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 835 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 868 base::SizeTToString(i)); | 836 base::SizeTToString(i)); |
| 869 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) | 837 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) |
| 870 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); | 838 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); |
| 871 EXPECT_LE(estimator.cached_network_qualities_.size(), | 839 EXPECT_LE(estimator.cached_network_qualities_.size(), |
| 872 static_cast<size_t>( | 840 static_cast<size_t>( |
| 873 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); | 841 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); |
| 874 } | 842 } |
| 875 // One more call so that the last network is also written to cache. | 843 // One more call so that the last network is also written to cache. |
| 876 estimator.downstream_throughput_kbps_observations_.AddObservation( | 844 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 877 NetworkQualityEstimator::ThroughputObservation( | 845 NetworkQualityEstimator::ThroughputObservation( |
| 878 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); | 846 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 879 estimator.rtt_observations_.AddObservation( | 847 estimator.rtt_observations_.AddObservation( |
| 880 NetworkQualityEstimator::RttObservation( | 848 NetworkQualityEstimator::RttObservation( |
| 881 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), | 849 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), |
| 882 NetworkQualityEstimator::URL_REQUEST)); | 850 NetworkQualityEstimator::URL_REQUEST)); |
| 883 estimator.SimulateNetworkChangeTo( | 851 estimator.SimulateNetworkChangeTo( |
| 884 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 852 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 885 base::SizeTToString(network_count - 1)); | 853 base::SizeTToString(network_count - 1)); |
| 886 EXPECT_EQ(static_cast<size_t>( | 854 EXPECT_EQ(static_cast<size_t>( |
| 887 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), | 855 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), |
| 888 estimator.cached_network_qualities_.size()); | 856 estimator.cached_network_qualities_.size()); |
| 889 | 857 |
| 890 // Test that the cache is LRU by examining its contents. Networks in cache | 858 // Test that the cache is LRU by examining its contents. Networks in cache |
| 891 // must all be newer than the 100th network. | 859 // must all be newer than the 100th network. |
| 892 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = | 860 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = |
| 893 estimator.cached_network_qualities_.begin(); | 861 estimator.cached_network_qualities_.begin(); |
| 894 it != estimator.cached_network_qualities_.end(); ++it) { | 862 it != estimator.cached_network_qualities_.end(); ++it) { |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1322 } | 1290 } |
| 1323 // At least one notification should be received per socket performance | 1291 // At least one notification should be received per socket performance |
| 1324 // watcher. | 1292 // watcher. |
| 1325 EXPECT_LE(1U, after_count_tcp_rtt_observations - | 1293 EXPECT_LE(1U, after_count_tcp_rtt_observations - |
| 1326 before_count_tcp_rtt_observations) | 1294 before_count_tcp_rtt_observations) |
| 1327 << i; | 1295 << i; |
| 1328 } | 1296 } |
| 1329 } | 1297 } |
| 1330 | 1298 |
| 1331 } // namespace net | 1299 } // namespace net |
| OLD | NEW |