| 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/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 20 #include "base/metrics/histogram_samples.h" | 20 #include "base/metrics/histogram_samples.h" |
| 21 #include "base/optional.h" |
| 21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 22 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/test/histogram_tester.h" | 24 #include "base/test/histogram_tester.h" |
| 24 #include "base/test/simple_test_tick_clock.h" | 25 #include "base/test/simple_test_tick_clock.h" |
| 25 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 28 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
| 29 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
| 30 #include "net/http/http_response_headers.h" | 31 #include "net/http/http_response_headers.h" |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile100.Unknown", 1); | 294 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile100.Unknown", 1); |
| 294 | 295 |
| 295 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 0); | 296 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 0); |
| 296 | 297 |
| 297 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 298 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 298 EXPECT_FALSE( | 299 EXPECT_FALSE( |
| 299 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 300 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 300 | 301 |
| 301 // Verify that metrics are logged correctly on main-frame requests. | 302 // Verify that metrics are logged correctly on main-frame requests. |
| 302 histogram_tester.ExpectTotalCount("NQE.MainFrame.RTT.Percentile50", 1); | 303 histogram_tester.ExpectTotalCount("NQE.MainFrame.RTT.Percentile50", 1); |
| 304 histogram_tester.ExpectTotalCount("NQE.WeightedAverage.MainFrame.RTT", 1); |
| 305 histogram_tester.ExpectTotalCount("NQE.UnweightedAverage.MainFrame.RTT", 1); |
| 303 histogram_tester.ExpectTotalCount("NQE.MainFrame.RTT.Percentile50.Unknown", | 306 histogram_tester.ExpectTotalCount("NQE.MainFrame.RTT.Percentile50.Unknown", |
| 304 1); | 307 1); |
| 305 histogram_tester.ExpectTotalCount("NQE.MainFrame.TransportRTT.Percentile50", | 308 histogram_tester.ExpectTotalCount("NQE.MainFrame.TransportRTT.Percentile50", |
| 306 0); | 309 0); |
| 307 histogram_tester.ExpectTotalCount( | 310 histogram_tester.ExpectTotalCount( |
| 308 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", 0); | 311 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", 0); |
| 309 histogram_tester.ExpectTotalCount("NQE.MainFrame.Kbps.Percentile50", 1); | 312 histogram_tester.ExpectTotalCount("NQE.MainFrame.Kbps.Percentile50", 1); |
| 310 histogram_tester.ExpectTotalCount("NQE.MainFrame.Kbps.Percentile50.Unknown", | 313 histogram_tester.ExpectTotalCount("NQE.MainFrame.Kbps.Percentile50.Unknown", |
| 311 1); | 314 1); |
| 312 | 315 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 int32_t kbps; | 542 int32_t kbps; |
| 540 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 543 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 541 EXPECT_FALSE( | 544 EXPECT_FALSE( |
| 542 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 545 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 543 | 546 |
| 544 TestDelegate test_delegate; | 547 TestDelegate test_delegate; |
| 545 TestURLRequestContext context(true); | 548 TestURLRequestContext context(true); |
| 546 context.set_network_quality_estimator(&estimator); | 549 context.set_network_quality_estimator(&estimator); |
| 547 context.Init(); | 550 context.Init(); |
| 548 | 551 |
| 549 // Push more observations than the maximum buffer size. | 552 const size_t kMaxObservations = 10; |
| 550 const size_t kMaxObservations = 1000; | |
| 551 for (size_t i = 0; i < kMaxObservations; ++i) { | 553 for (size_t i = 0; i < kMaxObservations; ++i) { |
| 552 std::unique_ptr<URLRequest> request(context.CreateRequest( | 554 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 553 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 555 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 554 request->Start(); | 556 request->Start(); |
| 555 base::RunLoop().Run(); | 557 base::RunLoop().Run(); |
| 556 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 558 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 557 EXPECT_TRUE( | 559 EXPECT_TRUE( |
| 558 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 560 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 559 } | 561 } |
| 560 | 562 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 572 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 574 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { |
| 573 TestNetworkQualityEstimator estimator; | 575 TestNetworkQualityEstimator estimator; |
| 574 | 576 |
| 575 std::vector<NetworkQualityObservationSource> disallowed_observation_sources; | 577 std::vector<NetworkQualityObservationSource> disallowed_observation_sources; |
| 576 disallowed_observation_sources.push_back( | 578 disallowed_observation_sources.push_back( |
| 577 NETWORK_QUALITY_OBSERVATION_SOURCE_TCP); | 579 NETWORK_QUALITY_OBSERVATION_SOURCE_TCP); |
| 578 disallowed_observation_sources.push_back( | 580 disallowed_observation_sources.push_back( |
| 579 NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC); | 581 NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC); |
| 580 | 582 |
| 581 EXPECT_EQ(nqe::internal::InvalidRTT(), | 583 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 582 estimator.GetRTTEstimateInternal(disallowed_observation_sources, | 584 estimator.GetRTTEstimateInternal( |
| 583 base::TimeTicks(), 100)); | 585 disallowed_observation_sources, base::TimeTicks(), |
| 586 base::Optional<NetworkQualityEstimator::Statistic>(), 100)); |
| 584 EXPECT_EQ(nqe::internal::kInvalidThroughput, | 587 EXPECT_EQ(nqe::internal::kInvalidThroughput, |
| 585 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 588 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 586 base::TimeTicks(), 100)); | 589 base::TimeTicks(), 100)); |
| 587 | 590 |
| 588 TestDelegate test_delegate; | 591 TestDelegate test_delegate; |
| 589 TestURLRequestContext context(true); | 592 TestURLRequestContext context(true); |
| 590 context.set_network_quality_estimator(&estimator); | 593 context.set_network_quality_estimator(&estimator); |
| 591 context.Init(); | 594 context.Init(); |
| 592 | 595 |
| 593 // Number of observations are more than the maximum buffer size. | 596 for (size_t i = 0; i < 10U; ++i) { |
| 594 for (size_t i = 0; i < 1000U; ++i) { | |
| 595 std::unique_ptr<URLRequest> request(context.CreateRequest( | 597 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 596 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 598 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 597 request->Start(); | 599 request->Start(); |
| 598 base::RunLoop().Run(); | 600 base::RunLoop().Run(); |
| 599 } | 601 } |
| 600 | 602 |
| 601 // Verify the percentiles through simple tests. | 603 // Verify the percentiles through simple tests. |
| 602 for (int i = 0; i <= 100; ++i) { | 604 for (int i = 0; i <= 100; ++i) { |
| 603 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 605 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 604 base::TimeTicks(), i), | 606 base::TimeTicks(), i), |
| 605 0); | 607 0); |
| 606 EXPECT_LT(estimator.GetRTTEstimateInternal(disallowed_observation_sources, | 608 EXPECT_LT(estimator.GetRTTEstimateInternal( |
| 607 base::TimeTicks(), i), | 609 disallowed_observation_sources, base::TimeTicks(), |
| 610 base::Optional<NetworkQualityEstimator::Statistic>(), i), |
| 608 base::TimeDelta::Max()); | 611 base::TimeDelta::Max()); |
| 609 | 612 |
| 610 if (i != 0) { | 613 if (i != 0) { |
| 611 // Throughput percentiles are in decreasing order. | 614 // Throughput percentiles are in decreasing order. |
| 612 EXPECT_LE(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 615 EXPECT_LE(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 613 base::TimeTicks(), i), | 616 base::TimeTicks(), i), |
| 614 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 617 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 615 base::TimeTicks(), i - 1)); | 618 base::TimeTicks(), i - 1)); |
| 616 | 619 |
| 620 // Weighted average statistic should be computed correctly. |
| 621 EXPECT_NE(nqe::internal::InvalidRTT(), |
| 622 estimator.GetRTTEstimateInternal( |
| 623 disallowed_observation_sources, base::TimeTicks(), |
| 624 NetworkQualityEstimator::STATISTIC_WEIGHTED_AVERAGE, i)); |
| 625 |
| 626 // Weighted average statistic should disregard the value of the percentile |
| 627 // argument. |
| 628 EXPECT_EQ( |
| 629 estimator.GetRTTEstimateInternal( |
| 630 disallowed_observation_sources, base::TimeTicks(), |
| 631 NetworkQualityEstimator::STATISTIC_WEIGHTED_AVERAGE, i), |
| 632 estimator.GetRTTEstimateInternal( |
| 633 disallowed_observation_sources, base::TimeTicks(), |
| 634 NetworkQualityEstimator::STATISTIC_WEIGHTED_AVERAGE, i - 1)); |
| 635 |
| 617 // RTT percentiles are in increasing order. | 636 // RTT percentiles are in increasing order. |
| 618 EXPECT_GE(estimator.GetRTTEstimateInternal(disallowed_observation_sources, | 637 EXPECT_GE( |
| 619 base::TimeTicks(), i), | 638 estimator.GetRTTEstimateInternal( |
| 620 estimator.GetRTTEstimateInternal(disallowed_observation_sources, | 639 disallowed_observation_sources, base::TimeTicks(), |
| 621 base::TimeTicks(), i - 1)); | 640 base::Optional<NetworkQualityEstimator::Statistic>(), i), |
| 641 estimator.GetRTTEstimateInternal( |
| 642 disallowed_observation_sources, base::TimeTicks(), |
| 643 base::Optional<NetworkQualityEstimator::Statistic>(), i - 1)); |
| 622 } | 644 } |
| 623 } | 645 } |
| 624 } | 646 } |
| 625 | 647 |
| 626 // Verifies that the observers receive the notifications when default estimates | 648 // Verifies that the observers receive the notifications when default estimates |
| 627 // are added to the observations. | 649 // are added to the observations. |
| 628 TEST(NetworkQualityEstimatorTest, DefaultObservations) { | 650 TEST(NetworkQualityEstimatorTest, DefaultObservations) { |
| 629 base::HistogramTester histogram_tester; | 651 base::HistogramTester histogram_tester; |
| 630 | 652 |
| 631 TestEffectiveConnectionTypeObserver effective_connection_type_observer; | 653 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| (...skipping 1660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2292 tick_clock_ptr->Advance(base::TimeDelta::FromSeconds(1)); | 2314 tick_clock_ptr->Advance(base::TimeDelta::FromSeconds(1)); |
| 2293 | 2315 |
| 2294 std::vector<base::TimeDelta> accuracy_recording_intervals; | 2316 std::vector<base::TimeDelta> accuracy_recording_intervals; |
| 2295 accuracy_recording_intervals.push_back(accuracy_recording_delay); | 2317 accuracy_recording_intervals.push_back(accuracy_recording_delay); |
| 2296 estimator.SetAccuracyRecordingIntervals(accuracy_recording_intervals); | 2318 estimator.SetAccuracyRecordingIntervals(accuracy_recording_intervals); |
| 2297 | 2319 |
| 2298 // RTT is higher than threshold. Network is slow. | 2320 // RTT is higher than threshold. Network is slow. |
| 2299 // Network was predicted to be slow and actually was slow. | 2321 // Network was predicted to be slow and actually was slow. |
| 2300 estimator.set_start_time_null_http_rtt(test.rtt); | 2322 estimator.set_start_time_null_http_rtt(test.rtt); |
| 2301 estimator.set_recent_http_rtt(test.recent_rtt); | 2323 estimator.set_recent_http_rtt(test.recent_rtt); |
| 2324 estimator.set_rtt_estimate_internal(test.recent_rtt); |
| 2302 estimator.set_start_time_null_transport_rtt(test.rtt); | 2325 estimator.set_start_time_null_transport_rtt(test.rtt); |
| 2303 estimator.set_recent_transport_rtt(test.recent_rtt); | 2326 estimator.set_recent_transport_rtt(test.recent_rtt); |
| 2304 estimator.set_start_time_null_downlink_throughput_kbps( | 2327 estimator.set_start_time_null_downlink_throughput_kbps( |
| 2305 test.downstream_throughput_kbps); | 2328 test.downstream_throughput_kbps); |
| 2306 estimator.set_recent_downlink_throughput_kbps( | 2329 estimator.set_recent_downlink_throughput_kbps( |
| 2307 test.recent_downstream_throughput_kbps); | 2330 test.recent_downstream_throughput_kbps); |
| 2308 | 2331 |
| 2309 base::HistogramTester histogram_tester; | 2332 base::HistogramTester histogram_tester; |
| 2310 | 2333 |
| 2311 TestDelegate test_delegate; | 2334 TestDelegate test_delegate; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2387 histogram_tester.ExpectUniqueSample( | 2410 histogram_tester.ExpectUniqueSample( |
| 2388 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + | 2411 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + |
| 2389 rtt_sign_suffix_with_one_sample + "." + interval_value + | 2412 rtt_sign_suffix_with_one_sample + "." + interval_value + |
| 2390 ".300_620", | 2413 ".300_620", |
| 2391 rtt_diff, 1); | 2414 rtt_diff, 1); |
| 2392 histogram_tester.ExpectTotalCount( | 2415 histogram_tester.ExpectTotalCount( |
| 2393 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + | 2416 "NQE.Accuracy.HttpRTT.EstimatedObservedDiff." + |
| 2394 rtt_sign_suffix_with_zero_samples + "." + interval_value + | 2417 rtt_sign_suffix_with_zero_samples + "." + interval_value + |
| 2395 ".300_620", | 2418 ".300_620", |
| 2396 0); | 2419 0); |
| 2420 |
| 2421 // All samples are recorded in bucket 0 because recent HTTP RTT and |
| 2422 // HTTP RTT are equal when weighted or unweighted average algorithms are |
| 2423 // used. |
| 2424 histogram_tester.ExpectUniqueSample( |
| 2425 "NQE.WeightedAverage.Accuracy.HttpRTT.EstimatedObservedDiff." |
| 2426 "Positive." + |
| 2427 interval_value + ".300_620", |
| 2428 0, 1); |
| 2429 histogram_tester.ExpectUniqueSample( |
| 2430 "NQE.UnweightedAverage.Accuracy.HttpRTT.EstimatedObservedDiff." |
| 2431 "Positive." + |
| 2432 interval_value + ".300_620", |
| 2433 0, 1); |
| 2434 |
| 2397 histogram_tester.ExpectUniqueSample( | 2435 histogram_tester.ExpectUniqueSample( |
| 2398 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + | 2436 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + |
| 2399 rtt_sign_suffix_with_one_sample + "." + interval_value + | 2437 rtt_sign_suffix_with_one_sample + "." + interval_value + |
| 2400 ".300_620", | 2438 ".300_620", |
| 2401 rtt_diff, 1); | 2439 rtt_diff, 1); |
| 2402 histogram_tester.ExpectTotalCount( | 2440 histogram_tester.ExpectTotalCount( |
| 2403 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + | 2441 "NQE.Accuracy.TransportRTT.EstimatedObservedDiff." + |
| 2404 rtt_sign_suffix_with_zero_samples + "." + interval_value + | 2442 rtt_sign_suffix_with_zero_samples + "." + interval_value + |
| 2405 ".300_620", | 2443 ".300_620", |
| 2406 0); | 2444 0); |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3076 | 3114 |
| 3077 // Cleanup. | 3115 // Cleanup. |
| 3078 estimator.RemoveRTTObserver(&rtt_observer); | 3116 estimator.RemoveRTTObserver(&rtt_observer); |
| 3079 estimator.RemoveThroughputObserver(&throughput_observer); | 3117 estimator.RemoveThroughputObserver(&throughput_observer); |
| 3080 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); | 3118 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 3081 estimator.RemoveEffectiveConnectionTypeObserver( | 3119 estimator.RemoveEffectiveConnectionTypeObserver( |
| 3082 &effective_connection_type_observer); | 3120 &effective_connection_type_observer); |
| 3083 } | 3121 } |
| 3084 | 3122 |
| 3085 } // namespace net | 3123 } // namespace net |
| OLD | NEW |