| 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> |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 160 |
| 161 private: | 161 private: |
| 162 std::vector<Observation> observations_; | 162 std::vector<Observation> observations_; |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 } // namespace | 165 } // namespace |
| 166 | 166 |
| 167 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 167 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 168 base::HistogramTester histogram_tester; | 168 base::HistogramTester histogram_tester; |
| 169 // Enable requests to local host to be used for network quality estimation. | 169 // Enable requests to local host to be used for network quality estimation. |
| 170 TestNetworkQualityEstimator estimator; | 170 std::map<std::string, std::string> variation_params; |
| 171 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 172 TestNetworkQualityEstimator estimator(variation_params); |
| 171 | 173 |
| 172 estimator.SimulateNetworkChange( | 174 estimator.SimulateNetworkChange( |
| 173 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); | 175 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); |
| 174 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", | 176 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", |
| 175 false, 1); | 177 false, 1); |
| 176 | 178 |
| 177 base::TimeDelta rtt; | 179 base::TimeDelta rtt; |
| 178 int32_t kbps; | 180 int32_t kbps; |
| 179 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 181 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 180 EXPECT_FALSE( | 182 EXPECT_FALSE( |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", false, | 319 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", false, |
| 318 3); | 320 3); |
| 319 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, | 321 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, |
| 320 1); | 322 1); |
| 321 } | 323 } |
| 322 | 324 |
| 323 // Tests that the network quality estimator writes and reads network quality | 325 // Tests that the network quality estimator writes and reads network quality |
| 324 // from the cache store correctly. | 326 // from the cache store correctly. |
| 325 TEST(NetworkQualityEstimatorTest, Caching) { | 327 TEST(NetworkQualityEstimatorTest, Caching) { |
| 326 base::HistogramTester histogram_tester; | 328 base::HistogramTester histogram_tester; |
| 327 TestNetworkQualityEstimator estimator; | 329 std::map<std::string, std::string> variation_params; |
| 330 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 331 TestNetworkQualityEstimator estimator(variation_params); |
| 328 | 332 |
| 329 estimator.SimulateNetworkChange( | 333 estimator.SimulateNetworkChange( |
| 330 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); | 334 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 331 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", | 335 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", |
| 332 false, 1); | 336 false, 1); |
| 333 | 337 |
| 334 base::TimeDelta rtt; | 338 base::TimeDelta rtt; |
| 335 int32_t kbps; | 339 int32_t kbps; |
| 336 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 340 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 337 EXPECT_FALSE( | 341 EXPECT_FALSE( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 // Verify that the cached network quality was read, and observers were | 412 // Verify that the cached network quality was read, and observers were |
| 409 // notified. |observer| must be notified once right after it was added, and | 413 // notified. |observer| must be notified once right after it was added, and |
| 410 // once again after the cached network quality was read. | 414 // once again after the cached network quality was read. |
| 411 EXPECT_LE(2U, observer.effective_connection_types().size()); | 415 EXPECT_LE(2U, observer.effective_connection_types().size()); |
| 412 EXPECT_EQ(estimator.GetEffectiveConnectionType(), | 416 EXPECT_EQ(estimator.GetEffectiveConnectionType(), |
| 413 observer.effective_connection_types().back()); | 417 observer.effective_connection_types().back()); |
| 414 EXPECT_EQ(1U, rtt_observer.observations().size()); | 418 EXPECT_EQ(1U, rtt_observer.observations().size()); |
| 415 EXPECT_EQ(1U, throughput_observer.observations().size()); | 419 EXPECT_EQ(1U, throughput_observer.observations().size()); |
| 416 } | 420 } |
| 417 | 421 |
| 422 // Tests that the network quality estimator does not read the network quality |
| 423 // from the cache store when caching is not enabled. |
| 424 TEST(NetworkQualityEstimatorTest, CachingDisabled) { |
| 425 base::HistogramTester histogram_tester; |
| 426 std::map<std::string, std::string> variation_params; |
| 427 // Do not set |persistent_cache_reading_enabled| variation param. |
| 428 TestNetworkQualityEstimator estimator(variation_params); |
| 429 |
| 430 estimator.SimulateNetworkChange( |
| 431 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 432 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 433 |
| 434 base::TimeDelta rtt; |
| 435 int32_t kbps; |
| 436 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 437 EXPECT_FALSE( |
| 438 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 439 |
| 440 TestDelegate test_delegate; |
| 441 TestURLRequestContext context(true); |
| 442 context.set_network_quality_estimator(&estimator); |
| 443 context.Init(); |
| 444 |
| 445 // Start two requests so that the network quality is added to cache store at |
| 446 // the beginning of the second request from the network traffic observed from |
| 447 // the first request. |
| 448 for (size_t i = 0; i < 2; ++i) { |
| 449 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 450 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 451 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
| 452 request->Start(); |
| 453 base::RunLoop().Run(); |
| 454 } |
| 455 |
| 456 base::RunLoop().RunUntilIdle(); |
| 457 |
| 458 // Both RTT and downstream throughput should be updated. |
| 459 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 460 EXPECT_TRUE( |
| 461 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 462 EXPECT_NE(EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 463 estimator.GetEffectiveConnectionType()); |
| 464 EXPECT_FALSE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); |
| 465 |
| 466 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 467 |
| 468 // Add the observers before changing the network type. |
| 469 TestRTTObserver rtt_observer; |
| 470 estimator.AddRTTObserver(&rtt_observer); |
| 471 TestThroughputObserver throughput_observer; |
| 472 estimator.AddThroughputObserver(&throughput_observer); |
| 473 |
| 474 estimator.SimulateNetworkChange( |
| 475 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 476 |
| 477 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 478 base::RunLoop().RunUntilIdle(); |
| 479 |
| 480 // Verify that the cached network quality was read, and observers were |
| 481 // notified. |observer| must be notified once right after it was added, and |
| 482 // once again after the cached network quality was read. |
| 483 EXPECT_EQ(0U, rtt_observer.observations().size()); |
| 484 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 485 } |
| 486 |
| 418 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 487 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 419 TestNetworkQualityEstimator estimator; | 488 TestNetworkQualityEstimator estimator; |
| 420 | 489 |
| 421 base::TimeDelta rtt; | 490 base::TimeDelta rtt; |
| 422 int32_t kbps; | 491 int32_t kbps; |
| 423 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 492 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 424 EXPECT_FALSE( | 493 EXPECT_FALSE( |
| 425 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 494 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 426 | 495 |
| 427 TestDelegate test_delegate; | 496 TestDelegate test_delegate; |
| (...skipping 2254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2682 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, | 2751 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2683 "test_ect_slow_2g")] = | 2752 "test_ect_slow_2g")] = |
| 2684 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 2753 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 2685 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, | 2754 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, |
| 2686 "test_ect_4g")] = | 2755 "test_ect_4g")] = |
| 2687 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); | 2756 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2688 | 2757 |
| 2689 std::map<std::string, std::string> variation_params; | 2758 std::map<std::string, std::string> variation_params; |
| 2690 variation_params["effective_connection_type_algorithm"] = | 2759 variation_params["effective_connection_type_algorithm"] = |
| 2691 "TransportRTTOrDownstreamThroughput"; | 2760 "TransportRTTOrDownstreamThroughput"; |
| 2761 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 2692 // Disable default platform values so that the effect of cached estimates | 2762 // Disable default platform values so that the effect of cached estimates |
| 2693 // at the time of startup can be studied in isolation. | 2763 // at the time of startup can be studied in isolation. |
| 2694 TestNetworkQualityEstimator estimator( | 2764 TestNetworkQualityEstimator estimator( |
| 2695 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, | 2765 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2696 true, false /* use_default_platform_values */, | 2766 true, false /* use_default_platform_values */, |
| 2697 base::MakeUnique<BoundTestNetLog>()); | 2767 base::MakeUnique<BoundTestNetLog>()); |
| 2698 | 2768 |
| 2699 // Add observers. | 2769 // Add observers. |
| 2700 TestRTTObserver rtt_observer; | 2770 TestRTTObserver rtt_observer; |
| 2701 TestThroughputObserver throughput_observer; | 2771 TestThroughputObserver throughput_observer; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2771 effective_connection_type_observer.effective_connection_types().back()); | 2841 effective_connection_type_observer.effective_connection_types().back()); |
| 2772 | 2842 |
| 2773 // Cleanup. | 2843 // Cleanup. |
| 2774 estimator.RemoveRTTObserver(&rtt_observer); | 2844 estimator.RemoveRTTObserver(&rtt_observer); |
| 2775 estimator.RemoveThroughputObserver(&throughput_observer); | 2845 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2776 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); | 2846 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2777 estimator.RemoveEffectiveConnectionTypeObserver( | 2847 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2778 &effective_connection_type_observer); | 2848 &effective_connection_type_observer); |
| 2779 } | 2849 } |
| 2780 | 2850 |
| 2851 // Verify that the cached network qualities from the prefs are not used if the |
| 2852 // reading of the network quality prefs is not enabled.. |
| 2853 TEST(NetworkQualityEstimatorTest, OnPrefsReadWithReadingDisabled) { |
| 2854 base::HistogramTester histogram_tester; |
| 2855 |
| 2856 // Construct the read prefs. |
| 2857 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
| 2858 read_prefs; |
| 2859 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2860 "test_ect_2g")] = |
| 2861 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_2G); |
| 2862 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2863 "test_ect_slow_2g")] = |
| 2864 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 2865 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, |
| 2866 "test_ect_4g")] = |
| 2867 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2868 |
| 2869 std::map<std::string, std::string> variation_params; |
| 2870 variation_params["effective_connection_type_algorithm"] = |
| 2871 "TransportRTTOrDownstreamThroughput"; |
| 2872 // |persistent_cache_reading_enabled| variation param is not set. |
| 2873 |
| 2874 // Disable default platform values so that the effect of cached estimates |
| 2875 // at the time of startup can be studied in isolation. |
| 2876 TestNetworkQualityEstimator estimator( |
| 2877 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2878 true, false /* use_default_platform_values */, |
| 2879 base::MakeUnique<BoundTestNetLog>()); |
| 2880 |
| 2881 // Add observers. |
| 2882 TestRTTObserver rtt_observer; |
| 2883 TestThroughputObserver throughput_observer; |
| 2884 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; |
| 2885 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| 2886 estimator.AddRTTObserver(&rtt_observer); |
| 2887 estimator.AddThroughputObserver(&throughput_observer); |
| 2888 estimator.AddRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2889 estimator.AddEffectiveConnectionTypeObserver( |
| 2890 &effective_connection_type_observer); |
| 2891 |
| 2892 std::string network_name("test_ect_2g"); |
| 2893 |
| 2894 estimator.SimulateNetworkChange( |
| 2895 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2896 EXPECT_EQ(0u, rtt_observer.observations().size()); |
| 2897 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2898 EXPECT_LE(0, rtt_throughput_observer.notifications_received()); |
| 2899 |
| 2900 // Simulate reading of prefs. |
| 2901 estimator.OnPrefsRead(read_prefs); |
| 2902 histogram_tester.ExpectUniqueSample("NQE.Prefs.ReadSize", read_prefs.size(), |
| 2903 1); |
| 2904 |
| 2905 // Force read the network quality store from the store to verify that store |
| 2906 // gets populated even if reading of prefs is not enabled. |
| 2907 nqe::internal::CachedNetworkQuality cached_network_quality; |
| 2908 EXPECT_TRUE(estimator.network_quality_store_->GetById( |
| 2909 nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2910 "test_ect_2g"), |
| 2911 &cached_network_quality)); |
| 2912 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_2G, |
| 2913 cached_network_quality.effective_connection_type()); |
| 2914 |
| 2915 // Taken from network_quality_estimator_params.cc. |
| 2916 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2917 rtt_observer.last_rtt( |
| 2918 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2919 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2920 rtt_observer.last_rtt( |
| 2921 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2922 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2923 |
| 2924 EXPECT_EQ( |
| 2925 0u, |
| 2926 effective_connection_type_observer.effective_connection_types().size()); |
| 2927 |
| 2928 // Change to a different connection type. |
| 2929 network_name = "test_ect_slow_2g"; |
| 2930 estimator.SimulateNetworkChange( |
| 2931 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2932 |
| 2933 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2934 rtt_observer.last_rtt( |
| 2935 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2936 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2937 rtt_observer.last_rtt( |
| 2938 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2939 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 2940 |
| 2941 // Cleanup. |
| 2942 estimator.RemoveRTTObserver(&rtt_observer); |
| 2943 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2944 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2945 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2946 &effective_connection_type_observer); |
| 2947 } |
| 2948 |
| 2781 } // namespace net | 2949 } // namespace net |
| OLD | NEW |