Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(560)

Side by Side Diff: net/base/network_quality_estimator_unittest.cc

Issue 1144163008: Add in-memory caching of network quality estimates across network changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using map instead of vector, added NetworkID struct Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 } 65 }
65 66
66 // 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
67 // updated. 68 // updated.
68 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes); 69 estimator.NotifyDataReceived(*(request.get()), min_transfer_size_in_bytes);
69 { 70 {
70 NetworkQuality network_quality = estimator.GetEstimate(); 71 NetworkQuality network_quality = estimator.GetEstimate();
71 EXPECT_GT(network_quality.fastest_rtt_confidence, 0); 72 EXPECT_GT(network_quality.fastest_rtt_confidence, 0);
72 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); 73 EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0);
73 EXPECT_GE(network_quality.fastest_rtt, request_duration); 74 EXPECT_GE(network_quality.fastest_rtt, request_duration);
74 EXPECT_GT(network_quality.peak_throughput_kbps, uint32_t(0)); 75 EXPECT_GT(network_quality.peak_throughput_kbps, 0U);
75 EXPECT_LE( 76 EXPECT_LE(
76 network_quality.peak_throughput_kbps, 77 network_quality.peak_throughput_kbps,
77 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds()); 78 min_transfer_size_in_bytes * 8.0 / request_duration.InMilliseconds());
78 } 79 }
79 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true); 80 EXPECT_EQ(estimator.bytes_read_since_last_connection_change_, true);
80 81
81 // Check UMA histograms. 82 // Check UMA histograms.
82 base::HistogramTester histogram_tester; 83 base::HistogramTester histogram_tester;
83 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); 84 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
84 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); 85 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0);
85 86
86 estimator.OnConnectionTypeChanged( 87 estimator.OnConnectionTypeChanged(
87 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); 88 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
88 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); 89 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1);
89 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); 90 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
90 { 91 {
91 NetworkQuality network_quality = estimator.GetEstimate(); 92 NetworkQuality network_quality = estimator.GetEstimate();
92 EXPECT_EQ(estimator.current_connection_type_, 93 EXPECT_EQ(estimator.current_network_id_.type,
93 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); 94 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
94 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0); 95 EXPECT_EQ(network_quality.fastest_rtt_confidence, 0);
95 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0); 96 EXPECT_EQ(network_quality.peak_throughput_kbps_confidence, 0);
96 } 97 }
97 } 98 }
98 #endif // !defined(OS_IOS) 99 #endif // !defined(OS_IOS)
99 100
101 // Helps in setting the current network name.
102 class TestNetworkQualityEstimator : public NetworkQualityEstimator {
mmenke 2015/06/09 19:48:07 This should be in an anonymous namespace. May be
tbansal1 2015/06/10 00:23:29 Moved to anon namespace. Not sure what test can I
103 public:
104 TestNetworkQualityEstimator() : current_network_name_(std::string()) {}
mmenke 2015/06/09 19:48:07 nit: No need to explicitly initialize current_net
tbansal1 2015/06/10 00:23:29 Done.
105
106 ~TestNetworkQualityEstimator() override {}
107
108 // Overrides the current network name.
109 void SetCurrentNetworkName(std::string current_network_name) {
110 current_network_name_ = current_network_name;
111 }
112
113 using NetworkQualityEstimator::GetCacheSizeForTests;
114 using NetworkQualityEstimator::OnConnectionTypeChanged;
115 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
116
117 private:
118 // NetworkQualityEstimator implementation that returns the overriden network
mmenke 2015/06/09 19:48:08 overriden -> overridden
tbansal1 2015/06/10 00:23:29 Done.
119 // name (instead of invoking platform APIs).
120 std::string GetCurrentNetworkName() const override {
121 return current_network_name_;
122 }
123
124 std::string current_network_name_;
125 };
126
127 // Test if the network estimates are cached when network change notification
128 // is invoked.
129 TEST(NetworkQualityEstimatorTest, TestCaching) {
130 TestNetworkQualityEstimator estimator;
131 EXPECT_EQ(0U, estimator.GetCacheSizeForTests());
132
133 size_t cache_size = 1;
mmenke 2015/06/09 19:48:07 expected_cache_size
tbansal1 2015/06/10 00:23:29 Done.
134 estimator.SetCurrentNetworkName("test-1");
135 estimator.OnConnectionTypeChanged(
136 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
mmenke 2015/06/09 19:48:07 Think this test would be much clearer as a single
tbansal1 2015/06/10 00:23:29 Thanks, this helped a lot.
137 // Cache entry will be added for (NONE, "")
mmenke 2015/06/09 19:48:07 Suggest putting this above the line that actually
tbansal1 2015/06/10 00:23:29 Done.
138 EXPECT_EQ(cache_size, 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).
mmenke 2015/06/09 19:48:08 Not true - the second iteration adds an entry. I
tbansal1 2015/06/10 00:23:29 Tried to make it simpler. Removed the for loop.
143 estimator.SetCurrentNetworkName("test-1");
144 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i;
145 estimator.OnConnectionTypeChanged(
146 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
147
148 // When i == 1, entry will be added for (2G, "test1")
149 // When i == 2, entry will be added for (2G, "test2")
150 if (i == 1 || i == 2)
151 ++cache_size;
152 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i;
153
154 estimator.SetCurrentNetworkName("test-2");
155 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i;
156 estimator.OnConnectionTypeChanged(
157 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
mmenke 2015/06/09 19:48:08 Should also make a test-1 connection with type 3G,
tbansal1 2015/06/10 00:23:29 Done.
158 EXPECT_EQ(cache_size, estimator.GetCacheSizeForTests()) << i;
159 }
160
161 // Reading quality of a networks seen before should return true.
162 estimator.SetCurrentNetworkName("test-1");
mmenke 2015/06/09 19:48:07 BUG: This doesn't actually change current_network
tbansal1 2015/06/10 00:23:29 Fixed.
163 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
164 estimator.SetCurrentNetworkName("test-2");
165 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
mmenke 2015/06/09 19:48:07 This seems like a very weak check that something w
tbansal1 2015/06/10 00:23:29 Done.
166
167 // Reading quality of a network never seen should return false.
168 estimator.SetCurrentNetworkName("test-3");
169 estimator.OnConnectionTypeChanged(
170 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
171 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate());
172 }
173
174 // Tests if the cache size remains bounded.
175 TEST(NetworkQualityEstimatorTest, TestCacheMaximumSize) {
176 TestNetworkQualityEstimator estimator;
177 estimator.OnConnectionTypeChanged(
178 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
179 EXPECT_EQ(1U, estimator.GetCacheSizeForTests());
180
181 char network_name[20];
182 const size_t network_count = 100;
183 for (size_t i = 1; i <= network_count; ++i) {
184 base::strings::SafeSPrintf(network_name, "%d", i);
185 estimator.SetCurrentNetworkName(network_name);
186 estimator.OnConnectionTypeChanged(
187 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
188 }
mmenke 2015/06/09 19:48:07 Suggest exposing NetworkQualityEstimator::kMaximum
tbansal1 2015/06/10 00:23:29 Done.
189 EXPECT_LT(estimator.GetCacheSizeForTests(), network_count);
190 EXPECT_GT(estimator.GetCacheSizeForTests(), 0U);
191
192 // First network should be evicted from the cache.
193 base::strings::SafeSPrintf(network_name, "%d", 1);
194 estimator.SetCurrentNetworkName(network_name);
195 estimator.OnConnectionTypeChanged(
196 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
197 EXPECT_FALSE(estimator.ReadCachedNetworkQualityEstimate());
198
199 // Last network should not be evicted from the cache.
200 base::strings::SafeSPrintf(network_name, "%d", network_count);
201 estimator.SetCurrentNetworkName(network_name);
202 estimator.OnConnectionTypeChanged(
203 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
204 EXPECT_TRUE(estimator.ReadCachedNetworkQualityEstimate());
205 }
206
100 } // namespace net 207 } // namespace net
OLDNEW
« net/base/network_quality_estimator.cc ('K') | « net/base/network_quality_estimator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698