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/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/safe_sprintf.h" | |
| 11 #include "base/test/histogram_tester.h" | 12 #include "base/test/histogram_tester.h" |
| 12 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
| 16 #include "net/base/network_quality.h" | 17 #include "net/base/network_quality.h" |
| 17 #include "net/test/spawned_test_server/spawned_test_server.h" | 18 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 23 namespace { | |
| 24 | |
| 25 // Helps in setting the current network type and id. | |
| 26 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | |
| 27 public: | |
| 28 TestNetworkQualityEstimator() {} | |
| 29 | |
| 30 ~TestNetworkQualityEstimator() override {} | |
| 31 | |
| 32 // Overrides the current network type and id. | |
| 33 // Notifies network quality estimator of change in connection. | |
| 34 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | |
| 35 std::string network_id) { | |
| 36 current_network_id_ = network_id; | |
| 37 OnConnectionTypeChanged(type); | |
| 38 } | |
| 39 | |
| 40 using NetworkQualityEstimator::GetCacheSizeForTests; | |
| 41 using NetworkQualityEstimator::OnConnectionTypeChanged; | |
| 42 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | |
| 43 | |
| 44 private: | |
| 45 // NetworkQualityEstimator implementation that returns the overridden network | |
| 46 // id (instead of invoking platform APIs). | |
|
bengr
2015/06/11 00:02:28
id -> ID
tbansal1
2015/06/11 02:20:23
"id" may be okay because this refers to NetworkID.
bengr
2015/06/13 00:28:55
Acknowledged.
| |
| 47 std::string GetCurrentNetworkName() const override { | |
|
bengr
2015/06/11 00:02:28
Return a const&?
tbansal1
2015/06/11 02:20:23
Not possible. It has to match the signature of ove
| |
| 48 return current_network_id_; | |
| 49 } | |
| 50 | |
| 51 std::string current_network_id_; | |
| 52 }; | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 22 namespace net { | 56 namespace net { |
| 23 | 57 |
| 24 // SpawnedTestServer not supported on iOS (see http://crbug.com/148666). | 58 // SpawnedTestServer not supported on iOS (see http://crbug.com/148666). |
| 25 #if !defined(OS_IOS) | 59 #if !defined(OS_IOS) |
| 26 TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) { | 60 TEST(NetworkQualityEstimatorTest, TestPeakKbpsFastestRTTUpdates) { |
| 27 SpawnedTestServer test_server_( | 61 SpawnedTestServer test_server_( |
| 28 SpawnedTestServer::TYPE_HTTP, SpawnedTestServer::kLocalhost, | 62 SpawnedTestServer::TYPE_HTTP, SpawnedTestServer::kLocalhost, |
| 29 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | 63 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 30 ASSERT_TRUE(test_server_.Start()); | 64 ASSERT_TRUE(test_server_.Start()); |
| 31 | 65 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 } | 98 } |
| 65 | 99 |
| 66 // With large transfer, both |fastest_rtt| and |peak_throughput_kbps| will be | 100 // With large transfer, both |fastest_rtt| and |peak_throughput_kbps| will be |
| 67 // updated. | 101 // updated. |
| 68 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes); | 102 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes); |
| 69 { | 103 { |
| 70 NetworkQuality network_quality = estimator.GetEstimate(); | 104 NetworkQuality network_quality = estimator.GetEstimate(); |
| 71 EXPECT_GT(network_quality.fastest_rtt_confidence, 0); | 105 EXPECT_GT(network_quality.fastest_rtt_confidence, 0); |
| 72 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); | 106 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); |
| 73 EXPECT_GE(network_quality.fastest_rtt, request_duration); | 107 EXPECT_GE(network_quality.fastest_rtt, request_duration); |
| 74 EXPECT_GT(network_quality.peak_throughput_kbps, uint32_t(0)); | 108 EXPECT_GT(network_quality.peak_throughput_kbps, 0U); |
| 75 EXPECT_LE( | 109 EXPECT_LE( |
| 76 network_quality.peak_throughput_kbps, | 110 network_quality.peak_throughput_kbps, |
| 77 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); | 111 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); |
| 78 } | 112 } |
| 79 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true); | 113 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true); |
| 80 | 114 |
| 81 // Check UMA histograms. | 115 // Check UMA histograms. |
| 82 base::HistogramTester histogram_tester; | 116 base::HistogramTester histogram_tester; |
| 83 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 117 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 84 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 118 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 85 | 119 |
| 86 estimator.OnConnectionTypeChanged( | 120 estimator.OnConnectionTypeChanged( |
| 87 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 121 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 88 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 122 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 89 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 123 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 90 { | 124 { |
| 91 NetworkQuality network_quality = estimator.GetEstimate(); | 125 NetworkQuality network_quality = estimator.GetEstimate(); |
| 92 EXPECT_EQ(estimator.current_connection_type_, | 126 EXPECT_EQ(estimator.current_network_id_.type, |
| 93 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 127 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 94 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0); | 128 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0); |
| 95 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0); | 129 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0); |
| 96 } | 130 } |
| 97 } | 131 } |
| 98 #endif // !defined(OS_IOS) | 132 #endif // !defined(OS_IOS) |
| 99 | 133 |
| 134 // Test if the network estimates are cached when network change notification | |
| 135 // is invoked. | |
| 136 TEST(NetworkQualityEstimatorTest, TestCaching) { | |
| 137 TestNetworkQualityEstimator estimator; | |
| 138 size_t expected_cache_size = 0; | |
| 139 EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 140 | |
| 141 // Cache entry will be added for (NONE, ""). | |
| 142 estimator.SimulateNetworkChangeTo( | |
| 143 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 144 EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 145 | |
| 146 // Entry will be added for (2G, "test1"). | |
| 147 // Also, set the network quality for (2G, "test1") so that it is stored in the | |
| 148 // cache. | |
| 149 estimator.peak_kbps_since_last_connection_change_ = 1; | |
| 150 estimator.fastest_RTT_since_last_connection_change_ = | |
| 151 base::TimeDelta::FromMilliseconds(1000); | |
| 152 estimator.SimulateNetworkChangeTo( | |
| 153 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); | |
| 154 EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 155 | |
| 156 // Entry will be added for (3G, "test1"). | |
| 157 // Also, set the network quality for (3G, "test1") so that it is stored in the | |
| 158 // cache. | |
| 159 estimator.peak_kbps_since_last_connection_change_ = 2; | |
| 160 estimator.fastest_RTT_since_last_connection_change_ = | |
| 161 base::TimeDelta::FromMilliseconds(500); | |
| 162 estimator.SimulateNetworkChangeTo( | |
| 163 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); | |
| 164 EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 165 | |
| 166 // Entry will be added for (3G, "test2"). | |
| 167 estimator.SimulateNetworkChangeTo( | |
| 168 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 169 EXPECT_EQ(++expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 170 | |
| 171 // Read the network quality for (2G, "test-1"). | |
| 172 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 173 EXPECT_EQ(1U, estimator.peak_kbps_since_last_connection_change_); | |
| 174 EXPECT_EQ(base::TimeDelta::FromMilliseconds(1000), | |
| 175 estimator.fastest_RTT_since_last_connection_change_); | |
| 176 // No new entry should be added for (2G, "test1") since it already exists | |
| 177 // in the cache. | |
| 178 estimator.SimulateNetworkChangeTo( | |
| 179 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); | |
| 180 EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 181 | |
| 182 // Read the network quality for (3G, "test1"). | |
| 183 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 184 EXPECT_EQ(2U, estimator.peak_kbps_since_last_connection_change_); | |
| 185 EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), | |
| 186 estimator.fastest_RTT_since_last_connection_change_); | |
| 187 // No new entry should be added for (3G, "test1") since it already exists | |
| 188 // in the cache. | |
| 189 estimator.SimulateNetworkChangeTo( | |
| 190 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); | |
| 191 EXPECT_EQ(expected_cache_size, estimator.GetCacheSizeForTests()); | |
| 192 | |
| 193 // Reading quality of (3G, "test2") should return true. | |
| 194 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 195 EXPECT_EQ(0U, estimator.peak_kbps_since_last_connection_change_); | |
| 196 EXPECT_EQ(base::TimeDelta(), | |
| 197 estimator.fastest_RTT_since_last_connection_change_); | |
| 198 | |
| 199 // Reading quality of (2G, "test-3") should return false. | |
| 200 estimator.SimulateNetworkChangeTo( | |
| 201 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-3"); | |
| 202 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate()); | |
| 203 } | |
| 204 | |
| 205 // Tests if the cache size remains bounded. Also, ensure that the cache is | |
| 206 // LRU. | |
| 207 TEST(NetworkQualityEstimatorTest, TestLRUCacheMaximumSize) { | |
| 208 TestNetworkQualityEstimator estimator; | |
| 209 estimator.SimulateNetworkChangeTo( | |
| 210 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); | |
| 211 EXPECT_EQ(1U, estimator.GetCacheSizeForTests()); | |
| 212 | |
| 213 char network_name[20]; | |
| 214 // Add 100 more networks than the maximum size of the cache. | |
| 215 size_t network_count = | |
| 216 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; | |
| 217 | |
| 218 for (size_t i = 1; i <= network_count; ++i) { | |
| 219 base::strings::SafeSPrintf(network_name, "%d", i); | |
| 220 estimator.SimulateNetworkChangeTo( | |
| 221 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); | |
| 222 EXPECT_LE(estimator.GetCacheSizeForTests(), | |
| 223 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) | |
| 224 << i; | |
| 225 } | |
| 226 // One more call so that the last network is also written to cache. | |
| 227 estimator.SimulateNetworkChangeTo( | |
| 228 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); | |
| 229 | |
| 230 EXPECT_EQ(NetworkQualityEstimator::kMaximumNetworkQualityCacheSize, | |
| 231 estimator.GetCacheSizeForTests()); | |
| 232 | |
| 233 // Test that the cache is LRU by examining its contents. | |
| 234 for (size_t i = 1; i <= network_count; ++i) { | |
| 235 // The first 100 networks should not be present in the cache. | |
| 236 bool expect_present_in_cache = i >= 101; | |
| 237 base::strings::SafeSPrintf(network_name, "%d", i); | |
| 238 | |
| 239 NetworkQualityEstimator::NetworkID network_id( | |
| 240 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, network_name); | |
| 241 | |
| 242 bool found = estimator.cached_network_quality_.find(network_id) != | |
| 243 estimator.cached_network_quality_.end(); | |
| 244 EXPECT_EQ(expect_present_in_cache, found) << i; | |
| 245 } | |
| 246 } | |
| 247 | |
| 100 } // namespace net | 248 } // namespace net |
| OLD | NEW |