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/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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { | 135 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { |
| 131 if (http_rtt_set_) { | 136 if (http_rtt_set_) { |
| 132 *rtt = http_rtt_; | 137 *rtt = http_rtt_; |
| 133 return true; | 138 return true; |
| 134 } | 139 } |
| 135 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); | 140 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); |
| 136 } | 141 } |
| 137 | 142 |
| 138 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, | 143 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, |
| 139 base::TimeDelta* rtt) const override { | 144 base::TimeDelta* rtt) const override { |
| 140 if (http_rtt_set_) { | 145 if (recent_http_rtt_set_) { |
| 141 *rtt = http_rtt_; | 146 *rtt = recent_http_rtt_; |
| 142 return true; | 147 return true; |
| 143 } | 148 } |
| 144 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt); | 149 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt); |
| 145 } | 150 } |
| 146 | 151 |
| 147 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) { | 152 void set_recent_http_rtt(const base::TimeDelta& recent_http_rtt) { |
| 148 downlink_throughput_kbps_set_ = true; | 153 recent_http_rtt_set_ = true; |
| 149 downlink_throughput_kbps_ = downlink_throughput_kbps; | 154 recent_http_rtt_ = recent_http_rtt; |
| 150 } | 155 } |
| 151 | 156 |
| 152 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override { | 157 void set_transport_rtt(const base::TimeDelta& transport_rtt) { |
| 153 if (downlink_throughput_kbps_set_) { | 158 transport_rtt_set_ = true; |
| 154 *kbps = downlink_throughput_kbps_; | 159 transport_rtt_ = transport_rtt; |
| 155 return true; | |
| 156 } | 160 } |
| 157 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); | |
| 158 } | |
| 159 | 161 |
| 160 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time, | 162 void set_recent_transport_rtt(const base::TimeDelta& recent_transport_rtt) { |
| 161 int32_t* kbps) const override { | 163 recent_transport_rtt_set_ = true; |
| 162 if (downlink_throughput_kbps_set_) { | 164 recent_transport_rtt_ = recent_transport_rtt; |
| 163 *kbps = downlink_throughput_kbps_; | |
| 164 return true; | |
| 165 } | 165 } |
| 166 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( | |
| 167 start_time, kbps); | |
| 168 } | |
| 169 | 166 |
| 170 using NetworkQualityEstimator::SetTickClockForTesting; | 167 bool GetTransportRTTEstimate(base::TimeDelta* rtt) const override { |
|
bengr
2016/06/01 16:55:30
Add comments saying what you're overriding.
tbansal1
2016/06/01 21:49:02
Done.
| |
| 171 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 168 if (transport_rtt_set_) { |
| 172 using NetworkQualityEstimator::OnConnectionTypeChanged; | 169 *rtt = transport_rtt_; |
| 170 return true; | |
| 171 } | |
| 172 return NetworkQualityEstimator::GetTransportRTTEstimate(rtt); | |
| 173 } | |
| 173 | 174 |
| 174 private: | 175 bool GetRecentTransportRTTMedian(const base::TimeTicks& start_time, |
| 175 // NetworkQualityEstimator implementation that returns the overridden network | 176 base::TimeDelta* rtt) const override { |
| 176 // id (instead of invoking platform APIs). | 177 if (recent_transport_rtt_set_) { |
| 177 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 178 *rtt = recent_transport_rtt_; |
| 178 return NetworkQualityEstimator::NetworkID(current_network_type_, | 179 return true; |
| 179 current_network_id_); | 180 } |
| 180 } | 181 return NetworkQualityEstimator::GetRecentTransportRTTMedian(start_time, |
| 182 rtt); | |
| 183 } | |
| 181 | 184 |
| 182 bool effective_connection_type_set_; | 185 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) { |
| 183 EffectiveConnectionType effective_connection_type_; | 186 downlink_throughput_kbps_set_ = true; |
| 187 downlink_throughput_kbps_ = downlink_throughput_kbps; | |
| 188 } | |
| 184 | 189 |
| 185 NetworkChangeNotifier::ConnectionType current_network_type_; | 190 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override { |
| 186 std::string current_network_id_; | 191 if (downlink_throughput_kbps_set_) { |
| 192 *kbps = downlink_throughput_kbps_; | |
| 193 return true; | |
| 194 } | |
| 195 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); | |
| 196 } | |
| 187 | 197 |
| 188 bool http_rtt_set_; | 198 bool GetRecentMedianDownlinkThroughputKbps( |
| 189 base::TimeDelta http_rtt_; | 199 const base::TimeTicks& start_time, |
| 200 int32_t* kbps) const override { | |
| 201 if (downlink_throughput_kbps_set_) { | |
| 202 *kbps = downlink_throughput_kbps_; | |
| 203 return true; | |
| 204 } | |
| 205 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( | |
| 206 start_time, kbps); | |
| 207 } | |
| 190 | 208 |
| 191 bool downlink_throughput_kbps_set_; | 209 void SetAccuracyRecordingIntervals( |
| 192 int32_t downlink_throughput_kbps_; | 210 const std::vector<base::TimeDelta>& accuracy_recording_intervals) { |
| 211 accuracy_recording_intervals_set_ = true; | |
| 212 accuracy_recording_intervals_ = accuracy_recording_intervals; | |
| 213 } | |
| 193 | 214 |
| 194 // Embedded server used for testing. | 215 const std::vector<base::TimeDelta>& GetAccuracyRecordingIntervals() |
| 195 EmbeddedTestServer embedded_test_server_; | 216 const override { |
| 217 if (accuracy_recording_intervals_set_) | |
| 218 return accuracy_recording_intervals_; | |
| 196 | 219 |
| 197 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | 220 return NetworkQualityEstimator::GetAccuracyRecordingIntervals(); |
| 221 } | |
| 222 | |
| 223 using NetworkQualityEstimator::SetTickClockForTesting; | |
| 224 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | |
| 225 using NetworkQualityEstimator::OnConnectionTypeChanged; | |
| 226 | |
| 227 private: | |
| 228 // NetworkQualityEstimator implementation that returns the overridden | |
| 229 // network | |
| 230 // id (instead of invoking platform APIs). | |
| 231 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | |
| 232 return NetworkQualityEstimator::NetworkID(current_network_type_, | |
| 233 current_network_id_); | |
| 234 } | |
| 235 | |
| 236 bool effective_connection_type_set_; | |
| 237 EffectiveConnectionType effective_connection_type_; | |
| 238 | |
| 239 NetworkChangeNotifier::ConnectionType current_network_type_; | |
| 240 std::string current_network_id_; | |
| 241 | |
| 242 bool accuracy_recording_intervals_set_; | |
| 243 std::vector<base::TimeDelta> accuracy_recording_intervals_; | |
| 244 | |
| 245 bool http_rtt_set_; | |
| 246 base::TimeDelta http_rtt_; | |
| 247 | |
| 248 bool recent_http_rtt_set_; | |
| 249 base::TimeDelta recent_http_rtt_; | |
| 250 | |
| 251 bool transport_rtt_set_; | |
| 252 base::TimeDelta transport_rtt_; | |
| 253 | |
| 254 bool recent_transport_rtt_set_; | |
| 255 base::TimeDelta recent_transport_rtt_; | |
| 256 | |
| 257 bool downlink_throughput_kbps_set_; | |
| 258 int32_t downlink_throughput_kbps_; | |
| 259 | |
| 260 // Embedded server used for testing. | |
| 261 EmbeddedTestServer embedded_test_server_; | |
| 262 | |
| 263 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | |
| 198 }; | 264 }; |
| 199 | 265 |
| 200 class TestEffectiveConnectionTypeObserver | 266 class TestEffectiveConnectionTypeObserver |
| 201 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver { | 267 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver { |
| 202 public: | 268 public: |
| 203 std::vector<NetworkQualityEstimator::EffectiveConnectionType>& | 269 std::vector<NetworkQualityEstimator::EffectiveConnectionType>& |
| 204 effective_connection_types() { | 270 effective_connection_types() { |
| 205 return effective_connection_types_; | 271 return effective_connection_types_; |
| 206 } | 272 } |
| 207 | 273 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 const struct { | 572 const struct { |
| 507 int32_t rtt_msec; | 573 int32_t rtt_msec; |
| 508 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; | 574 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; |
| 509 } tests[] = { | 575 } tests[] = { |
| 510 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 576 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 511 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 577 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 512 }; | 578 }; |
| 513 | 579 |
| 514 for (const auto& test : tests) { | 580 for (const auto& test : tests) { |
| 515 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 581 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 582 estimator.set_recent_http_rtt( | |
| 583 base::TimeDelta::FromMilliseconds(test.rtt_msec)); | |
| 516 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 584 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 517 } | 585 } |
| 518 } | 586 } |
| 519 | 587 |
| 520 // Tests that |GetEffectiveConnectionType| returns | 588 // Tests that |GetEffectiveConnectionType| returns |
| 521 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. | 589 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. |
| 522 TEST(NetworkQualityEstimatorTest, Offline) { | 590 TEST(NetworkQualityEstimatorTest, Offline) { |
| 523 std::map<std::string, std::string> variation_params; | 591 std::map<std::string, std::string> variation_params; |
| 524 TestNetworkQualityEstimator estimator(variation_params); | 592 TestNetworkQualityEstimator estimator(variation_params); |
| 525 | 593 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 575 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 643 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 576 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, | 644 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, |
| 577 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, | 645 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, |
| 578 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 646 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 579 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 647 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 580 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, | 648 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, |
| 581 }; | 649 }; |
| 582 | 650 |
| 583 for (const auto& test : tests) { | 651 for (const auto& test : tests) { |
| 584 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 652 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 653 estimator.set_recent_http_rtt( | |
| 654 base::TimeDelta::FromMilliseconds(test.rtt_msec)); | |
| 585 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 655 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 586 } | 656 } |
| 587 } | 657 } |
| 588 | 658 |
| 589 // Tests that |GetEffectiveConnectionType| returns correct connection type when | 659 // Tests that |GetEffectiveConnectionType| returns correct connection type when |
| 590 // both RTT and throughput thresholds are specified in the variation params. | 660 // both RTT and throughput thresholds are specified in the variation params. |
| 591 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) { | 661 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) { |
| 592 std::map<std::string, std::string> variation_params; | 662 std::map<std::string, std::string> variation_params; |
| 593 | 663 |
| 594 variation_params["Offline.ThresholdMedianHttpRTTMsec"] = "4000"; | 664 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. | 704 // Set both RTT and throughput. RTT is the bottleneck. |
| 635 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 705 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 636 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 706 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 637 // Set throughput to an invalid value. | 707 // Set throughput to an invalid value. |
| 638 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, | 708 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, |
| 639 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, | 709 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, |
| 640 }; | 710 }; |
| 641 | 711 |
| 642 for (const auto& test : tests) { | 712 for (const auto& test : tests) { |
| 643 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); | 713 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); |
| 714 estimator.set_recent_http_rtt( | |
| 715 base::TimeDelta::FromMilliseconds(test.rtt_msec)); | |
| 644 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); | 716 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); |
| 645 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); | 717 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); |
| 646 } | 718 } |
| 647 } | 719 } |
| 648 | 720 |
| 649 // Tests if |weight_multiplier_per_second_| is set to correct value for various | 721 // Tests if |weight_multiplier_per_second_| is set to correct value for various |
| 650 // values of half life parameter. | 722 // values of half life parameter. |
| 651 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 723 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 652 std::map<std::string, std::string> variation_params; | 724 std::map<std::string, std::string> variation_params; |
| 653 | 725 |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1453 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); | 1525 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); |
| 1454 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); | 1526 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); |
| 1455 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", | 1527 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", |
| 1456 1); | 1528 1); |
| 1457 | 1529 |
| 1458 // Verify that metrics are logged correctly on main-frame requests. | 1530 // Verify that metrics are logged correctly on main-frame requests. |
| 1459 histogram_tester.ExpectTotalCount( | 1531 histogram_tester.ExpectTotalCount( |
| 1460 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); | 1532 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); |
| 1461 } | 1533 } |
| 1462 | 1534 |
| 1535 // Tests if the NQE accuracy metrics are recorded properly. | |
| 1536 TEST(NetworkQualityEstimatorTest, RecordAccuracyNonZeroDelay) { | |
| 1537 const int expected_rtt_msec = 100; | |
| 1538 | |
| 1539 const base::TimeDelta accuracy_recording_delays[] = { | |
| 1540 base::TimeDelta::FromSeconds(0), base::TimeDelta::FromSeconds(1), | |
| 1541 }; | |
| 1542 | |
| 1543 const struct { | |
| 1544 base::TimeDelta rtt; | |
| 1545 base::TimeDelta recent_rtt; | |
| 1546 } tests[] = { | |
| 1547 {base::TimeDelta::FromMilliseconds(expected_rtt_msec), | |
| 1548 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, | |
| 1549 {base::TimeDelta::FromMilliseconds(expected_rtt_msec + 1), | |
| 1550 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, | |
| 1551 {base::TimeDelta::FromMilliseconds(expected_rtt_msec - 1), | |
| 1552 base::TimeDelta::FromMilliseconds(expected_rtt_msec)}, | |
| 1553 }; | |
| 1554 | |
| 1555 for (const auto& accuracy_recording_delay : accuracy_recording_delays) { | |
| 1556 for (const auto& test : tests) { | |
| 1557 std::unique_ptr<base::SimpleTestTickClock> tick_clock( | |
| 1558 new base::SimpleTestTickClock()); | |
| 1559 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); | |
| 1560 tick_clock_ptr->Advance(base::TimeDelta::FromSeconds(1)); | |
| 1561 | |
| 1562 std::map<std::string, std::string> variation_params; | |
| 1563 TestNetworkQualityEstimator estimator(variation_params); | |
| 1564 estimator.SetTickClockForTesting(std::move(tick_clock)); | |
| 1565 | |
| 1566 std::vector<base::TimeDelta> accuracy_recording_intervals; | |
| 1567 accuracy_recording_intervals.push_back(accuracy_recording_delay); | |
| 1568 estimator.SetAccuracyRecordingIntervals(accuracy_recording_intervals); | |
| 1569 | |
| 1570 // RTT is higher than threshold. Network is slow. | |
| 1571 // Network was predicted to be slow and actually was slow. | |
| 1572 estimator.set_http_rtt(test.rtt); | |
| 1573 estimator.set_recent_http_rtt(test.recent_rtt); | |
| 1574 estimator.set_transport_rtt(test.rtt); | |
| 1575 estimator.set_recent_transport_rtt(test.recent_rtt); | |
| 1576 | |
| 1577 base::HistogramTester histogram_tester; | |
| 1578 | |
| 1579 TestDelegate test_delegate; | |
| 1580 TestURLRequestContext context(true); | |
| 1581 context.set_network_quality_estimator(&estimator); | |
| 1582 context.Init(); | |
| 1583 | |
| 1584 // Start a main-frame request which should cause network quality estimator | |
| 1585 // to record accuracy UMA. | |
| 1586 std::unique_ptr<URLRequest> request(context.CreateRequest( | |
| 1587 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | |
| 1588 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | |
| 1589 request->Start(); | |
| 1590 base::RunLoop().Run(); | |
| 1591 | |
| 1592 if (accuracy_recording_delay != base::TimeDelta()) { | |
| 1593 tick_clock_ptr->Advance(accuracy_recording_delay); | |
| 1594 | |
| 1595 // Sleep to ensure that the delayed task is posted. | |
| 1596 base::PlatformThread::Sleep(accuracy_recording_delay); | |
| 1597 base::RunLoop().RunUntilIdle(); | |
| 1598 } | |
| 1599 | |
| 1600 const int diff = std::abs(test.rtt.InMilliseconds() - | |
| 1601 test.recent_rtt.InMilliseconds()); | |
| 1602 const std::string diff_value = | |
| 1603 base::IntToString(accuracy_recording_delay.InSeconds()); | |
| 1604 | |
| 1605 if (test.rtt >= test.recent_rtt) { | |
| 1606 histogram_tester.ExpectUniqueSample( | |
| 1607 "NQE.Accuracy.HttpRTT.EstimatedActualDiff." + diff_value + | |
| 1608 ".60_140", | |
| 1609 diff, 1); | |
| 1610 histogram_tester.ExpectTotalCount( | |
| 1611 "NQE.Accuracy.HttpRTT.ActualEstimatedDiff." + diff_value + | |
| 1612 ".60_140", | |
| 1613 0); | |
| 1614 histogram_tester.ExpectUniqueSample( | |
| 1615 "NQE.Accuracy.TransportRTT.EstimatedActualDiff." + diff_value + | |
| 1616 ".60_140", | |
| 1617 diff, 1); | |
| 1618 histogram_tester.ExpectTotalCount( | |
| 1619 "NQE.Accuracy.TransportRTT.ActualEstimatedDiff." + diff_value + | |
| 1620 ".60_140", | |
| 1621 0); | |
| 1622 } else { | |
| 1623 histogram_tester.ExpectTotalCount( | |
| 1624 "NQE.Accuracy.HttpRTT.EstimatedActualDiff." + diff_value + | |
| 1625 ".60_140", | |
| 1626 0); | |
| 1627 histogram_tester.ExpectUniqueSample( | |
| 1628 "NQE.Accuracy.HttpRTT.ActualEstimatedDiff." + diff_value + | |
| 1629 ".60_140", | |
| 1630 diff, 1); | |
| 1631 histogram_tester.ExpectTotalCount( | |
| 1632 "NQE.Accuracy.TransportRTT.EstimatedActualDiff." + diff_value + | |
| 1633 ".60_140", | |
| 1634 0); | |
| 1635 histogram_tester.ExpectUniqueSample( | |
| 1636 "NQE.Accuracy.TransportRTT.ActualEstimatedDiff." + diff_value + | |
| 1637 ".60_140", | |
| 1638 diff, 1); | |
| 1639 } | |
| 1640 } | |
| 1641 } | |
| 1642 } | |
| 1643 | |
| 1463 } // namespace net | 1644 } // namespace net |
| OLD | NEW |