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/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/metrics/histogram_samples.h" | 20 #include "base/metrics/histogram_samples.h" |
| 20 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 23 #include "base/test/simple_test_tick_clock.h" | 24 #include "base/test/simple_test_tick_clock.h" |
| 24 #include "base/threading/platform_thread.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_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 30 #include "net/http/http_response_info.h" | 31 #include "net/http/http_response_info.h" |
| 31 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 33 #include "net/log/test_net_log.h" | |
| 32 #include "net/nqe/effective_connection_type.h" | 34 #include "net/nqe/effective_connection_type.h" |
| 33 #include "net/nqe/external_estimate_provider.h" | 35 #include "net/nqe/external_estimate_provider.h" |
| 34 #include "net/nqe/network_quality_estimator_test_util.h" | 36 #include "net/nqe/network_quality_estimator_test_util.h" |
| 35 #include "net/nqe/network_quality_observation.h" | 37 #include "net/nqe/network_quality_observation.h" |
| 36 #include "net/nqe/network_quality_observation_source.h" | 38 #include "net/nqe/network_quality_observation_source.h" |
| 37 #include "net/nqe/observation_buffer.h" | 39 #include "net/nqe/observation_buffer.h" |
| 38 #include "net/socket/socket_performance_watcher.h" | 40 #include "net/socket/socket_performance_watcher.h" |
| 39 #include "net/socket/socket_performance_watcher_factory.h" | 41 #include "net/socket/socket_performance_watcher_factory.h" |
| 40 #include "net/url_request/url_request.h" | 42 #include "net/url_request/url_request.h" |
| 41 #include "net/url_request/url_request_test_util.h" | 43 #include "net/url_request/url_request_test_util.h" |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 2); | 291 2); |
| 290 histogram_tester.ExpectBucketCount("NQE.EstimateAvailable.MainFrame.Kbps", 1, | 292 histogram_tester.ExpectBucketCount("NQE.EstimateAvailable.MainFrame.Kbps", 1, |
| 291 1); | 293 1); |
| 292 | 294 |
| 293 estimator.SimulateNetworkChange( | 295 estimator.SimulateNetworkChange( |
| 294 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); | 296 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); |
| 295 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", false, | 297 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", false, |
| 296 3); | 298 3); |
| 297 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, | 299 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, |
| 298 1); | 300 1); |
| 301 | |
| 302 EXPECT_LT( | |
| 303 0, estimator.GetEntriesCount(NetLogEventType::NETWORK_QUALITY_CHANGED)); | |
| 299 } | 304 } |
| 300 | 305 |
| 301 // Tests that the network quality estimator writes and reads network quality | 306 // Tests that the network quality estimator writes and reads network quality |
| 302 // from the cache store correctly. | 307 // from the cache store correctly. |
| 303 TEST(NetworkQualityEstimatorTest, Caching) { | 308 TEST(NetworkQualityEstimatorTest, Caching) { |
| 304 base::HistogramTester histogram_tester; | 309 base::HistogramTester histogram_tester; |
| 305 TestNetworkQualityEstimator estimator; | 310 TestNetworkQualityEstimator estimator; |
| 306 | 311 |
| 307 estimator.SimulateNetworkChange( | 312 estimator.SimulateNetworkChange( |
| 308 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); | 313 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 // Verifies that the observers receive the notifications when default estimates | 472 // Verifies that the observers receive the notifications when default estimates |
| 468 // are added to the observations. | 473 // are added to the observations. |
| 469 TEST(NetworkQualityEstimatorTest, DefaultObservations) { | 474 TEST(NetworkQualityEstimatorTest, DefaultObservations) { |
| 470 TestEffectiveConnectionTypeObserver effective_connection_type_observer; | 475 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| 471 TestRTTAndThroughputEstimatesObserver rtt_throughput_estimates_observer; | 476 TestRTTAndThroughputEstimatesObserver rtt_throughput_estimates_observer; |
| 472 TestRTTObserver rtt_observer; | 477 TestRTTObserver rtt_observer; |
| 473 TestThroughputObserver throughput_observer; | 478 TestThroughputObserver throughput_observer; |
| 474 std::map<std::string, std::string> variation_params; | 479 std::map<std::string, std::string> variation_params; |
| 475 TestNetworkQualityEstimator estimator( | 480 TestNetworkQualityEstimator estimator( |
| 476 nullptr, variation_params, false, false, | 481 nullptr, variation_params, false, false, |
| 477 true /* add_default_platform_observations */); | 482 true /* add_default_platform_observations */, |
| 483 base::MakeUnique<BoundTestNetLog>()); | |
| 478 base::TimeDelta rtt; | 484 base::TimeDelta rtt; |
| 479 int32_t kbps; | 485 int32_t kbps; |
| 480 | 486 |
| 481 // Default estimates should be available. | 487 // Default estimates should be available. |
| 482 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 488 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 483 EXPECT_EQ(base::TimeDelta::FromMilliseconds(115), rtt); | 489 EXPECT_EQ(base::TimeDelta::FromMilliseconds(115), rtt); |
| 484 EXPECT_TRUE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); | 490 EXPECT_TRUE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); |
| 485 EXPECT_EQ(base::TimeDelta::FromMilliseconds(55), rtt); | 491 EXPECT_EQ(base::TimeDelta::FromMilliseconds(55), rtt); |
| 486 EXPECT_TRUE( | 492 EXPECT_TRUE( |
| 487 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 493 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 507 EXPECT_EQ(749, kbps); | 513 EXPECT_EQ(749, kbps); |
| 508 | 514 |
| 509 EXPECT_NE(EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | 515 EXPECT_NE(EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 510 estimator.GetEffectiveConnectionType()); | 516 estimator.GetEffectiveConnectionType()); |
| 511 EXPECT_EQ( | 517 EXPECT_EQ( |
| 512 1U, | 518 1U, |
| 513 effective_connection_type_observer.effective_connection_types().size()); | 519 effective_connection_type_observer.effective_connection_types().size()); |
| 514 EXPECT_NE( | 520 EXPECT_NE( |
| 515 EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | 521 EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 516 effective_connection_type_observer.effective_connection_types().front()); | 522 effective_connection_type_observer.effective_connection_types().front()); |
| 523 EXPECT_LT( | |
| 524 0, estimator.GetEntriesCount(NetLogEventType::NETWORK_QUALITY_CHANGED)); | |
|
eroman
2017/01/19 23:57:25
Is it not possible to expect an exact number of ev
tbansal1
2017/01/20 01:30:34
Done.
| |
| 517 | 525 |
| 518 EXPECT_EQ(3, rtt_throughput_estimates_observer.notifications_received()); | 526 EXPECT_EQ(3, rtt_throughput_estimates_observer.notifications_received()); |
| 519 EXPECT_EQ(base::TimeDelta::FromMilliseconds(272), | 527 EXPECT_EQ(base::TimeDelta::FromMilliseconds(272), |
| 520 rtt_throughput_estimates_observer.http_rtt()); | 528 rtt_throughput_estimates_observer.http_rtt()); |
| 521 EXPECT_EQ(base::TimeDelta::FromMilliseconds(209), | 529 EXPECT_EQ(base::TimeDelta::FromMilliseconds(209), |
| 522 rtt_throughput_estimates_observer.transport_rtt()); | 530 rtt_throughput_estimates_observer.transport_rtt()); |
| 523 EXPECT_EQ(749, | 531 EXPECT_EQ(749, |
| 524 rtt_throughput_estimates_observer.downstream_throughput_kbps()); | 532 rtt_throughput_estimates_observer.downstream_throughput_kbps()); |
| 525 | 533 |
| 526 EXPECT_EQ(2U, rtt_observer.observations().size()); | 534 EXPECT_EQ(2U, rtt_observer.observations().size()); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 551 // Negative variation value should not be used. | 559 // Negative variation value should not be used. |
| 552 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; | 560 variation_params["2G.DefaultMedianRTTMsec"] = "-5"; |
| 553 | 561 |
| 554 variation_params["Unknown.DefaultMedianTransportRTTMsec"] = "500"; | 562 variation_params["Unknown.DefaultMedianTransportRTTMsec"] = "500"; |
| 555 variation_params["WiFi.DefaultMedianTransportRTTMsec"] = "1000"; | 563 variation_params["WiFi.DefaultMedianTransportRTTMsec"] = "1000"; |
| 556 // Negative variation value should not be used. | 564 // Negative variation value should not be used. |
| 557 variation_params["2G.DefaultMedianTransportRTTMsec"] = "-5"; | 565 variation_params["2G.DefaultMedianTransportRTTMsec"] = "-5"; |
| 558 | 566 |
| 559 TestNetworkQualityEstimator estimator( | 567 TestNetworkQualityEstimator estimator( |
| 560 nullptr, variation_params, false, false, | 568 nullptr, variation_params, false, false, |
| 561 true /* add_default_platform_observations */); | 569 true /* add_default_platform_observations */, |
| 570 base::MakeUnique<BoundTestNetLog>()); | |
| 562 | 571 |
| 563 base::TimeDelta rtt; | 572 base::TimeDelta rtt; |
| 564 int32_t kbps; | 573 int32_t kbps; |
| 565 | 574 |
| 566 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 575 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 567 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); | 576 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), rtt); |
| 568 EXPECT_TRUE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); | 577 EXPECT_TRUE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); |
| 569 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), rtt); | 578 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), rtt); |
| 570 EXPECT_TRUE( | 579 EXPECT_TRUE( |
| 571 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 580 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| (...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1444 }, | 1453 }, |
| 1445 { | 1454 { |
| 1446 true, | 1455 true, |
| 1447 }, | 1456 }, |
| 1448 }; | 1457 }; |
| 1449 | 1458 |
| 1450 for (const auto& test : tests) { | 1459 for (const auto& test : tests) { |
| 1451 TestNetworkQualityEstimator estimator( | 1460 TestNetworkQualityEstimator estimator( |
| 1452 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, | 1461 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, |
| 1453 test.allow_small_localhost_requests, | 1462 test.allow_small_localhost_requests, |
| 1454 test.allow_small_localhost_requests, false); | 1463 test.allow_small_localhost_requests, false, |
| 1464 base::MakeUnique<BoundTestNetLog>()); | |
| 1455 | 1465 |
| 1456 base::TimeDelta rtt; | 1466 base::TimeDelta rtt; |
| 1457 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 1467 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 1458 int32_t kbps; | 1468 int32_t kbps; |
| 1459 EXPECT_FALSE( | 1469 EXPECT_FALSE( |
| 1460 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 1470 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 1461 | 1471 |
| 1462 TestDelegate test_delegate; | 1472 TestDelegate test_delegate; |
| 1463 TestURLRequestContext context(true); | 1473 TestURLRequestContext context(true); |
| 1464 context.set_network_quality_estimator(&estimator); | 1474 context.set_network_quality_estimator(&estimator); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1512 estimator.set_start_time_null_downlink_throughput_kbps(100000); | 1522 estimator.set_start_time_null_downlink_throughput_kbps(100000); |
| 1513 | 1523 |
| 1514 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); | 1524 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
| 1515 | 1525 |
| 1516 std::unique_ptr<URLRequest> request(context.CreateRequest( | 1526 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 1517 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1527 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1518 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | 1528 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
| 1519 request->Start(); | 1529 request->Start(); |
| 1520 base::RunLoop().Run(); | 1530 base::RunLoop().Run(); |
| 1521 EXPECT_EQ(1U, observer.effective_connection_types().size()); | 1531 EXPECT_EQ(1U, observer.effective_connection_types().size()); |
| 1532 EXPECT_LT( | |
| 1533 0, estimator.GetEntriesCount(NetLogEventType::NETWORK_QUALITY_CHANGED)); | |
| 1522 histogram_tester.ExpectUniqueSample("NQE.MainFrame.EffectiveConnectionType", | 1534 histogram_tester.ExpectUniqueSample("NQE.MainFrame.EffectiveConnectionType", |
| 1523 EFFECTIVE_CONNECTION_TYPE_2G, 1); | 1535 EFFECTIVE_CONNECTION_TYPE_2G, 1); |
| 1524 histogram_tester.ExpectUniqueSample( | 1536 histogram_tester.ExpectUniqueSample( |
| 1525 "NQE.MainFrame.EffectiveConnectionType.Unknown", | 1537 "NQE.MainFrame.EffectiveConnectionType.Unknown", |
| 1526 EFFECTIVE_CONNECTION_TYPE_2G, 1); | 1538 EFFECTIVE_CONNECTION_TYPE_2G, 1); |
| 1527 | 1539 |
| 1528 // Next request should not trigger recomputation of effective connection type | 1540 // Next request should not trigger recomputation of effective connection type |
| 1529 // since there has been no change in the clock. | 1541 // since there has been no change in the clock. |
| 1530 std::unique_ptr<URLRequest> request2(context.CreateRequest( | 1542 std::unique_ptr<URLRequest> request2(context.CreateRequest( |
| 1531 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1543 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| (...skipping 1070 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2602 "test_ect_4g")] = | 2614 "test_ect_4g")] = |
| 2603 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); | 2615 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2604 | 2616 |
| 2605 std::map<std::string, std::string> variation_params; | 2617 std::map<std::string, std::string> variation_params; |
| 2606 variation_params["effective_connection_type_algorithm"] = | 2618 variation_params["effective_connection_type_algorithm"] = |
| 2607 "TransportRTTOrDownstreamThroughput"; | 2619 "TransportRTTOrDownstreamThroughput"; |
| 2608 // Disable default platform values so that the effect of cached estimates | 2620 // Disable default platform values so that the effect of cached estimates |
| 2609 // at the time of startup can be studied in isolation. | 2621 // at the time of startup can be studied in isolation. |
| 2610 TestNetworkQualityEstimator estimator( | 2622 TestNetworkQualityEstimator estimator( |
| 2611 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, | 2623 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2612 true, false /* use_default_platform_values */); | 2624 true, false /* use_default_platform_values */, |
| 2625 base::MakeUnique<BoundTestNetLog>()); | |
| 2613 | 2626 |
| 2614 // Add observers. | 2627 // Add observers. |
| 2615 TestRTTObserver rtt_observer; | 2628 TestRTTObserver rtt_observer; |
| 2616 TestThroughputObserver throughput_observer; | 2629 TestThroughputObserver throughput_observer; |
| 2617 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; | 2630 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; |
| 2618 TestEffectiveConnectionTypeObserver effective_connection_type_observer; | 2631 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| 2619 estimator.AddRTTObserver(&rtt_observer); | 2632 estimator.AddRTTObserver(&rtt_observer); |
| 2620 estimator.AddThroughputObserver(&throughput_observer); | 2633 estimator.AddThroughputObserver(&throughput_observer); |
| 2621 estimator.AddRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); | 2634 estimator.AddRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2622 estimator.AddEffectiveConnectionTypeObserver( | 2635 estimator.AddEffectiveConnectionTypeObserver( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2689 | 2702 |
| 2690 // Cleanup. | 2703 // Cleanup. |
| 2691 estimator.RemoveRTTObserver(&rtt_observer); | 2704 estimator.RemoveRTTObserver(&rtt_observer); |
| 2692 estimator.RemoveThroughputObserver(&throughput_observer); | 2705 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2693 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); | 2706 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2694 estimator.RemoveEffectiveConnectionTypeObserver( | 2707 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2695 &effective_connection_type_observer); | 2708 &effective_connection_type_observer); |
| 2696 } | 2709 } |
| 2697 | 2710 |
| 2698 } // namespace net | 2711 } // namespace net |
| OLD | NEW |