| 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> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/metrics/histogram_samples.h" | 20 #include "base/metrics/histogram_samples.h" |
| 21 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 24 #include "base/test/simple_test_tick_clock.h" | 24 #include "base/test/simple_test_tick_clock.h" |
| 25 #include "base/threading/platform_thread.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 27 #include "net/base/load_flags.h" | 28 #include "net/base/load_flags.h" |
| 28 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 29 #include "net/http/http_status_code.h" | 30 #include "net/http/http_status_code.h" |
| 30 #include "net/nqe/external_estimate_provider.h" | 31 #include "net/nqe/external_estimate_provider.h" |
| 31 #include "net/nqe/network_quality_observation.h" | 32 #include "net/nqe/network_quality_observation.h" |
| 32 #include "net/nqe/network_quality_observation_source.h" | 33 #include "net/nqe/network_quality_observation_source.h" |
| 33 #include "net/nqe/observation_buffer.h" | 34 #include "net/nqe/observation_buffer.h" |
| 34 #include "net/socket/socket_performance_watcher.h" | 35 #include "net/socket/socket_performance_watcher.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 61 const std::map<std::string, std::string>& variation_params, | 62 const std::map<std::string, std::string>& variation_params, |
| 62 bool allow_local_host_requests_for_tests, | 63 bool allow_local_host_requests_for_tests, |
| 63 bool allow_smaller_responses_for_tests) | 64 bool allow_smaller_responses_for_tests) |
| 64 : NetworkQualityEstimator(std::move(external_estimate_provider), | 65 : NetworkQualityEstimator(std::move(external_estimate_provider), |
| 65 variation_params, | 66 variation_params, |
| 66 allow_local_host_requests_for_tests, | 67 allow_local_host_requests_for_tests, |
| 67 allow_smaller_responses_for_tests), | 68 allow_smaller_responses_for_tests), |
| 68 effective_connection_type_set_(false), | 69 effective_connection_type_set_(false), |
| 69 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | 70 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 70 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN), | 71 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN), |
| 72 accuracy_recording_intervals_set_(false), |
| 71 http_rtt_set_(false), | 73 http_rtt_set_(false), |
| 74 recent_http_rtt_set_(false), |
| 75 transport_rtt_set_(false), |
| 76 recent_transport_rtt_set_(false), |
| 72 downlink_throughput_kbps_set_(false) { | 77 downlink_throughput_kbps_set_(false) { |
| 73 // Set up embedded test server. | 78 // Set up embedded test server. |
| 74 embedded_test_server_.ServeFilesFromDirectory( | 79 embedded_test_server_.ServeFilesFromDirectory( |
| 75 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 80 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 76 EXPECT_TRUE(embedded_test_server_.Start()); | 81 EXPECT_TRUE(embedded_test_server_.Start()); |
| 77 embedded_test_server_.RegisterRequestHandler(base::Bind( | 82 embedded_test_server_.RegisterRequestHandler(base::Bind( |
| 78 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | 83 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); |
| 79 } | 84 } |
| 80 | 85 |
| 81 explicit TestNetworkQualityEstimator( | 86 explicit TestNetworkQualityEstimator( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 104 http_response->set_content("hello"); | 109 http_response->set_content("hello"); |
| 105 http_response->set_content_type("text/plain"); | 110 http_response->set_content_type("text/plain"); |
| 106 return std::move(http_response); | 111 return std::move(http_response); |
| 107 } | 112 } |
| 108 | 113 |
| 109 // Returns a GURL hosted at embedded test server. | 114 // Returns a GURL hosted at embedded test server. |
| 110 const GURL GetEchoURL() const { | 115 const GURL GetEchoURL() const { |
| 111 return embedded_test_server_.GetURL("/echo.html"); | 116 return embedded_test_server_.GetURL("/echo.html"); |
| 112 } | 117 } |
| 113 | 118 |
| 119 void set_effective_connection_type(EffectiveConnectionType type) { |
| 120 effective_connection_type_set_ = true; |
| 121 effective_connection_type_ = type; |
| 122 } |
| 123 |
| 124 // Returns the effective connection type that was set using |
| 125 // |set_effective_connection_type|. If connection type has not been set, then |
| 126 // the base implementation is called. |
| 127 EffectiveConnectionType GetEffectiveConnectionType() const override { |
| 128 if (effective_connection_type_set_) |
| 129 return effective_connection_type_; |
| 130 return NetworkQualityEstimator::GetEffectiveConnectionType(); |
| 131 } |
| 132 |
| 114 void set_http_rtt(const base::TimeDelta& http_rtt) { | 133 void set_http_rtt(const base::TimeDelta& http_rtt) { |
| 115 http_rtt_set_ = true; | 134 http_rtt_set_ = true; |
| 116 http_rtt_ = http_rtt; | 135 http_rtt_ = http_rtt; |
| 117 } | 136 } |
| 118 | 137 |
| 119 void set_effective_connection_type(EffectiveConnectionType type) { | 138 // Returns the HTTP RTT that was set using |set_http_rtt|. If the HTTP RTT has |
| 120 effective_connection_type_set_ = true; | 139 // not been set, then the base implementation is called. |
| 121 effective_connection_type_ = type; | |
| 122 } | |
| 123 | |
| 124 EffectiveConnectionType GetEffectiveConnectionType() const override { | |
| 125 if (effective_connection_type_set_) | |
| 126 return effective_connection_type_; | |
| 127 return NetworkQualityEstimator::GetEffectiveConnectionType(); | |
| 128 } | |
| 129 | |
| 130 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { | 140 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { |
| 131 if (http_rtt_set_) { | 141 if (http_rtt_set_) { |
| 132 *rtt = http_rtt_; | 142 *rtt = http_rtt_; |
| 133 return true; | 143 return true; |
| 134 } | 144 } |
| 135 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); | 145 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); |
| 136 } | 146 } |
| 137 | 147 |
| 148 void set_recent_http_rtt(const base::TimeDelta& recent_http_rtt) { |
| 149 recent_http_rtt_set_ = true; |
| 150 recent_http_rtt_ = recent_http_rtt; |
| 151 } |
| 152 |
| 153 // Returns the recent HTTP RTT that was set using |set_recent_http_rtt|. If |
| 154 // the recent HTTP RTT has not been set, then the base implementation is |
| 155 // called. |
| 138 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, | 156 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, |
| 139 base::TimeDelta* rtt) const override { | 157 base::TimeDelta* rtt) const override { |
| 140 if (http_rtt_set_) { | 158 if (recent_http_rtt_set_) { |
| 141 *rtt = http_rtt_; | 159 *rtt = recent_http_rtt_; |
| 142 return true; | 160 return true; |
| 143 } | 161 } |
| 144 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt); | 162 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt); |
| 145 } | 163 } |
| 146 | 164 |
| 165 void set_transport_rtt(const base::TimeDelta& transport_rtt) { |
| 166 transport_rtt_set_ = true; |
| 167 transport_rtt_ = transport_rtt; |
| 168 } |
| 169 |
| 170 // Returns the transport RTT that was set using |set_transport_rtt|. If the |
| 171 // transport RTT has not been set, then the base implementation is called. |
| 172 bool GetTransportRTTEstimate(base::TimeDelta* rtt) const override { |
| 173 if (transport_rtt_set_) { |
| 174 *rtt = transport_rtt_; |
| 175 return true; |
| 176 } |
| 177 return NetworkQualityEstimator::GetTransportRTTEstimate(rtt); |
| 178 } |
| 179 |
| 180 void set_recent_transport_rtt(const base::TimeDelta& recent_transport_rtt) { |
| 181 recent_transport_rtt_set_ = true; |
| 182 recent_transport_rtt_ = recent_transport_rtt; |
| 183 } |
| 184 |
| 185 // Returns the recent transport RTT that was set using |
| 186 // |set_recent_transport_rtt|. If the recent transport RTT has not been set, |
| 187 // then the base implementation is called. |
| 188 bool GetRecentTransportRTTMedian(const base::TimeTicks& start_time, |
| 189 base::TimeDelta* rtt) const override { |
| 190 if (recent_transport_rtt_set_) { |
| 191 *rtt = recent_transport_rtt_; |
| 192 return true; |
| 193 } |
| 194 return NetworkQualityEstimator::GetRecentTransportRTTMedian(start_time, |
| 195 rtt); |
| 196 } |
| 197 |
| 147 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) { | 198 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) { |
| 148 downlink_throughput_kbps_set_ = true; | 199 downlink_throughput_kbps_set_ = true; |
| 149 downlink_throughput_kbps_ = downlink_throughput_kbps; | 200 downlink_throughput_kbps_ = downlink_throughput_kbps; |
| 150 } | 201 } |
| 151 | 202 |
| 203 // Returns the downlink throughput that was set using |
| 204 // |set_downlink_throughput_kbps|. If the downlink throughput has not been |
| 205 // set, then the base implementation is called. |
| 152 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override { | 206 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override { |
| 153 if (downlink_throughput_kbps_set_) { | 207 if (downlink_throughput_kbps_set_) { |
| 154 *kbps = downlink_throughput_kbps_; | 208 *kbps = downlink_throughput_kbps_; |
| 155 return true; | 209 return true; |
| 156 } | 210 } |
| 157 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); | 211 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); |
| 158 } | 212 } |
| 159 | 213 |
| 214 // Returns the downlink throughput that was set using |
| 215 // |set_downlink_throughput_kbps|. If the downlink throughput has not been |
| 216 // set, then the base implementation is called. |
| 160 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time, | 217 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time, |
| 161 int32_t* kbps) const override { | 218 int32_t* kbps) const override { |
| 162 if (downlink_throughput_kbps_set_) { | 219 if (downlink_throughput_kbps_set_) { |
| 163 *kbps = downlink_throughput_kbps_; | 220 *kbps = downlink_throughput_kbps_; |
| 164 return true; | 221 return true; |
| 165 } | 222 } |
| 166 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( | 223 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( |
| 167 start_time, kbps); | 224 start_time, kbps); |
| 168 } | 225 } |
| 169 | 226 |
| 227 void SetAccuracyRecordingIntervals( |
| 228 const std::vector<base::TimeDelta>& accuracy_recording_intervals) { |
| 229 accuracy_recording_intervals_set_ = true; |
| 230 accuracy_recording_intervals_ = accuracy_recording_intervals; |
| 231 } |
| 232 |
| 233 const std::vector<base::TimeDelta>& GetAccuracyRecordingIntervals() |
| 234 const override { |
| 235 if (accuracy_recording_intervals_set_) |
| 236 return accuracy_recording_intervals_; |
| 237 |
| 238 return NetworkQualityEstimator::GetAccuracyRecordingIntervals(); |
| 239 } |
| 240 |
| 170 using NetworkQualityEstimator::SetTickClockForTesting; | 241 using NetworkQualityEstimator::SetTickClockForTesting; |
| 171 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 242 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; |
| 172 using NetworkQualityEstimator::OnConnectionTypeChanged; | 243 using NetworkQualityEstimator::OnConnectionTypeChanged; |
| 173 | 244 |
| 174 private: | 245 private: |
| 175 // NetworkQualityEstimator implementation that returns the overridden network | 246 // NetworkQualityEstimator implementation that returns the overridden |
| 247 // network |
| 176 // id (instead of invoking platform APIs). | 248 // id (instead of invoking platform APIs). |
| 177 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 249 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { |
| 178 return NetworkQualityEstimator::NetworkID(current_network_type_, | 250 return NetworkQualityEstimator::NetworkID(current_network_type_, |
| 179 current_network_id_); | 251 current_network_id_); |
| 180 } | 252 } |
| 181 | 253 |
| 182 bool effective_connection_type_set_; | 254 bool effective_connection_type_set_; |
| 183 EffectiveConnectionType effective_connection_type_; | 255 EffectiveConnectionType effective_connection_type_; |
| 184 | 256 |
| 185 NetworkChangeNotifier::ConnectionType current_network_type_; | 257 NetworkChangeNotifier::ConnectionType current_network_type_; |
| 186 std::string current_network_id_; | 258 std::string current_network_id_; |
| 187 | 259 |
| 260 bool accuracy_recording_intervals_set_; |
| 261 std::vector<base::TimeDelta> accuracy_recording_intervals_; |
| 262 |
| 188 bool http_rtt_set_; | 263 bool http_rtt_set_; |
| 189 base::TimeDelta http_rtt_; | 264 base::TimeDelta http_rtt_; |
| 190 | 265 |
| 266 bool recent_http_rtt_set_; |
| 267 base::TimeDelta recent_http_rtt_; |
| 268 |
| 269 bool transport_rtt_set_; |
| 270 base::TimeDelta transport_rtt_; |
| 271 |
| 272 bool recent_transport_rtt_set_; |
| 273 base::TimeDelta recent_transport_rtt_; |
| 274 |
| 191 bool downlink_throughput_kbps_set_; | 275 bool downlink_throughput_kbps_set_; |
| 192 int32_t downlink_throughput_kbps_; | 276 int32_t downlink_throughput_kbps_; |
| 193 | 277 |
| 194 // Embedded server used for testing. | 278 // Embedded server used for testing. |
| 195 EmbeddedTestServer embedded_test_server_; | 279 EmbeddedTestServer embedded_test_server_; |
| 196 | 280 |
| 197 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | 281 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| 198 }; | 282 }; |
| 199 | 283 |
| 200 class TestEffectiveConnectionTypeObserver | 284 class TestEffectiveConnectionTypeObserver |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 const struct { | 590 const struct { |
| 507 int32_t rtt_msec; | 591 int32_t rtt_msec; |
| 508 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 592 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 509 } tests[] = { | 593 } tests[] = { |
| 510 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 594 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 511 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 595 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 512 }; | 596 }; |
| 513 | 597 |
| 514 for (const auto& test : tests) { | 598 for (const auto& test : tests) { |
| 515 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 599 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 600 estimator.set_recent_http_rtt( |
| 601 base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 516 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 602 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 517 } | 603 } |
| 518 } | 604 } |
| 519 | 605 |
| 520 // Tests that |GetEffectiveConnectionType| returns | 606 // Tests that |GetEffectiveConnectionType| returns |
| 521 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. | 607 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. |
| 522 TEST(NetworkQualityEstimatorTest, Offline) { | 608 TEST(NetworkQualityEstimatorTest, Offline) { |
| 523 std::map<std::string, std::string> variation_params; | 609 std::map<std::string, std::string> variation_params; |
| 524 TestNetworkQualityEstimator estimator(variation_params); | 610 TestNetworkQualityEstimator estimator(variation_params); |
| 525 | 611 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 661 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 576 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, | 662 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, |
| 577 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, | 663 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, |
| 578 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 664 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 579 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 665 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 580 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 666 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 581 }; | 667 }; |
| 582 | 668 |
| 583 for (const auto& test : tests) { | 669 for (const auto& test : tests) { |
| 584 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 670 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 671 estimator.set_recent_http_rtt( |
| 672 base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 585 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 673 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 586 } | 674 } |
| 587 } | 675 } |
| 588 | 676 |
| 589 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 677 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 590 // both RTT and throughput thresholds are specified in the variation params. | 678 // both RTT and throughput thresholds are specified in the variation params. |
| 591 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) { | 679 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) { |
| 592 std::map<std::string, std::string> variation_params; | 680 std::map<std::string, std::string> variation_params; |
| 593 | 681 |
| 594 variation_params["Offline.ThresholdMedianHttpRTTMsec"] = "4000"; | 682 variation_params["Offline.ThresholdMedianHttpRTTMsec"] = "4000"; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 // Set both RTT and throughput. RTT is the bottleneck. | 722 // Set both RTT and throughput. RTT is the bottleneck. |
| 635 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 723 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 636 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 724 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 637 // Set throughput to an invalid value. | 725 // Set throughput to an invalid value. |
| 638 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 726 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 639 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 727 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 640 }; | 728 }; |
| 641 | 729 |
| 642 for (const auto& test : tests) { | 730 for (const auto& test : tests) { |
| 643 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 731 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 732 estimator.set_recent_http_rtt( |
| 733 base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 644 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); | 734 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); |
| 645 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 735 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 646 } | 736 } |
| 647 } | 737 } |
| 648 | 738 |
| 649 // Tests if |weight_multiplier_per_second_| is set to correct value for various | 739 // Tests if |weight_multiplier_per_second_| is set to correct value for various |
| 650 // values of half life parameter. | 740 // values of half life parameter. |
| 651 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 741 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 652 std::map<std::string, std::string> variation_params; | 742 std::map<std::string, std::string> variation_params; |
| 653 | 743 |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1453 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); | 1543 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); |
| 1454 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); | 1544 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); |
| 1455 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", | 1545 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", |
| 1456 1); | 1546 1); |
| 1457 | 1547 |
| 1458 // Verify that metrics are logged correctly on main-frame requests. | 1548 // Verify that metrics are logged correctly on main-frame requests. |
| 1459 histogram_tester.ExpectTotalCount( | 1549 histogram_tester.ExpectTotalCount( |
| 1460 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); | 1550 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); |
| 1461 } | 1551 } |
| 1462 | 1552 |
| 1553 #if defined(OS_IOS) |
| 1554 // Flaky on iOS when |accuracy_recording_delay| is non-zero. |
| 1555 #define MAYBE_RecordAccuracy DISABLED_RecordAccuracy |
| 1556 #else |
| 1557 #define MAYBE_RecordAccuracy RecordAccuracy |
| 1558 #endif |
| 1559 // Tests if the NQE accuracy metrics are recorded properly. |
| 1560 TEST(NetworkQualityEstimatorTest, MAYBE_RecordAccuracy) { |
| 1561 const int expected_rtt_msec = 100; |
| 1562 |
| 1563 const base::TimeDelta accuracy_recording_delays[] = { |
| 1564 base::TimeDelta::FromSeconds(0), base::TimeDelta::FromSeconds(1), |
| 1565 }; |
| 1566 |
| 1567 const struct { |
| 1568 base::TimeDelta rtt; |
| 1569 base::TimeDelta recent_rtt; |
| 1570 } tests[] = { |
| 1571 {base::TimeDelta::FromMilliseconds(expected_rtt_msec), |
| 1572 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, |
| 1573 {base::TimeDelta::FromMilliseconds(expected_rtt_msec + 1), |
| 1574 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, |
| 1575 {base::TimeDelta::FromMilliseconds(expected_rtt_msec - 1), |
| 1576 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, |
| 1577 }; |
| 1578 |
| 1579 for (const auto& accuracy_recording_delay : accuracy_recording_delays) { |
| 1580 for (const auto& test : tests) { |
| 1581 std::unique_ptr<base::SimpleTestTickClock> tick_clock( |
| 1582 new base::SimpleTestTickClock()); |
| 1583 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); |
| 1584 tick_clock_ptr->Advance(base::TimeDelta::FromSeconds(1)); |
| 1585 |
| 1586 std::map<std::string, std::string> variation_params; |
| 1587 TestNetworkQualityEstimator estimator(variation_params); |
| 1588 estimator.SetTickClockForTesting(std::move(tick_clock)); |
| 1589 |
| 1590 std::vector<base::TimeDelta> accuracy_recording_intervals; |
| 1591 accuracy_recording_intervals.push_back(accuracy_recording_delay); |
| 1592 estimator.SetAccuracyRecordingIntervals(accuracy_recording_intervals); |
| 1593 |
| 1594 // RTT is higher than threshold. Network is slow. |
| 1595 // Network was predicted to be slow and actually was slow. |
| 1596 estimator.set_http_rtt(test.rtt); |
| 1597 estimator.set_recent_http_rtt(test.recent_rtt); |
| 1598 estimator.set_transport_rtt(test.rtt); |
| 1599 estimator.set_recent_transport_rtt(test.recent_rtt); |
| 1600 |
| 1601 base::HistogramTester histogram_tester; |
| 1602 |
| 1603 TestDelegate test_delegate; |
| 1604 TestURLRequestContext context(true); |
| 1605 context.set_network_quality_estimator(&estimator); |
| 1606 context.Init(); |
| 1607 |
| 1608 // Start a main-frame request which should cause network quality estimator |
| 1609 // to record accuracy UMA. |
| 1610 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 1611 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1612 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1613 request->Start(); |
| 1614 base::RunLoop().Run(); |
| 1615 |
| 1616 if (accuracy_recording_delay != base::TimeDelta()) { |
| 1617 tick_clock_ptr->Advance(accuracy_recording_delay); |
| 1618 |
| 1619 // Sleep for some time to ensure that the delayed task is posted. |
| 1620 base::PlatformThread::Sleep(accuracy_recording_delay * 2); |
| 1621 base::RunLoop().RunUntilIdle(); |
| 1622 } |
| 1623 |
| 1624 const int diff = std::abs(test.rtt.InMilliseconds() - |
| 1625 test.recent_rtt.InMilliseconds()); |
| 1626 const std::string sign_suffix_with_one_sample = |
| 1627 test.rtt.InMilliseconds() - test.recent_rtt.InMilliseconds() >= 0 |
| 1628 ? "Positive" |
| 1629 : "Negative"; |
| 1630 const std::string sign_suffix_with_zero_samples = |
| 1631 test.rtt.InMilliseconds() - test.recent_rtt.InMilliseconds() >= 0 |
| 1632 ? "Negative" |
| 1633 : "Positive"; |
| 1634 const std::string interval_value = |
| 1635 base::IntToString(accuracy_recording_delay.InSeconds()); |
| 1636 |
| 1637 histogram_tester.ExpectUniqueSample( |
| 1638 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + |
| 1639 sign_suffix_with_one_sample + "." + interval_value + ".60_140", |
| 1640 diff, 1); |
| 1641 histogram_tester.ExpectTotalCount( |
| 1642 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + |
| 1643 sign_suffix_with_zero_samples + "." + interval_value + ".60_140", |
| 1644 0); |
| 1645 histogram_tester.ExpectUniqueSample( |
| 1646 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + |
| 1647 sign_suffix_with_one_sample + "." + interval_value + ".60_140", |
| 1648 diff, 1); |
| 1649 histogram_tester.ExpectTotalCount( |
| 1650 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + |
| 1651 sign_suffix_with_zero_samples + "." + interval_value + ".60_140", |
| 1652 0); |
| 1653 } |
| 1654 } |
| 1655 } |
| 1656 |
| 1463 } // namespace net | 1657 } // namespace net |
| OLD | NEW |