| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 111 |
| 112 std::vector<Observation>& observations() { return observations_; } | 112 std::vector<Observation>& observations() { return observations_; } |
| 113 | 113 |
| 114 // RttObserver implementation: | 114 // RttObserver implementation: |
| 115 void OnRTTObservation(int32_t rtt_ms, | 115 void OnRTTObservation(int32_t rtt_ms, |
| 116 const base::TimeTicks& timestamp, | 116 const base::TimeTicks& timestamp, |
| 117 NetworkQualityObservationSource source) override { | 117 NetworkQualityObservationSource source) override { |
| 118 observations_.push_back(Observation(rtt_ms, timestamp, source)); | 118 observations_.push_back(Observation(rtt_ms, timestamp, source)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // Returns the last received RTT observation that has source set to |source|. |
| 122 base::TimeDelta last_rtt(NetworkQualityObservationSource source) { |
| 123 for (std::vector<Observation>::reverse_iterator i = observations_.rbegin(); |
| 124 i != observations_.rend(); ++i) { |
| 125 Observation observation = *i; |
| 126 if (observation.source == source) |
| 127 return base::TimeDelta::FromMilliseconds(observation.rtt_ms); |
| 128 } |
| 129 return nqe::internal::InvalidRTT(); |
| 130 } |
| 131 |
| 121 private: | 132 private: |
| 122 std::vector<Observation> observations_; | 133 std::vector<Observation> observations_; |
| 123 }; | 134 }; |
| 124 | 135 |
| 125 class TestThroughputObserver | 136 class TestThroughputObserver |
| 126 : public NetworkQualityEstimator::ThroughputObserver { | 137 : public NetworkQualityEstimator::ThroughputObserver { |
| 127 public: | 138 public: |
| 128 struct Observation { | 139 struct Observation { |
| 129 Observation(int32_t kbps, | 140 Observation(int32_t kbps, |
| 130 const base::TimeTicks& ts, | 141 const base::TimeTicks& ts, |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 1); | 347 1); |
| 337 | 348 |
| 338 // Add the observers before changing the network type. | 349 // Add the observers before changing the network type. |
| 339 TestEffectiveConnectionTypeObserver observer; | 350 TestEffectiveConnectionTypeObserver observer; |
| 340 estimator.AddEffectiveConnectionTypeObserver(&observer); | 351 estimator.AddEffectiveConnectionTypeObserver(&observer); |
| 341 TestRTTObserver rtt_observer; | 352 TestRTTObserver rtt_observer; |
| 342 estimator.AddRTTObserver(&rtt_observer); | 353 estimator.AddRTTObserver(&rtt_observer); |
| 343 TestThroughputObserver throughput_observer; | 354 TestThroughputObserver throughput_observer; |
| 344 estimator.AddThroughputObserver(&throughput_observer); | 355 estimator.AddThroughputObserver(&throughput_observer); |
| 345 | 356 |
| 357 // |observer| should be notified as soon as it is added. |
| 358 base::RunLoop().RunUntilIdle(); |
| 359 EXPECT_EQ(1U, observer.effective_connection_types().size()); |
| 360 |
| 346 estimator.SimulateNetworkChange( | 361 estimator.SimulateNetworkChange( |
| 347 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); | 362 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test"); |
| 348 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, | 363 histogram_tester.ExpectBucketCount("NQE.CachedNetworkQualityAvailable", true, |
| 349 1); | 364 1); |
| 350 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 2); | 365 histogram_tester.ExpectTotalCount("NQE.CachedNetworkQualityAvailable", 2); |
| 351 base::RunLoop().RunUntilIdle(); | 366 base::RunLoop().RunUntilIdle(); |
| 352 | 367 |
| 353 // Verify that the cached network quality was read, and observers were | 368 // Verify that the cached network quality was read, and observers were |
| 354 // notified. |observer| must be notified once right after it was added, and | 369 // notified. |observer| must be notified once right after it was added, and |
| 355 // once again after the cached network quality was read. | 370 // once again after the cached network quality was read. |
| 356 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 371 EXPECT_LE(2U, observer.effective_connection_types().size()); |
| 372 EXPECT_EQ(estimator.GetEffectiveConnectionType(), |
| 373 observer.effective_connection_types().back()); |
| 357 EXPECT_EQ(1U, rtt_observer.observations().size()); | 374 EXPECT_EQ(1U, rtt_observer.observations().size()); |
| 358 EXPECT_EQ(1U, throughput_observer.observations().size()); | 375 EXPECT_EQ(1U, throughput_observer.observations().size()); |
| 359 } | 376 } |
| 360 | 377 |
| 361 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 378 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 362 std::map<std::string, std::string> variation_params; | 379 std::map<std::string, std::string> variation_params; |
| 363 TestNetworkQualityEstimator estimator(variation_params); | 380 TestNetworkQualityEstimator estimator(variation_params); |
| 364 | 381 |
| 365 base::TimeDelta rtt; | 382 base::TimeDelta rtt; |
| 366 int32_t kbps; | 383 int32_t kbps; |
| (...skipping 2196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2563 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | 2580 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); |
| 2564 request->Start(); | 2581 request->Start(); |
| 2565 base::RunLoop().Run(); | 2582 base::RunLoop().Run(); |
| 2566 | 2583 |
| 2567 EXPECT_EQ(effective_connection_type, | 2584 EXPECT_EQ(effective_connection_type, |
| 2568 estimator.GetEffectiveConnectionType()); | 2585 estimator.GetEffectiveConnectionType()); |
| 2569 } | 2586 } |
| 2570 } | 2587 } |
| 2571 } | 2588 } |
| 2572 | 2589 |
| 2590 // Verify that the cached network qualities from the prefs are correctly used. |
| 2591 TEST(NetworkQualityEstimatorTest, OnPrefsRead) { |
| 2592 base::HistogramTester histogram_tester; |
| 2593 |
| 2594 // Construct the read prefs. |
| 2595 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
| 2596 read_prefs; |
| 2597 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2598 "test_ect_2g")] = |
| 2599 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_2G); |
| 2600 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_WIFI, |
| 2601 "test_ect_slow_2g")] = |
| 2602 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 2603 read_prefs[nqe::internal::NetworkID(NetworkChangeNotifier::CONNECTION_4G, |
| 2604 "test_ect_4g")] = |
| 2605 nqe::internal::CachedNetworkQuality(EFFECTIVE_CONNECTION_TYPE_4G); |
| 2606 |
| 2607 std::map<std::string, std::string> variation_params; |
| 2608 variation_params["effective_connection_type_algorithm"] = |
| 2609 "TransportRTTOrDownstreamThroughput"; |
| 2610 // Disable default platform values so that the effect of cached estimates |
| 2611 // at the time of startup can be studied in isolation. |
| 2612 TestNetworkQualityEstimator estimator( |
| 2613 std::unique_ptr<net::ExternalEstimateProvider>(), variation_params, true, |
| 2614 true, false /* use_default_platform_values */); |
| 2615 |
| 2616 // Add observers. |
| 2617 TestRTTObserver rtt_observer; |
| 2618 TestThroughputObserver throughput_observer; |
| 2619 TestRTTAndThroughputEstimatesObserver rtt_throughput_observer; |
| 2620 TestEffectiveConnectionTypeObserver effective_connection_type_observer; |
| 2621 estimator.AddRTTObserver(&rtt_observer); |
| 2622 estimator.AddThroughputObserver(&throughput_observer); |
| 2623 estimator.AddRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2624 estimator.AddEffectiveConnectionTypeObserver( |
| 2625 &effective_connection_type_observer); |
| 2626 |
| 2627 std::string network_name("test_ect_2g"); |
| 2628 |
| 2629 estimator.SimulateNetworkChange( |
| 2630 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2631 EXPECT_EQ(0u, rtt_observer.observations().size()); |
| 2632 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2633 EXPECT_LE(0, rtt_throughput_observer.notifications_received()); |
| 2634 |
| 2635 // Simulate reading of prefs. |
| 2636 estimator.OnPrefsRead(read_prefs); |
| 2637 histogram_tester.ExpectUniqueSample("NQE.Prefs.ReadSize", read_prefs.size(), |
| 2638 1); |
| 2639 |
| 2640 // Taken from network_quality_estimator_params.cc. |
| 2641 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1800), |
| 2642 rtt_observer.last_rtt( |
| 2643 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2644 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1500), |
| 2645 rtt_observer.last_rtt( |
| 2646 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2647 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2648 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1800), |
| 2649 rtt_throughput_observer.http_rtt()); |
| 2650 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1500), |
| 2651 rtt_throughput_observer.transport_rtt()); |
| 2652 EXPECT_EQ(nqe::internal::kInvalidThroughput, |
| 2653 rtt_throughput_observer.downstream_throughput_kbps()); |
| 2654 EXPECT_LE( |
| 2655 1u, |
| 2656 effective_connection_type_observer.effective_connection_types().size()); |
| 2657 // Compare the ECT stored in prefs with the observer's last entry. |
| 2658 EXPECT_EQ( |
| 2659 read_prefs[nqe::internal::NetworkID( |
| 2660 NetworkChangeNotifier::CONNECTION_WIFI, network_name)] |
| 2661 .effective_connection_type(), |
| 2662 effective_connection_type_observer.effective_connection_types().back()); |
| 2663 |
| 2664 // Change to a different connection type. |
| 2665 network_name = "test_ect_slow_2g"; |
| 2666 estimator.SimulateNetworkChange( |
| 2667 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); |
| 2668 |
| 2669 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3600), |
| 2670 rtt_observer.last_rtt( |
| 2671 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE)); |
| 2672 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3000), |
| 2673 rtt_observer.last_rtt( |
| 2674 NETWORK_QUALITY_OBSERVATION_SOURCE_TRANSPORT_CACHED_ESTIMATE)); |
| 2675 EXPECT_EQ(0u, throughput_observer.observations().size()); |
| 2676 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3600), |
| 2677 rtt_throughput_observer.http_rtt()); |
| 2678 EXPECT_EQ(base::TimeDelta::FromMilliseconds(3000), |
| 2679 rtt_throughput_observer.transport_rtt()); |
| 2680 EXPECT_EQ(nqe::internal::kInvalidThroughput, |
| 2681 rtt_throughput_observer.downstream_throughput_kbps()); |
| 2682 EXPECT_LE( |
| 2683 2u, |
| 2684 effective_connection_type_observer.effective_connection_types().size()); |
| 2685 // Compare with the last entry. |
| 2686 EXPECT_EQ( |
| 2687 read_prefs[nqe::internal::NetworkID( |
| 2688 NetworkChangeNotifier::CONNECTION_WIFI, network_name)] |
| 2689 .effective_connection_type(), |
| 2690 effective_connection_type_observer.effective_connection_types().back()); |
| 2691 |
| 2692 // Cleanup. |
| 2693 estimator.RemoveRTTObserver(&rtt_observer); |
| 2694 estimator.RemoveThroughputObserver(&throughput_observer); |
| 2695 estimator.RemoveRTTAndThroughputEstimatesObserver(&rtt_throughput_observer); |
| 2696 estimator.RemoveEffectiveConnectionTypeObserver( |
| 2697 &effective_connection_type_observer); |
| 2698 } |
| 2699 |
| 2573 } // namespace net | 2700 } // namespace net |
| OLD | NEW |