| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 158 |
| 159 private: | 159 private: |
| 160 std::vector<Observation> observations_; | 160 std::vector<Observation> observations_; |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 } // namespace | 163 } // namespace |
| 164 | 164 |
| 165 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 165 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 166 base::HistogramTester histogram_tester; | 166 base::HistogramTester histogram_tester; |
| 167 // Enable requests to local host to be used for network quality estimation. | 167 // Enable requests to local host to be used for network quality estimation. |
| 168 TestNetworkQualityEstimator estimator; | 168 std::map<std::string, std::string> variation_params; |
| 169 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 170 TestNetworkQualityEstimator estimator(variation_params); |
| 169 | 171 |
| 170 estimator.SimulateNetworkChange( | 172 estimator.SimulateNetworkChange( |
| 171 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); | 173 NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, "test"); |
| 172 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", | 174 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", |
| 173 false, 1); | 175 false, 1); |
| 174 | 176 |
| 175 base::TimeDelta rtt; | 177 base::TimeDelta rtt; |
| 176 int32_t kbps; | 178 int32_t kbps; |
| 177 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 179 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 178 EXPECT_FALSE( | 180 EXPECT_FALSE( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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); |
| 299 } | 301 } |
| 300 | 302 |
| 301 // Tests that the network quality estimator writes and reads network quality | 303 // Tests that the network quality estimator writes and reads network quality |
| 302 // from the cache store correctly. | 304 // from the cache store correctly. |
| 303 TEST(NetworkQualityEstimatorTest, Caching) { | 305 TEST(NetworkQualityEstimatorTest, Caching) { |
| 304 base::HistogramTester histogram_tester; | 306 base::HistogramTester histogram_tester; |
| 305 TestNetworkQualityEstimator estimator; | 307 std::map<std::string, std::string> variation_params; |
| 308 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 309 TestNetworkQualityEstimator estimator(variation_params); |
| 306 | 310 |
| 307 estimator.SimulateNetworkChange( | 311 estimator.SimulateNetworkChange( |
| 308 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); | 312 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 309 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", | 313 histogram_tester.ExpectUniqueSample("NQE.CachedNetworkQualityAvailable", |
| 310 false, 1); | 314 false, 1); |
| 311 | 315 |
| 312 base::TimeDelta rtt; | 316 base::TimeDelta rtt; |
| 313 int32_t kbps; | 317 int32_t kbps; |
| 314 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 318 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 315 EXPECT_FALSE( | 319 EXPECT_FALSE( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // Verify that the cached network quality was read, and observers were | 370 // Verify that the cached network quality was read, and observers were |
| 367 // notified. |observer| must be notified once right after it was added, and | 371 // notified. |observer| must be notified once right after it was added, and |
| 368 // once again after the cached network quality was read. | 372 // once again after the cached network quality was read. |
| 369 EXPECT_LE(2U, observer.effective_connection_types().size()); | 373 EXPECT_LE(2U, observer.effective_connection_types().size()); |
| 370 EXPECT_EQ(estimator.GetEffectiveConnectionType(), | 374 EXPECT_EQ(estimator.GetEffectiveConnectionType(), |
| 371 observer.effective_connection_types().back()); | 375 observer.effective_connection_types().back()); |
| 372 EXPECT_EQ(1U, rtt_observer.observations().size()); | 376 EXPECT_EQ(1U, rtt_observer.observations().size()); |
| 373 EXPECT_EQ(1U, throughput_observer.observations().size()); | 377 EXPECT_EQ(1U, throughput_observer.observations().size()); |
| 374 } | 378 } |
| 375 | 379 |
| 380 // Tests that the network quality estimator does not read the network quality |
| 381 // from the cache store when caching is not enabled. |
| 382 TEST(NetworkQualityEstimatorTest, CachingDisabled) { |
| 383 base::HistogramTester histogram_tester; |
| 384 std::map<std::string, std::string> variation_params; |
| 385 // Do not set |persistent_cache_reading_enabled| variation param. |
| 386 TestNetworkQualityEstimator estimator(variation_params); |
| 387 |
| 388 estimator.SimulateNetworkChange( |
| 389 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 390 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 391 |
| 392 base::TimeDelta rtt; |
| 393 int32_t kbps; |
| 394 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 395 EXPECT_FALSE( |
| 396 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 397 |
| 398 TestDelegate test_delegate; |
| 399 TestURLRequestContext context(true); |
| 400 context.set_network_quality_estimator(&estimator); |
| 401 context.Init(); |
| 402 |
| 403 // Start two requests so that the network quality is added to cache store at |
| 404 // the beginning of the second request from the network traffic observed from |
| 405 // the first request. |
| 406 for (size_t i = 0; i < 2; ++i) { |
| 407 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 408 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 409 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
| 410 request->Start(); |
| 411 base::RunLoop().Run(); |
| 412 } |
| 413 |
| 414 base::RunLoop().RunUntilIdle(); |
| 415 |
| 416 // Both RTT and downstream throughput should be updated. |
| 417 EXPECT_TRUE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 418 EXPECT_TRUE( |
| 419 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 420 EXPECT_NE(EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 421 estimator.GetEffectiveConnectionType()); |
| 422 EXPECT_FALSE(estimator.GetRecentTransportRTT(base::TimeTicks(), &rtt)); |
| 423 |
| 424 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 425 |
| 426 // Add the observers before changing the network type. |
| 427 TestRTTObserver rtt_observer; |
| 428 estimator.AddRTTObserver(&rtt_observer); |
| 429 TestThroughputObserver throughput_observer; |
| 430 estimator.AddThroughputObserver(&throughput_observer); |
| 431 |
| 432 estimator.SimulateNetworkChange( |
| 433 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 434 |
| 435 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 0); |
| 436 base::RunLoop().RunUntilIdle(); |
| 437 |
| 438 // Verify that the cached network quality was read, and observers were |
| 439 // notified. |observer| must be notified once right after it was added, and |
| 440 // once again after the cached network quality was read. |
| 441 EXPECT_EQ(0U, rtt_observer.observations().size()); |
| 442 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 443 } |
| 444 |
| 376 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 445 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 377 TestNetworkQualityEstimator estimator; | 446 TestNetworkQualityEstimator estimator; |
| 378 | 447 |
| 379 base::TimeDelta rtt; | 448 base::TimeDelta rtt; |
| 380 int32_t kbps; | 449 int32_t kbps; |
| 381 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); | 450 EXPECT_FALSE(estimator.GetRecentHttpRTT(base::TimeTicks(), &rtt)); |
| 382 EXPECT_FALSE( | 451 EXPECT_FALSE( |
| 383 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); | 452 estimator.GetRecentDownlinkThroughputKbps(base::TimeTicks(), &kbps)); |
| 384 | 453 |
| 385 TestDelegate test_delegate; | 454 TestDelegate test_delegate; |
| (...skipping 2251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2637 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, | 2706 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2638 "test_ect_slow_2g")] = | 2707 "test_ect_slow_2g")] = |
| 2639 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 2708 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 2640 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, | 2709 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, |
| 2641 "test_ect_4g")] = | 2710 "test_ect_4g")] = |
| 2642 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); | 2711 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2643 | 2712 |
| 2644 std::map<std::string, std::string> variation_params; | 2713 std::map<std::string, std::string> variation_params; |
| 2645 variation_params["effective_connection_type_algorithm"] = | 2714 variation_params["effective_connection_type_algorithm"] = |
| 2646 "TransportRTTOrDownstreamThroughput"; | 2715 "TransportRTTOrDownstreamThroughput"; |
| 2716 variation_params["persistent_cache_reading_enabled"] = "true"; |
| 2647 // Disable default platform values so that the effect of cached estimates | 2717 // Disable default platform values so that the effect of cached estimates |
| 2648 // at the time of startup can be studied in isolation. | 2718 // at the time of startup can be studied in isolation. |
| 2649 TestNetworkQualityEstimator estimator( | 2719 TestNetworkQualityEstimator estimator( |
| 2650 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, | 2720 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2651 true, false /* use_default_platform_values */); | 2721 true, false /* use_default_platform_values */); |
| 2652 | 2722 |
| 2653 // Add observers. | 2723 // Add observers. |
| 2654 TestRTTObserver rtt_observer; | 2724 TestRTTObserver rtt_observer; |
| 2655 TestThroughputObserver throughput_observer; | 2725 TestThroughputObserver throughput_observer; |
| 2656 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; | 2726 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2725 effective_connection_type_observer.effective_connection_types().back()); | 2795 effective_connection_type_observer.effective_connection_types().back()); |
| 2726 | 2796 |
| 2727 // Cleanup. | 2797 // Cleanup. |
| 2728 estimator.RemoveRTTObserver(&rtt_observer); | 2798 estimator.RemoveRTTObserver(&rtt_observer); |
| 2729 estimator.RemoveThroughputObserver(&throughput_observer); | 2799 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2730 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); | 2800 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2731 estimator.RemoveEffectiveConnectionTypeObserver( | 2801 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2732 &effective_connection_type_observer); | 2802 &effective_connection_type_observer); |
| 2733 } | 2803 } |
| 2734 | 2804 |
| 2805 // Verify that the cached network qualities from the prefs are not used if the |
| 2806 // reading of the network quality prefs is not enabled.. |
| 2807 TEST(NetworkQualityEstimatorTest, OnPrefsReadWithReadingDisabled) { |
| 2808 base::HistogramTester histogram_tester; |
| 2809 |
| 2810 // Construct the read prefs. |
| 2811 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
| 2812 read_prefs; |
| 2813 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2814 "test_ect_2g")] = |
| 2815 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_2G); |
| 2816 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2817 "test_ect_slow_2g")] = |
| 2818 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 2819 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, |
| 2820 "test_ect_4g")] = |
| 2821 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2822 |
| 2823 std::map<std::string, std::string> variation_params; |
| 2824 variation_params["effective_connection_type_algorithm"] = |
| 2825 "TransportRTTOrDownstreamThroughput"; |
| 2826 // |persistent_cache_reading_enabled| variation param is not set. |
| 2827 |
| 2828 // Disable default platform values so that the effect of cached estimates |
| 2829 // at the time of startup can be studied in isolation. |
| 2830 TestNetworkQualityEstimator estimator( |
| 2831 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2832 true, false /* use_default_platform_values */); |
| 2833 |
| 2834 // Add observers. |
| 2835 TestRTTObserver rtt_observer; |
| 2836 TestThroughputObserver throughput_observer; |
| 2837 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; |
| 2838 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| 2839 estimator.AddRTTObserver(&rtt_observer); |
| 2840 estimator.AddThroughputObserver(&throughput_observer); |
| 2841 estimator.AddRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2842 estimator.AddEffectiveConnectionTypeObserver( |
| 2843 &effective_connection_type_observer); |
| 2844 |
| 2845 std::string network_name("test_ect_2g"); |
| 2846 |
| 2847 estimator.SimulateNetworkChange( |
| 2848 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2849 EXPECT_EQ(0u, rtt_observer.observations().size()); |
| 2850 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2851 EXPECT_LE(0, rtt_throughput_observer.notifications_received()); |
| 2852 |
| 2853 // Simulate reading of prefs. |
| 2854 estimator.OnPrefsRead(read_prefs); |
| 2855 histogram_tester.ExpectUniqueSample("NQE.Prefs.ReadSize", read_prefs.size(), |
| 2856 1); |
| 2857 |
| 2858 // Force read the network quality store from the store to verify that store |
| 2859 // gets populated even if reading of prefs is not enabled. |
| 2860 nqe::internal::CachedNetworkQuality cached_network_quality; |
| 2861 EXPECT_TRUE(estimator.network_quality_store_->GetById( |
| 2862 nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2863 "test_ect_2g"), |
| 2864 &cached_network_quality)); |
| 2865 EXPECT_EQ(EFFECTIVE_CONNECTION_TYPE_2G, |
| 2866 cached_network_quality.effective_connection_type()); |
| 2867 |
| 2868 // Taken from network_quality_estimator_params.cc. |
| 2869 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2870 rtt_observer.last_rtt( |
| 2871 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2872 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2873 rtt_observer.last_rtt( |
| 2874 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2875 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2876 |
| 2877 EXPECT_EQ( |
| 2878 0u, |
| 2879 effective_connection_type_observer.effective_connection_types().size()); |
| 2880 |
| 2881 // Change to a different connection type. |
| 2882 network_name = "test_ect_slow_2g"; |
| 2883 estimator.SimulateNetworkChange( |
| 2884 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2885 |
| 2886 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2887 rtt_observer.last_rtt( |
| 2888 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2889 EXPECT_EQ(nqe::internal::InvalidRTT(), |
| 2890 rtt_observer.last_rtt( |
| 2891 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2892 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 2893 |
| 2894 // Cleanup. |
| 2895 estimator.RemoveRTTObserver(&rtt_observer); |
| 2896 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2897 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2898 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2899 &effective_connection_type_observer); |
| 2900 } |
| 2901 |
| 2735 } // namespace net | 2902 } // namespace net |
| OLD | NEW |