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

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: Minor modifications to comments and code 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"
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
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 NetworkQualityEstimatorTest : public NetworkQualityEstimator {
mmenke 2015/05/28 15:25:03 This name is weird: This is the same name as you
tbansal1 2015/05/29 02:27:24 Done.
103 public:
104 NetworkQualityEstimatorTest() : NetworkQualityEstimator() {}
mmenke 2015/05/28 15:25:03 NetworkQualityEstimator() is not needed.
tbansal1 2015/05/29 02:27:24 Done.
105
106 // Overwrite the current network name for testing.
107 void SetCurrentNetworkName(std::string network_name) {
108 current_network_name_ = network_name;
109 }
110
111 std::string GetCurrentNetworkName() const override {
mmenke 2015/05/28 15:25:03 Should group the overridden methods with a comment
tbansal1 2015/05/29 02:27:24 Done.
112 return current_network_name_;
113 }
114
115 bool ReadCachedNetworkQualityEstimate() {
116 return NetworkQualityEstimator::ReadCachedNetworkQualityEstimate();
117 }
118
119 uint32_t GetCacheSize() const { return cached_network_quality_.size(); }
120
121 void OnConnectionTypeChanged(
122 NetworkChangeNotifier::ConnectionType connection_type) override {
123 NetworkQualityEstimator::OnConnectionTypeChanged(connection_type);
124 }
125 };
126
127 // Test if the network estimates are cached.
128 TEST(NetworkQualityEstimatorTest, TestCaching) {
129 NetworkQualityEstimatorTest estimator;
130 EXPECT_EQ(0U, estimator.GetCacheSize());
131
132 uint32_t cache_size = 0;
133 estimator.OnConnectionTypeChanged(
134 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
135 EXPECT_EQ(0U, estimator.GetCacheSize());
136 for (size_t i = 1; i <= 10; ++i) {
137 // Calling the loop multiple times should have no effect on cache size as
138 // the same cache entry should get updated (because the network name does
139 // not change with multiple iterations of the loop).
140 estimator.SetCurrentNetworkName("test-1");
141 estimator.OnConnectionTypeChanged(
142 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
143 // Network should be added to the cache exactly once.
144 if (i == 1)
145 ++cache_size;
146 EXPECT_EQ(cache_size, estimator.GetCacheSize()) << i;
147 }
148
149 estimator.SetCurrentNetworkName("test-2");
150
151 for (size_t i = 1; i <= 10; ++i) {
152 // Calling the loop multiple times should have no effect on cache size as
153 // the same cache entry should get updated (because the network name does
154 // not change with multiple iterations of the loop).
155 estimator.OnConnectionTypeChanged(
156 NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
157 // Network should be added to the cache exactly once.
158 if (i == 1)
159 ++cache_size;
160 EXPECT_EQ(cache_size, estimator.GetCacheSize()) << i;
161 }
162
163 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate());
164 // Reading quality of a network that has been seen before, should return true.
165 estimator.SetCurrentNetworkName("test-1");
166 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate());
167
168 // Reading quality of a network that has never been seen should return false.
169 estimator.SetCurrentNetworkName("test-3");
170 EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate());
171 }
172
173 // Tests if the cache size remains bounded.
174 TEST(NetworkQualityEstimatorTest, TestCacheMaximumSize) {
175 NetworkQualityEstimatorTest estimator;
176 estimator.OnConnectionTypeChanged(
177 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
178 EXPECT_EQ(0U, estimator.GetCacheSize());
179
180 char network_name[20];
181 for (size_t i = 1; i <= 10; ++i) {
182 base::strings::SafeSPrintf(network_name, "%d", i);
183 estimator.SetCurrentNetworkName(network_name);
184 estimator.OnConnectionTypeChanged(
185 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
186 }
187 EXPECT_LT(estimator.GetCacheSize(), 10U);
188 EXPECT_GT(estimator.GetCacheSize(), 0U);
189
190 // First network should be evicted from the cache.
191 base::strings::SafeSPrintf(network_name, "%d", 1);
192 estimator.SetCurrentNetworkName(network_name);
193 EXPECT_EQ(false, estimator.ReadCachedNetworkQualityEstimate());
194
195 // Last network should not be evicted from the cache.
196 base::strings::SafeSPrintf(network_name, "%d", 10);
197 estimator.SetCurrentNetworkName(network_name);
198 EXPECT_EQ(true, estimator.ReadCachedNetworkQualityEstimate());
199 }
200
101 } // namespace net 201 } // 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