| 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 1583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1594 "test"); | 1594 "test"); |
| 1595 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1595 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
| 1596 | 1596 |
| 1597 // A change in effective connection type does not trigger notification to the | 1597 // A change in effective connection type does not trigger notification to the |
| 1598 // observers, since it is not accompanied by any new observation or a network | 1598 // observers, since it is not accompanied by any new observation or a network |
| 1599 // change event. | 1599 // change event. |
| 1600 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 1600 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 1601 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1601 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
| 1602 } | 1602 } |
| 1603 | 1603 |
| 1604 // Tests that the effective connection type is computed on every RTT |
| 1605 // observation if the last computed effective connection type was unknown. |
| 1606 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { |
| 1607 std::unique_ptr<base::SimpleTestTickClock> tick_clock( |
| 1608 new base::SimpleTestTickClock()); |
| 1609 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); |
| 1610 |
| 1611 TestEffectiveConnectionTypeObserver observer; |
| 1612 std::map<std::string, std::string> variation_params; |
| 1613 TestNetworkQualityEstimator estimator(variation_params); |
| 1614 estimator.SetTickClockForTesting(std::move(tick_clock)); |
| 1615 estimator.AddEffectiveConnectionTypeObserver(&observer); |
| 1616 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
| 1617 |
| 1618 size_t expected_effective_connection_type_notifications = 0; |
| 1619 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 1620 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 1621 "test"); |
| 1622 |
| 1623 NetworkQualityEstimator::RttObservation rtt_observation( |
| 1624 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
| 1625 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST); |
| 1626 |
| 1627 for (size_t i = 0; i < 10; ++i) { |
| 1628 estimator.NotifyObserversOfRTT(rtt_observation); |
| 1629 EXPECT_EQ(expected_effective_connection_type_notifications, |
| 1630 observer.effective_connection_types().size()); |
| 1631 } |
| 1632 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 1633 // Even though there are 10 RTT samples already available, the addition of one |
| 1634 // more RTT sample should trigger recomputation of the effective connection |
| 1635 // type since the last computed effective connection type was unknown. |
| 1636 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( |
| 1637 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
| 1638 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| 1639 ++expected_effective_connection_type_notifications; |
| 1640 EXPECT_EQ(expected_effective_connection_type_notifications, |
| 1641 observer.effective_connection_types().size()); |
| 1642 } |
| 1643 |
| 1644 // Tests that the effective connection type is computed regularly depending |
| 1645 // on the number of RTT and bandwidth samples. |
| 1646 TEST(NetworkQualityEstimatorTest, |
| 1647 AdaptiveRecomputationEffectiveConnectionType) { |
| 1648 base::HistogramTester histogram_tester; |
| 1649 std::unique_ptr<base::SimpleTestTickClock> tick_clock( |
| 1650 new base::SimpleTestTickClock()); |
| 1651 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); |
| 1652 |
| 1653 TestEffectiveConnectionTypeObserver observer; |
| 1654 std::map<std::string, std::string> variation_params; |
| 1655 TestNetworkQualityEstimator estimator(variation_params); |
| 1656 estimator.SetTickClockForTesting(std::move(tick_clock)); |
| 1657 estimator.SimulateNetworkChangeTo(NetworkChangeNotifier::CONNECTION_WIFI, |
| 1658 "test"); |
| 1659 estimator.AddEffectiveConnectionTypeObserver(&observer); |
| 1660 |
| 1661 TestDelegate test_delegate; |
| 1662 TestURLRequestContext context(true); |
| 1663 context.set_network_quality_estimator(&estimator); |
| 1664 context.Init(); |
| 1665 |
| 1666 EXPECT_EQ(0U, observer.effective_connection_types().size()); |
| 1667 |
| 1668 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); |
| 1669 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); |
| 1670 |
| 1671 std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 1672 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1673 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1674 request->Start(); |
| 1675 base::RunLoop().Run(); |
| 1676 EXPECT_EQ(1U, observer.effective_connection_types().size()); |
| 1677 histogram_tester.ExpectUniqueSample( |
| 1678 "NQE.MainFrame.EffectiveConnectionType.WiFi", |
| 1679 EFFECTIVE_CONNECTION_TYPE_2G, 1); |
| 1680 |
| 1681 size_t expected_effective_connection_type_notifications = 1; |
| 1682 EXPECT_EQ(expected_effective_connection_type_notifications, |
| 1683 observer.effective_connection_types().size()); |
| 1684 |
| 1685 EXPECT_EQ(expected_effective_connection_type_notifications, |
| 1686 estimator.rtt_observations_.Size()); |
| 1687 |
| 1688 // Increase the number of RTT observations. Every time the number of RTT |
| 1689 // observations is more than doubled, effective connection type must be |
| 1690 // recomputed and notified to observers. |
| 1691 for (size_t repetition = 0; repetition < 2; ++repetition) { |
| 1692 // Change the effective connection type so that the observers are |
| 1693 // notified when the effective connection type is recomputed. |
| 1694 if (repetition % 2 == 0) { |
| 1695 estimator.set_effective_connection_type( |
| 1696 EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 1697 } else { |
| 1698 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 1699 } |
| 1700 size_t rtt_observations_count = estimator.rtt_observations_.Size(); |
| 1701 // Increase the number of RTT observations to more than twice the number |
| 1702 // of current observations. This should trigger recomputation of |
| 1703 // effective connection type. |
| 1704 for (size_t i = 0; i < rtt_observations_count + 1; ++i) { |
| 1705 estimator.rtt_observations_.AddObservation( |
| 1706 NetworkQualityEstimator::RttObservation( |
| 1707 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
| 1708 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| 1709 |
| 1710 estimator.NotifyObserversOfRTT(NetworkQualityEstimator::RttObservation( |
| 1711 base::TimeDelta::FromSeconds(5), tick_clock_ptr->NowTicks(), |
| 1712 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST)); |
| 1713 |
| 1714 if (i == rtt_observations_count) { |
| 1715 // Effective connection type must be recomputed since the number of RTT |
| 1716 // samples are now more than twice the number of RTT samples that were |
| 1717 // available when effective connection type was last computed. |
| 1718 ++expected_effective_connection_type_notifications; |
| 1719 } |
| 1720 EXPECT_EQ(expected_effective_connection_type_notifications, |
| 1721 observer.effective_connection_types().size()); |
| 1722 } |
| 1723 } |
| 1724 } |
| 1725 |
| 1604 TEST(NetworkQualityEstimatorTest, TestRttThroughputObservers) { | 1726 TEST(NetworkQualityEstimatorTest, TestRttThroughputObservers) { |
| 1605 TestRTTObserver rtt_observer; | 1727 TestRTTObserver rtt_observer; |
| 1606 TestThroughputObserver throughput_observer; | 1728 TestThroughputObserver throughput_observer; |
| 1607 std::map<std::string, std::string> variation_params; | 1729 std::map<std::string, std::string> variation_params; |
| 1608 TestNetworkQualityEstimator estimator(variation_params); | 1730 TestNetworkQualityEstimator estimator(variation_params); |
| 1609 estimator.AddRTTObserver(&rtt_observer); | 1731 estimator.AddRTTObserver(&rtt_observer); |
| 1610 estimator.AddThroughputObserver(&throughput_observer); | 1732 estimator.AddThroughputObserver(&throughput_observer); |
| 1611 | 1733 |
| 1612 TestDelegate test_delegate; | 1734 TestDelegate test_delegate; |
| 1613 TestURLRequestContext context(true); | 1735 TestURLRequestContext context(true); |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2097 | 2219 |
| 2098 // Get the bits at index 18-24 which contain the resource fetch time. | 2220 // Get the bits at index 18-24 which contain the resource fetch time. |
| 2099 EXPECT_LE(0, (buckets.at(0).min >> kBitsPerMetric) % 128); | 2221 EXPECT_LE(0, (buckets.at(0).min >> kBitsPerMetric) % 128); |
| 2100 | 2222 |
| 2101 // Get the bits at index 25-31 which contain the resource load size. | 2223 // Get the bits at index 25-31 which contain the resource load size. |
| 2102 EXPECT_LE(0, (buckets.at(0).min) % 128); | 2224 EXPECT_LE(0, (buckets.at(0).min) % 128); |
| 2103 } | 2225 } |
| 2104 } | 2226 } |
| 2105 | 2227 |
| 2106 } // namespace net | 2228 } // namespace net |
| OLD | NEW |