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 <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 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider) | 49 scoped_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 // Set up embedded test server. | 54 // Set up embedded test server. |
| 53 embedded_test_server_.ServeFilesFromDirectory( | 55 embedded_test_server_.ServeFilesFromDirectory( |
| 54 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 56 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 55 EXPECT_TRUE(embedded_test_server_.Start()); | 57 EXPECT_TRUE(embedded_test_server_.Start()); |
| 56 embedded_test_server_.RegisterRequestHandler(base::Bind( | 58 embedded_test_server_.RegisterRequestHandler(base::Bind( |
| 57 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | 59 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); |
| 58 } | 60 } |
| 59 | 61 |
| 60 explicit TestNetworkQualityEstimator( | 62 explicit TestNetworkQualityEstimator( |
| 61 const std::map<std::string, std::string>& variation_params) | 63 const std::map<std::string, std::string>& variation_params) |
| 62 : TestNetworkQualityEstimator( | 64 : TestNetworkQualityEstimator(variation_params, |
| 63 variation_params, | 65 scoped_ptr<ExternalEstimateProvider>()) {} |
| 64 scoped_ptr<net::ExternalEstimateProvider>()) {} | |
| 65 | 66 |
| 66 ~TestNetworkQualityEstimator() override {} | 67 ~TestNetworkQualityEstimator() override {} |
| 67 | 68 |
| 68 // Overrides the current network type and id. | 69 // Overrides the current network type and id. |
| 69 // Notifies network quality estimator of change in connection. | 70 // Notifies network quality estimator of change in connection. |
| 70 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 71 void SimulateNetworkChangeTo(NetworkChangeNotifier::ConnectionType type, |
| 71 std::string network_id) { | 72 std::string network_id) { |
| 72 current_network_type_ = type; | 73 current_network_type_ = type; |
| 73 current_network_id_ = network_id; | 74 current_network_id_ = network_id; |
| 74 OnConnectionTypeChanged(type); | 75 OnConnectionTypeChanged(type); |
| 75 } | 76 } |
| 76 | 77 |
| 77 // Called by embedded server when a HTTP request is received. | 78 // Called by embedded server when a HTTP request is received. |
| 78 scoped_ptr<net::test_server::HttpResponse> HandleRequest( | 79 scoped_ptr<test_server::HttpResponse> HandleRequest( |
| 79 const net::test_server::HttpRequest& request) { | 80 const test_server::HttpRequest& request) { |
| 80 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | 81 scoped_ptr<test_server::BasicHttpResponse> http_response( |
| 81 new net::test_server::BasicHttpResponse()); | 82 new test_server::BasicHttpResponse()); |
| 82 http_response->set_code(net::HTTP_OK); | 83 http_response->set_code(HTTP_OK); |
| 83 http_response->set_content("hello"); | 84 http_response->set_content("hello"); |
| 84 http_response->set_content_type("text/plain"); | 85 http_response->set_content_type("text/plain"); |
| 85 return std::move(http_response); | 86 return std::move(http_response); |
| 86 } | 87 } |
| 87 | 88 |
| 88 // Returns a GURL hosted at embedded test server. | 89 // Returns a GURL hosted at embedded test server. |
| 89 const GURL GetEchoURL() const { | 90 const GURL GetEchoURL() const { |
| 90 return embedded_test_server_.GetURL("/echo.html"); | 91 return embedded_test_server_.GetURL("/echo.html"); |
| 91 } | 92 } |
| 92 | 93 |
| 93 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 94 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 94 using NetworkQualityEstimator::OnConnectionTypeChanged; | 95 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 // NetworkQualityEstimator implementation that returns the overridden network | 98 // NetworkQualityEstimator implementation that returns the overridden network |
| 98 // id (instead of invoking platform APIs). | 99 // id (instead of invoking platform APIs). |
| 99 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 100 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 100 return NetworkQualityEstimator::NetworkID(current_network_type_, | 101 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 101 current_network_id_); | 102 current_network_id_); |
| 102 } | 103 } |
| 103 | 104 |
| 104 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 105 NetworkChangeNotifier::ConnectionType current_network_type_; |
| 105 std::string current_network_id_; | 106 std::string current_network_id_; |
| 106 | 107 |
| 107 // Embedded server used for testing. | 108 // Embedded server used for testing. |
| 108 net::EmbeddedTestServer embedded_test_server_; | 109 EmbeddedTestServer embedded_test_server_; |
| 109 | 110 |
| 110 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | 111 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| 111 }; | 112 }; |
| 112 | 113 |
| 113 class TestRTTObserver : public net::NetworkQualityEstimator::RTTObserver { | 114 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { |
| 114 public: | 115 public: |
| 115 struct Observation { | 116 struct Observation { |
| 116 Observation(int32_t ms, | 117 Observation(int32_t ms, |
| 117 const base::TimeTicks& ts, | 118 const base::TimeTicks& ts, |
| 118 net::NetworkQualityEstimator::ObservationSource src) | 119 NetworkQualityEstimator::ObservationSource src) |
| 119 : rtt_ms(ms), timestamp(ts), source(src) {} | 120 : rtt_ms(ms), timestamp(ts), source(src) {} |
| 120 int32_t rtt_ms; | 121 int32_t rtt_ms; |
| 121 base::TimeTicks timestamp; | 122 base::TimeTicks timestamp; |
| 122 net::NetworkQualityEstimator::ObservationSource source; | 123 NetworkQualityEstimator::ObservationSource source; |
| 123 }; | 124 }; |
| 124 | 125 |
| 125 std::vector<Observation>& observations() { return observations_; } | 126 std::vector<Observation>& observations() { return observations_; } |
| 126 | 127 |
| 127 // RttObserver implementation: | 128 // RttObserver implementation: |
| 128 void OnRTTObservation( | 129 void OnRTTObservation( |
| 129 int32_t rtt_ms, | 130 int32_t rtt_ms, |
| 130 const base::TimeTicks& timestamp, | 131 const base::TimeTicks& timestamp, |
| 131 net::NetworkQualityEstimator::ObservationSource source) override { | 132 NetworkQualityEstimator::ObservationSource source) override { |
| 132 observations_.push_back(Observation(rtt_ms, timestamp, source)); | 133 observations_.push_back(Observation(rtt_ms, timestamp, source)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 private: | 136 private: |
| 136 std::vector<Observation> observations_; | 137 std::vector<Observation> observations_; |
| 137 }; | 138 }; |
| 138 | 139 |
| 139 class TestThroughputObserver | 140 class TestThroughputObserver |
| 140 : public net::NetworkQualityEstimator::ThroughputObserver { | 141 : public NetworkQualityEstimator::ThroughputObserver { |
| 141 public: | 142 public: |
| 142 struct Observation { | 143 struct Observation { |
| 143 Observation(int32_t kbps, | 144 Observation(int32_t kbps, |
| 144 const base::TimeTicks& ts, | 145 const base::TimeTicks& ts, |
| 145 net::NetworkQualityEstimator::ObservationSource src) | 146 NetworkQualityEstimator::ObservationSource src) |
| 146 : throughput_kbps(kbps), timestamp(ts), source(src) {} | 147 : throughput_kbps(kbps), timestamp(ts), source(src) {} |
| 147 int32_t throughput_kbps; | 148 int32_t throughput_kbps; |
| 148 base::TimeTicks timestamp; | 149 base::TimeTicks timestamp; |
| 149 net::NetworkQualityEstimator::ObservationSource source; | 150 NetworkQualityEstimator::ObservationSource source; |
| 150 }; | 151 }; |
| 151 | 152 |
| 152 std::vector<Observation>& observations() { return observations_; } | 153 std::vector<Observation>& observations() { return observations_; } |
| 153 | 154 |
| 154 // ThroughputObserver implementation: | 155 // ThroughputObserver implementation: |
| 155 void OnThroughputObservation( | 156 void OnThroughputObservation( |
| 156 int32_t throughput_kbps, | 157 int32_t throughput_kbps, |
| 157 const base::TimeTicks& timestamp, | 158 const base::TimeTicks& timestamp, |
| 158 net::NetworkQualityEstimator::ObservationSource source) override { | 159 NetworkQualityEstimator::ObservationSource source) override { |
| 159 observations_.push_back(Observation(throughput_kbps, timestamp, source)); | 160 observations_.push_back(Observation(throughput_kbps, timestamp, source)); |
| 160 } | 161 } |
| 161 | 162 |
| 162 private: | 163 private: |
| 163 std::vector<Observation> observations_; | 164 std::vector<Observation> observations_; |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 } // namespace | 167 } // namespace |
| 167 | 168 |
| 168 namespace net { | |
| 169 | |
| 170 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 169 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 171 base::HistogramTester histogram_tester; | 170 base::HistogramTester histogram_tester; |
| 172 // Enable requests to local host to be used for network quality estimation. | 171 // Enable requests to local host to be used for network quality estimation. |
| 173 std::map<std::string, std::string> variation_params; | 172 std::map<std::string, std::string> variation_params; |
| 174 TestNetworkQualityEstimator estimator(variation_params); | 173 TestNetworkQualityEstimator estimator(variation_params); |
| 175 | 174 |
| 176 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 175 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 177 estimator.GetURLRequestRTTEstimateInternal(base::TimeTicks(), 100)); | 176 estimator.GetURLRequestRTTEstimateInternal(base::TimeTicks(), 100)); |
| 178 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 177 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 179 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 178 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 490 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 492 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); | 491 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); |
| 493 | 492 |
| 494 base::TimeDelta rtt; | 493 base::TimeDelta rtt; |
| 495 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); | 494 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 496 int32_t kbps; | 495 int32_t kbps; |
| 497 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 496 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 498 | 497 |
| 499 EXPECT_EQ(100, kbps); | 498 EXPECT_EQ(100, kbps); |
| 500 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); | 499 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); |
| 501 auto throughput_iterator = | |
| 502 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 503 EXPECT_EQ(100, (*throughput_iterator).value); | |
|
bengr
2016/04/21 22:00:54
Here and elsewhere I see a bunch of EXPECT stateme
tbansal1
2016/04/21 22:06:43
No they are not going to be moved. They were remov
| |
| 504 auto rtt_iterator = estimator.rtt_observations_.observations_.begin(); | |
| 505 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), (*rtt_iterator).value); | |
| 506 | 500 |
| 507 // Simulate network change to Wi-Fi. | 501 // Simulate network change to Wi-Fi. |
| 508 estimator.SimulateNetworkChangeTo( | 502 estimator.SimulateNetworkChangeTo( |
| 509 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | 503 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); |
| 510 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 504 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 511 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); | 505 EXPECT_EQ(1U, estimator.rtt_observations_.Size()); |
| 512 | 506 |
| 513 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); | 507 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 514 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 508 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 515 EXPECT_EQ(200, kbps); | 509 EXPECT_EQ(200, kbps); |
| 516 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt); | 510 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt); |
| 517 | 511 |
| 518 throughput_iterator = | |
| 519 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 520 EXPECT_EQ(200, (*throughput_iterator).value); | |
| 521 rtt_iterator = estimator.rtt_observations_.observations_.begin(); | |
| 522 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), (*rtt_iterator).value); | |
| 523 | |
| 524 // Peak network quality should not be affected by the network quality | 512 // Peak network quality should not be affected by the network quality |
| 525 // estimator field trial. | 513 // estimator field trial. |
| 526 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 514 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 527 estimator.peak_network_quality_.rtt()); | 515 estimator.peak_network_quality_.rtt()); |
| 528 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 516 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 529 estimator.peak_network_quality_.downstream_throughput_kbps()); | 517 estimator.peak_network_quality_.downstream_throughput_kbps()); |
| 530 | 518 |
| 531 // Simulate network change to 2G. Only the Kbps default estimate should be | 519 // Simulate network change to 2G. Only the Kbps default estimate should be |
| 532 // available. | 520 // available. |
| 533 estimator.SimulateNetworkChangeTo( | 521 estimator.SimulateNetworkChangeTo( |
| 534 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); | 522 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); |
| 535 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | 523 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 536 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); | 524 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); |
| 537 | 525 |
| 538 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); | 526 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 539 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 527 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 540 | 528 EXPECT_EQ(300, kbps); |
| 541 throughput_iterator = | |
| 542 estimator.downstream_throughput_kbps_observations_.observations_.begin(); | |
| 543 EXPECT_EQ(300, (*throughput_iterator).value); | |
| 544 | 529 |
| 545 // Simulate network change to 3G. Default estimates should be unavailable. | 530 // Simulate network change to 3G. Default estimates should be unavailable. |
| 546 estimator.SimulateNetworkChangeTo( | 531 estimator.SimulateNetworkChangeTo( |
| 547 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); | 532 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-3"); |
| 548 | 533 |
| 549 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); | 534 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); |
| 550 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 535 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 551 EXPECT_EQ(0U, estimator.downstream_throughput_kbps_observations_.Size()); | |
| 552 EXPECT_EQ(0U, estimator.rtt_observations_.Size()); | |
| 553 } | 536 } |
| 554 | 537 |
| 538 // Tests if |weight_multiplier_per_second_| is set to correct value for various | |
| 539 // values of half life parameter. | |
| 555 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 540 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 556 // Verifies if |weight_multiplier_per_second_| is set to correct value for | |
| 557 // various values of half life parameter. | |
| 558 std::map<std::string, std::string> variation_params; | 541 std::map<std::string, std::string> variation_params; |
| 559 { | 542 |
| 560 // Half life parameter is not set. Default value of | 543 const struct { |
| 561 // |weight_multiplier_per_second_| should be used. | 544 std::string description; |
| 545 std::string variation_params_value; | |
| 546 double expected_weight_multiplier; | |
| 547 } tests[] = { | |
| 548 {"Half life parameter is not set, default value should be used", | |
| 549 std::string(), 0.988}, | |
| 550 {"Half life parameter is set to negative, default value should be used", | |
| 551 "-100", 0.988}, | |
| 552 {"Half life parameter is set to zero, default value should be used", "0", | |
| 553 0.988}, | |
| 554 {"Half life parameter is set correctly", "10", 0.933}, | |
| 555 }; | |
| 556 | |
| 557 for (const auto& test : tests) { | |
| 558 variation_params["HalfLifeSeconds"] = test.variation_params_value; | |
| 562 TestNetworkQualityEstimator estimator(variation_params); | 559 TestNetworkQualityEstimator estimator(variation_params); |
| 563 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | 560 EXPECT_NEAR(test.expected_weight_multiplier, |
| 564 .weight_multiplier_per_second_, | 561 estimator.weight_multiplier_per_second_, 0.001) |
| 565 0.001); | 562 << test.description; |
| 566 } | |
| 567 | |
| 568 variation_params["HalfLifeSeconds"] = "-100"; | |
| 569 { | |
| 570 // Half life parameter is set to a negative value. Default value of | |
| 571 // |weight_multiplier_per_second_| should be used. | |
| 572 TestNetworkQualityEstimator estimator(variation_params); | |
| 573 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | |
| 574 .weight_multiplier_per_second_, | |
| 575 0.001); | |
| 576 } | |
| 577 | |
| 578 variation_params["HalfLifeSeconds"] = "0"; | |
| 579 { | |
| 580 // Half life parameter is set to zero. Default value of | |
| 581 // |weight_multiplier_per_second_| should be used. | |
| 582 TestNetworkQualityEstimator estimator(variation_params); | |
| 583 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | |
| 584 .weight_multiplier_per_second_, | |
| 585 0.001); | |
| 586 } | |
| 587 | |
| 588 variation_params["HalfLifeSeconds"] = "10"; | |
| 589 { | |
| 590 // Half life parameter is set to a valid value. | |
| 591 TestNetworkQualityEstimator estimator(variation_params); | |
| 592 EXPECT_NEAR(0.933, estimator.downstream_throughput_kbps_observations_ | |
| 593 .weight_multiplier_per_second_, | |
| 594 0.001); | |
| 595 } | 563 } |
| 596 } | 564 } |
| 597 | 565 |
| 598 // Test if the network estimates are cached when network change notification | 566 // Test if the network estimates are cached when network change notification |
| 599 // is invoked. | 567 // is invoked. |
| 600 TEST(NetworkQualityEstimatorTest, TestCaching) { | 568 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 601 std::map<std::string, std::string> variation_params; | 569 std::map<std::string, std::string> variation_params; |
| 602 TestNetworkQualityEstimator estimator(variation_params); | 570 TestNetworkQualityEstimator estimator(variation_params); |
| 603 size_t expected_cache_size = 0; | 571 size_t expected_cache_size = 0; |
| 604 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 572 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 686 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); | 654 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); |
| 687 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); | 655 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); |
| 688 } | 656 } |
| 689 | 657 |
| 690 // Tests if the cache size remains bounded. Also, ensure that the cache is | 658 // Tests if the cache size remains bounded. Also, ensure that the cache is |
| 691 // LRU. | 659 // LRU. |
| 692 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { | 660 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { |
| 693 std::map<std::string, std::string> variation_params; | 661 std::map<std::string, std::string> variation_params; |
| 694 TestNetworkQualityEstimator estimator(variation_params); | 662 TestNetworkQualityEstimator estimator(variation_params); |
| 695 estimator.SimulateNetworkChangeTo( | 663 estimator.SimulateNetworkChangeTo( |
| 696 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 664 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 697 std::string()); | |
| 698 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); | 665 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); |
| 699 | 666 |
| 700 // Add 100 more networks than the maximum size of the cache. | 667 // Add 100 more networks than the maximum size of the cache. |
| 701 size_t network_count = | 668 size_t network_count = |
| 702 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; | 669 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; |
| 703 | 670 |
| 704 base::TimeTicks update_time_of_network_100; | 671 base::TimeTicks update_time_of_network_100; |
| 705 for (size_t i = 0; i < network_count; ++i) { | 672 for (size_t i = 0; i < network_count; ++i) { |
| 706 estimator.downstream_throughput_kbps_observations_.AddObservation( | 673 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 707 NetworkQualityEstimator::ThroughputObservation( | 674 NetworkQualityEstimator::ThroughputObservation( |
| 708 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); | 675 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 709 estimator.rtt_observations_.AddObservation( | 676 estimator.rtt_observations_.AddObservation( |
| 710 NetworkQualityEstimator::RttObservation( | 677 NetworkQualityEstimator::RttObservation( |
| 711 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), | 678 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), |
| 712 NetworkQualityEstimator::URL_REQUEST)); | 679 NetworkQualityEstimator::URL_REQUEST)); |
| 713 | 680 |
| 714 if (i == 100) | 681 if (i == 100) |
| 715 update_time_of_network_100 = base::TimeTicks::Now(); | 682 update_time_of_network_100 = base::TimeTicks::Now(); |
| 716 | 683 |
| 717 estimator.SimulateNetworkChangeTo( | 684 estimator.SimulateNetworkChangeTo( |
| 718 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 685 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 719 base::SizeTToString(i)); | 686 base::SizeTToString(i)); |
| 720 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) | 687 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) |
| 721 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); | 688 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); |
| 722 EXPECT_LE(estimator.cached_network_qualities_.size(), | 689 EXPECT_LE(estimator.cached_network_qualities_.size(), |
| 723 static_cast<size_t>( | 690 static_cast<size_t>( |
| 724 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); | 691 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); |
| 725 } | 692 } |
| 726 // One more call so that the last network is also written to cache. | 693 // One more call so that the last network is also written to cache. |
| 727 estimator.downstream_throughput_kbps_observations_.AddObservation( | 694 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 728 NetworkQualityEstimator::ThroughputObservation( | 695 NetworkQualityEstimator::ThroughputObservation( |
| 729 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); | 696 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 730 estimator.rtt_observations_.AddObservation( | 697 estimator.rtt_observations_.AddObservation( |
| 731 NetworkQualityEstimator::RttObservation( | 698 NetworkQualityEstimator::RttObservation( |
| 732 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), | 699 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), |
| 733 NetworkQualityEstimator::URL_REQUEST)); | 700 NetworkQualityEstimator::URL_REQUEST)); |
| 734 estimator.SimulateNetworkChangeTo( | 701 estimator.SimulateNetworkChangeTo( |
| 735 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 702 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 736 base::SizeTToString(network_count - 1)); | 703 base::SizeTToString(network_count - 1)); |
| 737 EXPECT_EQ(static_cast<size_t>( | 704 EXPECT_EQ(static_cast<size_t>( |
| 738 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), | 705 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), |
| 739 estimator.cached_network_qualities_.size()); | 706 estimator.cached_network_qualities_.size()); |
| 740 | 707 |
| 741 // Test that the cache is LRU by examining its contents. Networks in cache | 708 // Test that the cache is LRU by examining its contents. Networks in cache |
| 742 // must all be newer than the 100th network. | 709 // must all be newer than the 100th network. |
| 743 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = | 710 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = |
| 744 estimator.cached_network_qualities_.begin(); | 711 estimator.cached_network_qualities_.begin(); |
| 745 it != estimator.cached_network_qualities_.end(); ++it) { | 712 it != estimator.cached_network_qualities_.end(); ++it) { |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1172 } | 1139 } |
| 1173 // At least one notification should be received per socket performance | 1140 // At least one notification should be received per socket performance |
| 1174 // watcher. | 1141 // watcher. |
| 1175 EXPECT_LE(1U, after_count_tcp_rtt_observations - | 1142 EXPECT_LE(1U, after_count_tcp_rtt_observations - |
| 1176 before_count_tcp_rtt_observations) | 1143 before_count_tcp_rtt_observations) |
| 1177 << i; | 1144 << i; |
| 1178 } | 1145 } |
| 1179 } | 1146 } |
| 1180 | 1147 |
| 1181 } // namespace net | 1148 } // namespace net |
| OLD | NEW |