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" | |
| 8 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/safe_sprintf.h" | |
| 10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
| 11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "net/base/network_change_notifier.h" | 14 #include "net/base/network_change_notifier.h" |
| 15 #include "net/base/network_quality.h" | 15 #include "net/base/network_quality.h" |
| 16 #include "net/test/spawned_test_server/spawned_test_server.h" | 16 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 } | 65 } |
| 66 | 66 |
| 67 // With large transfer, both |fastest_rtt| and |peak_throughput_kbps| will be | 67 // With large transfer, both |fastest_rtt| and |peak_throughput_kbps| will be |
| 68 // updated. | 68 // updated. |
| 69 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes); | 69 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes); |
| 70 { | 70 { |
| 71 NetworkQuality network_quality = estimator.GetEstimate(); | 71 NetworkQuality network_quality = estimator.GetEstimate(); |
| 72 EXPECT_GT(network_quality.fastest_rtt_confidence, 0); | 72 EXPECT_GT(network_quality.fastest_rtt_confidence, 0); |
| 73 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); | 73 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); |
| 74 EXPECT_GE(network_quality.fastest_rtt, request_duration); | 74 EXPECT_GE(network_quality.fastest_rtt, request_duration); |
| 75 EXPECT_GT(network_quality.peak_throughput_kbps, uint32(0)); | 75 EXPECT_GT(network_quality.peak_throughput_kbps, 0U); |
| 76 EXPECT_LE( | 76 EXPECT_LE( |
| 77 network_quality.peak_throughput_kbps, | 77 network_quality.peak_throughput_kbps, |
| 78 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); | 78 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); |
| 79 } | 79 } |
| 80 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true); | 80 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true); |
| 81 | 81 |
| 82 // Check UMA histograms. | 82 // Check UMA histograms. |
| 83 base::HistogramTester histogram_tester; | 83 base::HistogramTester histogram_tester; |
| 84 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 84 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 85 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 85 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 86 | 86 |
| 87 estimator.OnConnectionTypeChanged( | 87 estimator.OnConnectionTypeChanged( |
| 88 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 88 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 89 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 89 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 90 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 90 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 91 { | 91 { |
| 92 NetworkQuality network_quality = estimator.GetEstimate(); | 92 NetworkQuality network_quality = estimator.GetEstimate(); |
| 93 EXPECT_EQ(estimator.current_connection_type_, | 93 EXPECT_EQ(estimator.current_connection_type_, |
| 94 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | 94 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 95 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0); | 95 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0); |
| 96 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0); | 96 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 #endif // !defined(OS_IOS) | 99 #endif // !defined(OS_IOS) |
| 100 | 100 |
| 101 // Helps in setting the current network name. | |
| 102 class TestNetworkQualityEstimator : public NetworkQualityEstimator { | |
| 103 public: | |
| 104 TestNetworkQualityEstimator() : current_network_name_(std::string()) {} | |
| 105 | |
| 106 ~TestNetworkQualityEstimator() override {} | |
| 107 | |
| 108 void SetCurrentNetworkName(std::string current_network_name) { | |
| 109 current_network_name_ = current_network_name; | |
| 110 NetworkQualityEstimator::SetCurrentNetworkNameForTests( | |
| 111 current_network_name_); | |
| 112 } | |
| 113 | |
| 114 using NetworkQualityEstimator::GetCacheSizeForTests; | |
| 115 using NetworkQualityEstimator::OnConnectionTypeChanged; | |
| 116 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | |
| 117 | |
| 118 private: | |
| 119 // NetworkQualityEstimator implementation that updates the current network | |
| 120 // name (instead of invoking platform APIs). | |
| 121 void UpdateCurrentNetworkName() override { | |
| 122 NetworkQualityEstimator::SetCurrentNetworkNameForTests( | |
|
bengr
2015/05/29 17:38:10
It would be better, maybe, if you could use a test
tbansal1
2015/05/29 19:13:37
NCN only exposes ConnectionType and not
the SSID
bengr
2015/06/01 20:39:34
Uggh. Could you configure NQE to register to liste
tbansal1
2015/06/01 21:56:51
Not sure....NCN has nothing to do with SSID name o
bengr
2015/06/01 23:21:10
My mistake. I guess what you have is fine.
tbansal1
2015/06/02 18:18:05
Done.
| |
| 123 current_network_name_); | |
| 124 } | |
| 125 | |
| 126 std::string current_network_name_; | |
| 127 }; | |
| 128 | |
| 129 // Test if the network estimates are cached when network change notification | |
| 130 // is invoked. | |
| 131 TEST(NetworkQualityEstimatorTest, TestCaching) { | |
| 132 TestNetworkQualityEstimator estimator; | |
| 133 EXPECT_EQ(0U, estimator.GetCacheSizeForTests()); | |
| 134 | |
| 135 uint32_t cache_size = 0; | |
|
bengr
2015/05/29 17:38:10
Chromium style guide says: Do not use unsigned typ
tbansal1
2015/05/29 19:13:37
Changed to size_t (instead of int to avoid repeate
| |
| 136 estimator.OnConnectionTypeChanged( | |
| 137 NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | |
| 138 EXPECT_EQ(0U, estimator.GetCacheSizeForTests()); | |
| 139 for (size_t i = 1; i <= 10; ++i) { | |
| 140 // Calling the loop multiple times should have no effect on cache size as | |
| 141 // the same cache entry should get updated (because the network names do | |
| 142 // not change with multiple iterations of the loop). | |
| 143 estimator.SetCurrentNetworkName("test-1"); | |
| 144 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i; | |
| 145 estimator.OnConnectionTypeChanged( | |
| 146 NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | |
| 147 if (i == 1) | |
| 148 ++cache_size; | |
| 149 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i; | |
| 150 | |
| 151 estimator.SetCurrentNetworkName("test-2"); | |
| 152 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i; | |
| 153 estimator.OnConnectionTypeChanged( | |
| 154 NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | |
| 155 if (i == 1) | |
| 156 ++cache_size; | |
| 157 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i; | |
| 158 } | |
| 159 | |
| 160 // Reading quality of a networks seen before should return true. | |
| 161 estimator.SetCurrentNetworkName("test-1"); | |
| 162 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); | |
| 163 estimator.SetCurrentNetworkName("test-2"); | |
| 164 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); | |
| 165 | |
| 166 // Reading quality of a network never seen should return false. | |
| 167 estimator.SetCurrentNetworkName("test-3"); | |
| 168 EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate()); | |
| 169 } | |
| 170 | |
| 171 // Tests if the cache size remains bounded. | |
| 172 TEST(NetworkQualityEstimatorTest, TestCacheMaximumSize) { | |
| 173 TestNetworkQualityEstimator estimator; | |
| 174 estimator.OnConnectionTypeChanged( | |
| 175 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | |
| 176 EXPECT_EQ(0U, estimator.GetCacheSizeForTests()); | |
| 177 | |
| 178 char network_name[20]; | |
| 179 for (size_t i = 1; i <= 10; ++i) { | |
| 180 base::strings::SafeSPrintf(network_name, "%d", i); | |
| 181 estimator.SetCurrentNetworkName(network_name); | |
| 182 estimator.OnConnectionTypeChanged( | |
| 183 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); | |
| 184 } | |
| 185 EXPECT_LT(estimator.GetCacheSizeForTests(), 10U); | |
| 186 EXPECT_GT(estimator.GetCacheSizeForTests(), 0U); | |
| 187 | |
| 188 // First network should be evicted from the cache. | |
| 189 base::strings::SafeSPrintf(network_name, "%d", 1); | |
| 190 estimator.SetCurrentNetworkName(network_name); | |
| 191 EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate()); | |
| 192 | |
| 193 // Last network should not be evicted from the cache. | |
| 194 base::strings::SafeSPrintf(network_name, "%d", 10); | |
| 195 estimator.SetCurrentNetworkName(network_name); | |
| 196 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate()); | |
| 197 } | |
| 198 | |
| 101 } // namespace net | 199 } // namespace net |
| OLD | NEW |