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

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

Issue 1942893002: Split NQE to multiple files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix tests on Windows Created 4 years, 7 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/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>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/metrics/histogram_samples.h" 20 #include "base/metrics/histogram_samples.h"
21 #include "base/run_loop.h" 21 #include "base/run_loop.h"
22 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
23 #include "base/test/histogram_tester.h" 23 #include "base/test/histogram_tester.h"
24 #include "base/time/time.h" 24 #include "base/time/time.h"
25 #include "build/build_config.h" 25 #include "build/build_config.h"
26 #include "net/base/load_flags.h" 26 #include "net/base/load_flags.h"
27 #include "net/base/network_change_notifier.h" 27 #include "net/base/network_change_notifier.h"
28 #include "net/http/http_status_code.h" 28 #include "net/http/http_status_code.h"
29 #include "net/nqe/external_estimate_provider.h" 29 #include "net/nqe/external_estimate_provider.h"
30 #include "net/nqe/network_quality_observation.h"
31 #include "net/nqe/network_quality_observation_source.h"
32 #include "net/nqe/observation_buffer.h"
30 #include "net/socket/socket_performance_watcher.h" 33 #include "net/socket/socket_performance_watcher.h"
31 #include "net/socket/socket_performance_watcher_factory.h" 34 #include "net/socket/socket_performance_watcher_factory.h"
32 #include "net/test/embedded_test_server/embedded_test_server.h" 35 #include "net/test/embedded_test_server/embedded_test_server.h"
33 #include "net/test/embedded_test_server/http_request.h" 36 #include "net/test/embedded_test_server/http_request.h"
34 #include "net/test/embedded_test_server/http_response.h" 37 #include "net/test/embedded_test_server/http_response.h"
35 #include "net/url_request/url_request.h" 38 #include "net/url_request/url_request.h"
36 #include "net/url_request/url_request_test_util.h" 39 #include "net/url_request/url_request_test_util.h"
37 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
38 #include "url/gurl.h" 41 #include "url/gurl.h"
39 42
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 EmbeddedTestServer embedded_test_server_; 147 EmbeddedTestServer embedded_test_server_;
145 148
146 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 149 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
147 }; 150 };
148 151
149 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { 152 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver {
150 public: 153 public:
151 struct Observation { 154 struct Observation {
152 Observation(int32_t ms, 155 Observation(int32_t ms,
153 const base::TimeTicks& ts, 156 const base::TimeTicks& ts,
154 NetworkQualityEstimator::ObservationSource src) 157 NetworkQualityObservationSource src)
155 : rtt_ms(ms), timestamp(ts), source(src) {} 158 : rtt_ms(ms), timestamp(ts), source(src) {}
156 int32_t rtt_ms; 159 int32_t rtt_ms;
157 base::TimeTicks timestamp; 160 base::TimeTicks timestamp;
158 NetworkQualityEstimator::ObservationSource source; 161 NetworkQualityObservationSource source;
159 }; 162 };
160 163
161 std::vector<Observation>& observations() { return observations_; } 164 std::vector<Observation>& observations() { return observations_; }
162 165
163 // RttObserver implementation: 166 // RttObserver implementation:
164 void OnRTTObservation( 167 void OnRTTObservation(int32_t rtt_ms,
165 int32_t rtt_ms, 168 const base::TimeTicks& timestamp,
166 const base::TimeTicks& timestamp, 169 NetworkQualityObservationSource source) override {
167 NetworkQualityEstimator::ObservationSource source) override {
168 observations_.push_back(Observation(rtt_ms, timestamp, source)); 170 observations_.push_back(Observation(rtt_ms, timestamp, source));
169 } 171 }
170 172
171 private: 173 private:
172 std::vector<Observation> observations_; 174 std::vector<Observation> observations_;
173 }; 175 };
174 176
175 class TestThroughputObserver 177 class TestThroughputObserver
176 : public NetworkQualityEstimator::ThroughputObserver { 178 : public NetworkQualityEstimator::ThroughputObserver {
177 public: 179 public:
178 struct Observation { 180 struct Observation {
179 Observation(int32_t kbps, 181 Observation(int32_t kbps,
180 const base::TimeTicks& ts, 182 const base::TimeTicks& ts,
181 NetworkQualityEstimator::ObservationSource src) 183 NetworkQualityObservationSource src)
182 : throughput_kbps(kbps), timestamp(ts), source(src) {} 184 : throughput_kbps(kbps), timestamp(ts), source(src) {}
183 int32_t throughput_kbps; 185 int32_t throughput_kbps;
184 base::TimeTicks timestamp; 186 base::TimeTicks timestamp;
185 NetworkQualityEstimator::ObservationSource source; 187 NetworkQualityObservationSource source;
186 }; 188 };
187 189
188 std::vector<Observation>& observations() { return observations_; } 190 std::vector<Observation>& observations() { return observations_; }
189 191
190 // ThroughputObserver implementation: 192 // ThroughputObserver implementation:
191 void OnThroughputObservation( 193 void OnThroughputObservation(
192 int32_t throughput_kbps, 194 int32_t throughput_kbps,
193 const base::TimeTicks& timestamp, 195 const base::TimeTicks& timestamp,
194 NetworkQualityEstimator::ObservationSource source) override { 196 NetworkQualityObservationSource source) override {
195 observations_.push_back(Observation(throughput_kbps, timestamp, source)); 197 observations_.push_back(Observation(throughput_kbps, timestamp, source));
196 } 198 }
197 199
198 private: 200 private:
199 std::vector<Observation> observations_; 201 std::vector<Observation> observations_;
200 }; 202 };
201 203
202 } // namespace 204 } // namespace
203 205
204 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { 206 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) {
(...skipping 15 matching lines...) Expand all
220 std::unique_ptr<URLRequest> request(context.CreateRequest( 222 std::unique_ptr<URLRequest> request(context.CreateRequest(
221 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); 223 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
222 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); 224 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME);
223 request->Start(); 225 request->Start();
224 base::RunLoop().Run(); 226 base::RunLoop().Run();
225 227
226 // Both RTT and downstream throughput should be updated. 228 // Both RTT and downstream throughput should be updated.
227 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); 229 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
228 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 230 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
229 231
230 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
231 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
232
233 // Check UMA histograms. 232 // Check UMA histograms.
234 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); 233 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
235 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); 234 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0);
236 235
237 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0); 236 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0);
238 237
239 std::unique_ptr<URLRequest> request2(context.CreateRequest( 238 std::unique_ptr<URLRequest> request2(context.CreateRequest(
240 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); 239 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
241 request2->SetLoadFlags(request2->load_flags() | LOAD_MAIN_FRAME); 240 request2->SetLoadFlags(request2->load_flags() | LOAD_MAIN_FRAME);
242 request2->Start(); 241 request2->Start();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 int32_t kbps; 275 int32_t kbps;
277 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); 276 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt));
278 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 277 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
279 278
280 TestDelegate test_delegate; 279 TestDelegate test_delegate;
281 TestURLRequestContext context(true); 280 TestURLRequestContext context(true);
282 context.set_network_quality_estimator(&estimator); 281 context.set_network_quality_estimator(&estimator);
283 context.Init(); 282 context.Init();
284 283
285 // Push 10 more observations than the maximum buffer size. 284 // Push 10 more observations than the maximum buffer size.
286 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 10U; ++i) { 285 for (size_t i = 0;
286 i < estimator.downstream_throughput_kbps_observations_.Capacity() + 10U;
287 ++i) {
287 std::unique_ptr<URLRequest> request(context.CreateRequest( 288 std::unique_ptr<URLRequest> request(context.CreateRequest(
288 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); 289 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
289 request->Start(); 290 request->Start();
290 base::RunLoop().Run(); 291 base::RunLoop().Run();
291 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); 292 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
292 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 293 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
293 } 294 }
294 295
296 EXPECT_EQ(static_cast<size_t>(
297 estimator.downstream_throughput_kbps_observations_.Capacity()),
298 static_cast<size_t>(
299 estimator.downstream_throughput_kbps_observations_.Size()));
300 EXPECT_EQ(static_cast<size_t>(estimator.rtt_observations_.Capacity()),
301 static_cast<size_t>(estimator.rtt_observations_.Size()));
302
295 // Verify that the stored observations are cleared on network change. 303 // Verify that the stored observations are cleared on network change.
296 estimator.SimulateNetworkChangeTo( 304 estimator.SimulateNetworkChangeTo(
297 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2"); 305 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2");
298 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); 306 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt));
299 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 307 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
300 } 308 }
301 309
302 // Verifies that the percentiles are correctly computed. All observations have
303 // the same timestamp. Kbps percentiles must be in decreasing order. RTT
304 // percentiles must be in increasing order.
305 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) {
306 std::map<std::string, std::string> variation_params;
307 TestNetworkQualityEstimator estimator(variation_params);
308 base::TimeTicks now = base::TimeTicks::Now();
309
310 // Network quality should be unavailable when no observations are available.
311 base::TimeDelta rtt;
312 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt));
313 int32_t kbps;
314 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
315
316 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even
317 // samples. This helps in verifying that the order of samples does not matter.
318 for (int i = 1; i <= 99; i += 2) {
319 estimator.downstream_throughput_kbps_observations_.AddObservation(
320 NetworkQualityEstimator::ThroughputObservation(
321 i, now, NetworkQualityEstimator::URL_REQUEST));
322 estimator.rtt_observations_.AddObservation(
323 NetworkQualityEstimator::RttObservation(
324 base::TimeDelta::FromMilliseconds(i), now,
325 NetworkQualityEstimator::URL_REQUEST));
326 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
327 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
328 }
329
330 for (int i = 1; i <= 99; i += 2) {
331 // Insert TCP observation which should not be taken into account when
332 // computing median RTT at HTTP layer.
333 estimator.rtt_observations_.AddObservation(
334 NetworkQualityEstimator::RttObservation(
335 base::TimeDelta::FromMilliseconds(10000), now,
336 NetworkQualityEstimator::TCP));
337
338 // Insert QUIC observation which should not be taken into account when
339 // computing median RTT at HTTP layer.
340 estimator.rtt_observations_.AddObservation(
341 NetworkQualityEstimator::RttObservation(
342 base::TimeDelta::FromMilliseconds(10000), now,
343 NetworkQualityEstimator::QUIC));
344 }
345
346 for (int i = 2; i <= 100; i += 2) {
347 estimator.downstream_throughput_kbps_observations_.AddObservation(
348 NetworkQualityEstimator::ThroughputObservation(
349 i, now, NetworkQualityEstimator::URL_REQUEST));
350 estimator.rtt_observations_.AddObservation(
351 NetworkQualityEstimator::RttObservation(
352 base::TimeDelta::FromMilliseconds(i), now,
353 NetworkQualityEstimator::URL_REQUEST));
354 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
355 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
356 }
357
358 for (int i = 0; i <= 100; ++i) {
359 // Checks if the difference between the two integers is less than 1. This is
360 // required because computed percentiles may be slightly different from
361 // what is expected due to floating point computation errors and integer
362 // rounding off errors.
363 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal(
364 base::TimeTicks(), i),
365 100 - i, 1);
366 std::vector<NetworkQualityEstimator::ObservationSource>
367 disallowed_observation_sources;
368 disallowed_observation_sources.push_back(NetworkQualityEstimator::TCP);
369 disallowed_observation_sources.push_back(NetworkQualityEstimator::QUIC);
370 EXPECT_NEAR(estimator
371 .GetRTTEstimateInternal(disallowed_observation_sources,
372 base::TimeTicks(), i)
373 .InMilliseconds(),
374 i, 1);
375 }
376
377 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
378 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
379 }
380
381 // Verifies that the percentiles are correctly computed. Observations have
382 // different timestamps with half the observations being very old and the rest
383 // of them being very recent. Percentiles should factor in recent observations
384 // much more heavily than older samples. Kbps percentiles must be in decreasing
385 // order. RTT percentiles must be in increasing order.
386 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) {
387 std::map<std::string, std::string> variation_params;
388 TestNetworkQualityEstimator estimator(variation_params);
389 base::TimeTicks now = base::TimeTicks::Now();
390 base::TimeTicks very_old = now - base::TimeDelta::FromDays(365);
391
392 // First 50 samples have very old timestamp.
393 for (int i = 1; i <= 50; ++i) {
394 estimator.downstream_throughput_kbps_observations_.AddObservation(
395 NetworkQualityEstimator::ThroughputObservation(
396 i, very_old, NetworkQualityEstimator::URL_REQUEST));
397 estimator.rtt_observations_.AddObservation(
398 NetworkQualityEstimator::RttObservation(
399 base::TimeDelta::FromMilliseconds(i), very_old,
400 NetworkQualityEstimator::URL_REQUEST));
401 }
402
403 // Next 50 (i.e., from 51 to 100) have recent timestamp.
404 for (int i = 51; i <= 100; ++i) {
405 estimator.downstream_throughput_kbps_observations_.AddObservation(
406 NetworkQualityEstimator::ThroughputObservation(
407 i, now, NetworkQualityEstimator::URL_REQUEST));
408
409 // Insert TCP observation which should not be taken into account when
410 // computing median RTT at HTTP layer.
411 estimator.rtt_observations_.AddObservation(
412 NetworkQualityEstimator::RttObservation(
413 base::TimeDelta::FromMilliseconds(10000), now,
414 NetworkQualityEstimator::TCP));
415
416 estimator.rtt_observations_.AddObservation(
417 NetworkQualityEstimator::RttObservation(
418 base::TimeDelta::FromMilliseconds(i), now,
419 NetworkQualityEstimator::URL_REQUEST));
420 }
421
422 std::vector<NetworkQualityEstimator::ObservationSource>
423 disallowed_observation_sources;
424 disallowed_observation_sources.push_back(NetworkQualityEstimator::TCP);
425 disallowed_observation_sources.push_back(NetworkQualityEstimator::QUIC);
426
427 // Older samples have very little weight. So, all percentiles are >= 51
428 // (lowest value among recent observations).
429 for (int i = 1; i < 100; ++i) {
430 // Checks if the difference between the two integers is less than 1. This is
431 // required because computed percentiles may be slightly different from
432 // what is expected due to floating point computation errors and integer
433 // rounding off errors.
434 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal(
435 base::TimeTicks(), i),
436 51 + 0.49 * (100 - i), 1);
437 EXPECT_NEAR(estimator
438 .GetRTTEstimateInternal(disallowed_observation_sources,
439 base::TimeTicks(), i)
440 .InMilliseconds(),
441 51 + 0.49 * i, 1);
442 }
443 }
444
445 // This test notifies NetworkQualityEstimator of received data. Next, 310 // This test notifies NetworkQualityEstimator of received data. Next,
446 // throughput and RTT percentiles are checked for correctness by doing simple 311 // throughput and RTT percentiles are checked for correctness by doing simple
447 // verifications. 312 // verifications.
448 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { 313 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) {
449 std::map<std::string, std::string> variation_params; 314 std::map<std::string, std::string> variation_params;
450 TestNetworkQualityEstimator estimator(variation_params); 315 TestNetworkQualityEstimator estimator(variation_params);
451 316
452 std::vector<NetworkQualityEstimator::ObservationSource> 317 std::vector<NetworkQualityObservationSource> disallowed_observation_sources;
453 disallowed_observation_sources; 318 disallowed_observation_sources.push_back(
454 disallowed_observation_sources.push_back(NetworkQualityEstimator::TCP); 319 NETWORK_QUALITY_OBSERVATION_SOURCE_TCP);
455 disallowed_observation_sources.push_back(NetworkQualityEstimator::QUIC); 320 disallowed_observation_sources.push_back(
321 NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC);
456 322
457 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), 323 EXPECT_EQ(nqe::internal::InvalidRTT(),
458 estimator.GetRTTEstimateInternal(disallowed_observation_sources, 324 estimator.GetRTTEstimateInternal(disallowed_observation_sources,
459 base::TimeTicks(), 100)); 325 base::TimeTicks(), 100));
460 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, 326 EXPECT_EQ(nqe::internal::kInvalidThroughput,
461 estimator.GetDownlinkThroughputKbpsEstimateInternal( 327 estimator.GetDownlinkThroughputKbpsEstimateInternal(
462 base::TimeTicks(), 100)); 328 base::TimeTicks(), 100));
463 329
464 TestDelegate test_delegate; 330 TestDelegate test_delegate;
465 TestURLRequestContext context(true); 331 TestURLRequestContext context(true);
466 context.set_network_quality_estimator(&estimator); 332 context.set_network_quality_estimator(&estimator);
467 context.Init(); 333 context.Init();
468 334
469 // Number of observations are more than the maximum buffer size. 335 // Number of observations are more than the maximum buffer size.
470 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 100U; ++i) { 336 for (size_t i = 0; i < 1000U; ++i) {
471 std::unique_ptr<URLRequest> request(context.CreateRequest( 337 std::unique_ptr<URLRequest> request(context.CreateRequest(
472 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); 338 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
473 request->Start(); 339 request->Start();
474 base::RunLoop().Run(); 340 base::RunLoop().Run();
475 } 341 }
476 342
477 // Verify the percentiles through simple tests. 343 // Verify the percentiles through simple tests.
478 for (int i = 0; i <= 100; ++i) { 344 for (int i = 0; i <= 100; ++i) {
479 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal( 345 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal(
480 base::TimeTicks(), i), 346 base::TimeTicks(), i),
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 estimator.SimulateNetworkChangeTo( 390 estimator.SimulateNetworkChangeTo(
525 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); 391 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1");
526 392
527 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); 393 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
528 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 394 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
529 EXPECT_EQ(200, kbps); 395 EXPECT_EQ(200, kbps);
530 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt); 396 EXPECT_EQ(base::TimeDelta::FromMilliseconds(2000), rtt);
531 397
532 // Peak network quality should not be affected by the network quality 398 // Peak network quality should not be affected by the network quality
533 // estimator field trial. 399 // estimator field trial.
534 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), 400 EXPECT_EQ(nqe::internal::InvalidRTT(), estimator.peak_network_quality_.rtt());
535 estimator.peak_network_quality_.rtt()); 401 EXPECT_EQ(nqe::internal::kInvalidThroughput,
536 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput,
537 estimator.peak_network_quality_.downstream_throughput_kbps()); 402 estimator.peak_network_quality_.downstream_throughput_kbps());
538 403
539 // Simulate network change to 2G. Only the Kbps default estimate should be 404 // Simulate network change to 2G. Only the Kbps default estimate should be
540 // available. 405 // available.
541 estimator.SimulateNetworkChangeTo( 406 estimator.SimulateNetworkChangeTo(
542 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); 407 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2");
543 408
544 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); 409 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt));
545 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); 410 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
546 EXPECT_EQ(300, kbps); 411 EXPECT_EQ(300, kbps);
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 // is invoked. 565 // is invoked.
701 TEST(NetworkQualityEstimatorTest, TestCaching) { 566 TEST(NetworkQualityEstimatorTest, TestCaching) {
702 std::map<std::string, std::string> variation_params; 567 std::map<std::string, std::string> variation_params;
703 TestNetworkQualityEstimator estimator(variation_params); 568 TestNetworkQualityEstimator estimator(variation_params);
704 size_t expected_cache_size = 0; 569 size_t expected_cache_size = 0;
705 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); 570 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size());
706 571
707 // Cache entry will not be added for (NONE, ""). 572 // Cache entry will not be added for (NONE, "").
708 estimator.downstream_throughput_kbps_observations_.AddObservation( 573 estimator.downstream_throughput_kbps_observations_.AddObservation(
709 NetworkQualityEstimator::ThroughputObservation( 574 NetworkQualityEstimator::ThroughputObservation(
710 1, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); 575 1, base::TimeTicks::Now(),
576 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
711 estimator.rtt_observations_.AddObservation( 577 estimator.rtt_observations_.AddObservation(
712 NetworkQualityEstimator::RttObservation( 578 NetworkQualityEstimator::RttObservation(
713 base::TimeDelta::FromMilliseconds(1000), base::TimeTicks::Now(), 579 base::TimeDelta::FromMilliseconds(1000), base::TimeTicks::Now(),
714 NetworkQualityEstimator::URL_REQUEST)); 580 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
715 estimator.SimulateNetworkChangeTo( 581 estimator.SimulateNetworkChangeTo(
716 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); 582 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
717 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); 583 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size());
718 584
719 // Entry will be added for (2G, "test1"). 585 // Entry will be added for (2G, "test1").
720 // Also, set the network quality for (2G, "test1") so that it is stored in 586 // Also, set the network quality for (2G, "test1") so that it is stored in
721 // the cache. 587 // the cache.
722 estimator.downstream_throughput_kbps_observations_.AddObservation( 588 estimator.downstream_throughput_kbps_observations_.AddObservation(
723 NetworkQualityEstimator::ThroughputObservation( 589 NetworkQualityEstimator::ThroughputObservation(
724 1, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); 590 1, base::TimeTicks::Now(),
591 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
725 estimator.rtt_observations_.AddObservation( 592 estimator.rtt_observations_.AddObservation(
726 NetworkQualityEstimator::RttObservation( 593 NetworkQualityEstimator::RttObservation(
727 base::TimeDelta::FromMilliseconds(1000), base::TimeTicks::Now(), 594 base::TimeDelta::FromMilliseconds(1000), base::TimeTicks::Now(),
728 NetworkQualityEstimator::URL_REQUEST)); 595 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
729 596
730 estimator.SimulateNetworkChangeTo( 597 estimator.SimulateNetworkChangeTo(
731 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); 598 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1");
732 ++expected_cache_size; 599 ++expected_cache_size;
733 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); 600 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size());
734 601
735 // Entry will be added for (3G, "test1"). 602 // Entry will be added for (3G, "test1").
736 // Also, set the network quality for (3G, "test1") so that it is stored in 603 // Also, set the network quality for (3G, "test1") so that it is stored in
737 // the cache. 604 // the cache.
738 estimator.downstream_throughput_kbps_observations_.AddObservation( 605 estimator.downstream_throughput_kbps_observations_.AddObservation(
739 NetworkQualityEstimator::ThroughputObservation( 606 NetworkQualityEstimator::ThroughputObservation(
740 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); 607 2, base::TimeTicks::Now(),
608 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
741 estimator.rtt_observations_.AddObservation( 609 estimator.rtt_observations_.AddObservation(
742 NetworkQualityEstimator::RttObservation( 610 NetworkQualityEstimator::RttObservation(
743 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), 611 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(),
744 NetworkQualityEstimator::URL_REQUEST)); 612 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
745 estimator.SimulateNetworkChangeTo( 613 estimator.SimulateNetworkChangeTo(
746 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); 614 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2");
747 ++expected_cache_size; 615 ++expected_cache_size;
748 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); 616 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size());
749 617
750 // Entry will not be added for (3G, "test2"). 618 // Entry will not be added for (3G, "test2").
751 estimator.SimulateNetworkChangeTo( 619 estimator.SimulateNetworkChangeTo(
752 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); 620 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1");
753 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); 621 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size());
754 622
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); 666 EXPECT_EQ(0U, estimator.cached_network_qualities_.size());
799 667
800 // Add 100 more networks than the maximum size of the cache. 668 // Add 100 more networks than the maximum size of the cache.
801 size_t network_count = 669 size_t network_count =
802 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; 670 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100;
803 671
804 base::TimeTicks update_time_of_network_100; 672 base::TimeTicks update_time_of_network_100;
805 for (size_t i = 0; i < network_count; ++i) { 673 for (size_t i = 0; i < network_count; ++i) {
806 estimator.downstream_throughput_kbps_observations_.AddObservation( 674 estimator.downstream_throughput_kbps_observations_.AddObservation(
807 NetworkQualityEstimator::ThroughputObservation( 675 NetworkQualityEstimator::ThroughputObservation(
808 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); 676 2, base::TimeTicks::Now(),
677 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
809 estimator.rtt_observations_.AddObservation( 678 estimator.rtt_observations_.AddObservation(
810 NetworkQualityEstimator::RttObservation( 679 NetworkQualityEstimator::RttObservation(
811 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), 680 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(),
812 NetworkQualityEstimator::URL_REQUEST)); 681 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
813 682
814 if (i == 100) 683 if (i == 100)
815 update_time_of_network_100 = base::TimeTicks::Now(); 684 update_time_of_network_100 = base::TimeTicks::Now();
816 685
817 estimator.SimulateNetworkChangeTo( 686 estimator.SimulateNetworkChangeTo(
818 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, 687 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
819 base::SizeTToString(i)); 688 base::SizeTToString(i));
820 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) 689 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)
821 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); 690 EXPECT_EQ(i, estimator.cached_network_qualities_.size());
822 EXPECT_LE(estimator.cached_network_qualities_.size(), 691 EXPECT_LE(estimator.cached_network_qualities_.size(),
823 static_cast<size_t>( 692 static_cast<size_t>(
824 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); 693 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize));
825 } 694 }
826 // One more call so that the last network is also written to cache. 695 // One more call so that the last network is also written to cache.
827 estimator.downstream_throughput_kbps_observations_.AddObservation( 696 estimator.downstream_throughput_kbps_observations_.AddObservation(
828 NetworkQualityEstimator::ThroughputObservation( 697 NetworkQualityEstimator::ThroughputObservation(
829 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); 698 2, base::TimeTicks::Now(),
699 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
830 estimator.rtt_observations_.AddObservation( 700 estimator.rtt_observations_.AddObservation(
831 NetworkQualityEstimator::RttObservation( 701 NetworkQualityEstimator::RttObservation(
832 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(), 702 base::TimeDelta::FromMilliseconds(500), base::TimeTicks::Now(),
833 NetworkQualityEstimator::URL_REQUEST)); 703 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
834 estimator.SimulateNetworkChangeTo( 704 estimator.SimulateNetworkChangeTo(
835 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, 705 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI,
836 base::SizeTToString(network_count - 1)); 706 base::SizeTToString(network_count - 1));
837 EXPECT_EQ(static_cast<size_t>( 707 EXPECT_EQ(static_cast<size_t>(
838 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), 708 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize),
839 estimator.cached_network_qualities_.size()); 709 estimator.cached_network_qualities_.size());
840 710
841 // Test that the cache is LRU by examining its contents. Networks in cache 711 // Test that the cache is LRU by examining its contents. Networks in cache
842 // must all be newer than the 100th network. 712 // must all be newer than the 100th network.
843 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = 713 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it =
844 estimator.cached_network_qualities_.begin(); 714 estimator.cached_network_qualities_.begin();
845 it != estimator.cached_network_qualities_.end(); ++it) { 715 it != estimator.cached_network_qualities_.end(); ++it) {
846 EXPECT_GE((it->second).last_update_time_, update_time_of_network_100); 716 EXPECT_GE((it->second).last_update_time_, update_time_of_network_100);
847 } 717 }
848 } 718 }
849 719
850 TEST(NetworkQualityEstimatorTest, TestGetMedianRTTSince) { 720 TEST(NetworkQualityEstimatorTest, TestGetMedianRTTSince) {
851 std::map<std::string, std::string> variation_params; 721 std::map<std::string, std::string> variation_params;
852 TestNetworkQualityEstimator estimator(variation_params); 722 TestNetworkQualityEstimator estimator(variation_params);
853 base::TimeTicks now = base::TimeTicks::Now(); 723 base::TimeTicks now = base::TimeTicks::Now();
854 base::TimeTicks old = now - base::TimeDelta::FromMilliseconds(1); 724 base::TimeTicks old = now - base::TimeDelta::FromMilliseconds(1);
855 ASSERT_NE(old, now); 725 ASSERT_NE(old, now);
856 726
857 // First sample has very old timestamp. 727 // First sample has very old timestamp.
858 estimator.downstream_throughput_kbps_observations_.AddObservation( 728 estimator.downstream_throughput_kbps_observations_.AddObservation(
859 NetworkQualityEstimator::ThroughputObservation( 729 NetworkQualityEstimator::ThroughputObservation(
860 1, old, NetworkQualityEstimator::URL_REQUEST)); 730 1, old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
861 estimator.rtt_observations_.AddObservation( 731 estimator.rtt_observations_.AddObservation(
862 NetworkQualityEstimator::RttObservation( 732 NetworkQualityEstimator::RttObservation(
863 base::TimeDelta::FromMilliseconds(1), old, 733 base::TimeDelta::FromMilliseconds(1), old,
864 NetworkQualityEstimator::URL_REQUEST)); 734 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
865 735
866 estimator.downstream_throughput_kbps_observations_.AddObservation( 736 estimator.downstream_throughput_kbps_observations_.AddObservation(
867 NetworkQualityEstimator::ThroughputObservation( 737 NetworkQualityEstimator::ThroughputObservation(
868 100, now, NetworkQualityEstimator::URL_REQUEST)); 738 100, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
869 estimator.rtt_observations_.AddObservation( 739 estimator.rtt_observations_.AddObservation(
870 NetworkQualityEstimator::RttObservation( 740 NetworkQualityEstimator::RttObservation(
871 base::TimeDelta::FromMilliseconds(100), now, 741 base::TimeDelta::FromMilliseconds(100), now,
872 NetworkQualityEstimator::URL_REQUEST)); 742 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
873 743
874 const struct { 744 const struct {
875 base::TimeTicks start_timestamp; 745 base::TimeTicks start_timestamp;
876 bool expect_network_quality_available; 746 bool expect_network_quality_available;
877 base::TimeDelta expected_url_request_rtt; 747 base::TimeDelta expected_url_request_rtt;
878 int32_t expected_downstream_throughput; 748 int32_t expected_downstream_throughput;
879 } tests[] = { 749 } tests[] = {
880 {now + base::TimeDelta::FromSeconds(10), false, 750 {now + base::TimeDelta::FromSeconds(10), false,
881 base::TimeDelta::FromMilliseconds(0), 0}, 751 base::TimeDelta::FromMilliseconds(0), 0},
882 {now, true, base::TimeDelta::FromMilliseconds(100), 100}, 752 {now, true, base::TimeDelta::FromMilliseconds(100), 100},
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
1241 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); 1111 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
1242 1112
1243 int32_t throughput; 1113 int32_t throughput;
1244 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&throughput)); 1114 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&throughput));
1245 1115
1246 EXPECT_EQ(2U, rtt_observer.observations().size()); 1116 EXPECT_EQ(2U, rtt_observer.observations().size());
1247 EXPECT_EQ(2U, throughput_observer.observations().size()); 1117 EXPECT_EQ(2U, throughput_observer.observations().size());
1248 for (const auto& observation : rtt_observer.observations()) { 1118 for (const auto& observation : rtt_observer.observations()) {
1249 EXPECT_LE(0, observation.rtt_ms); 1119 EXPECT_LE(0, observation.rtt_ms);
1250 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); 1120 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
1251 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); 1121 EXPECT_EQ(NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST,
1122 observation.source);
1252 } 1123 }
1253 for (const auto& observation : throughput_observer.observations()) { 1124 for (const auto& observation : throughput_observer.observations()) {
1254 EXPECT_LE(0, observation.throughput_kbps); 1125 EXPECT_LE(0, observation.throughput_kbps);
1255 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); 1126 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
1256 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); 1127 EXPECT_EQ(NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST,
1128 observation.source);
1257 } 1129 }
1258 1130
1259 // Verify that observations from TCP and QUIC are passed on to the observers. 1131 // Verify that observations from TCP and QUIC are passed on to the observers.
1260 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); 1132 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1));
1261 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); 1133 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2));
1262 1134
1263 std::unique_ptr<SocketPerformanceWatcher> tcp_watcher = 1135 std::unique_ptr<SocketPerformanceWatcher> tcp_watcher =
1264 estimator.GetSocketPerformanceWatcherFactory() 1136 estimator.GetSocketPerformanceWatcherFactory()
1265 ->CreateSocketPerformanceWatcher( 1137 ->CreateSocketPerformanceWatcher(
1266 SocketPerformanceWatcherFactory::PROTOCOL_TCP); 1138 SocketPerformanceWatcherFactory::PROTOCOL_TCP);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1312 1184
1313 EXPECT_EQ(0U, rtt_observer.observations().size()); 1185 EXPECT_EQ(0U, rtt_observer.observations().size());
1314 base::TimeDelta rtt; 1186 base::TimeDelta rtt;
1315 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt)); 1187 EXPECT_FALSE(estimator.GetURLRequestRTTEstimate(&rtt));
1316 1188
1317 // Send two requests. Verify that the completion of each request generates at 1189 // Send two requests. Verify that the completion of each request generates at
1318 // least one TCP RTT observation. 1190 // least one TCP RTT observation.
1319 for (size_t i = 0; i < 2; ++i) { 1191 for (size_t i = 0; i < 2; ++i) {
1320 size_t before_count_tcp_rtt_observations = 0; 1192 size_t before_count_tcp_rtt_observations = 0;
1321 for (const auto& observation : rtt_observer.observations()) { 1193 for (const auto& observation : rtt_observer.observations()) {
1322 if (observation.source == NetworkQualityEstimator::TCP) 1194 if (observation.source == NETWORK_QUALITY_OBSERVATION_SOURCE_TCP)
1323 ++before_count_tcp_rtt_observations; 1195 ++before_count_tcp_rtt_observations;
1324 } 1196 }
1325 1197
1326 std::unique_ptr<URLRequest> request(context.CreateRequest( 1198 std::unique_ptr<URLRequest> request(context.CreateRequest(
1327 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); 1199 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
1328 request->Start(); 1200 request->Start();
1329 base::RunLoop().Run(); 1201 base::RunLoop().Run();
1330 1202
1331 size_t after_count_tcp_rtt_observations = 0; 1203 size_t after_count_tcp_rtt_observations = 0;
1332 for (const auto& observation : rtt_observer.observations()) { 1204 for (const auto& observation : rtt_observer.observations()) {
1333 if (observation.source == NetworkQualityEstimator::TCP) 1205 if (observation.source == NETWORK_QUALITY_OBSERVATION_SOURCE_TCP)
1334 ++after_count_tcp_rtt_observations; 1206 ++after_count_tcp_rtt_observations;
1335 } 1207 }
1336 // At least one notification should be received per socket performance 1208 // At least one notification should be received per socket performance
1337 // watcher. 1209 // watcher.
1338 EXPECT_LE(1U, after_count_tcp_rtt_observations - 1210 EXPECT_LE(1U, after_count_tcp_rtt_observations -
1339 before_count_tcp_rtt_observations) 1211 before_count_tcp_rtt_observations)
1340 << i; 1212 << i;
1341 } 1213 }
1342 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt)); 1214 EXPECT_TRUE(estimator.GetURLRequestRTTEstimate(&rtt));
1343 } 1215 }
1344 1216
1345 } // namespace net 1217 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698