| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/metrics/histogram_samples.h" | 17 #include "base/metrics/histogram_samples.h" |
| 17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 97 |
| 97 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 98 net::NetworkChangeNotifier::ConnectionType current_network_type_; |
| 98 std::string current_network_id_; | 99 std::string current_network_id_; |
| 99 | 100 |
| 100 // Embedded server used for testing. | 101 // Embedded server used for testing. |
| 101 net::test_server::EmbeddedTestServer embedded_test_server_; | 102 net::test_server::EmbeddedTestServer embedded_test_server_; |
| 102 | 103 |
| 103 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | 104 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| 104 }; | 105 }; |
| 105 | 106 |
| 107 class TestRTTObserver : public net::NetworkQualityEstimator::RTTObserver { |
| 108 public: |
| 109 struct Observation { |
| 110 Observation(int32_t ms, |
| 111 const base::TimeTicks& ts, |
| 112 net::NetworkQualityEstimator::ObservationSource src) |
| 113 : rtt_ms(ms), timestamp(ts), source(src) {} |
| 114 int32_t rtt_ms; |
| 115 base::TimeTicks timestamp; |
| 116 net::NetworkQualityEstimator::ObservationSource source; |
| 117 }; |
| 118 |
| 119 std::vector<Observation>& observations() { return observations_; } |
| 120 |
| 121 // RttObserver implementation: |
| 122 void OnRTTObservation( |
| 123 int32_t rtt_ms, |
| 124 const base::TimeTicks& timestamp, |
| 125 net::NetworkQualityEstimator::ObservationSource source) override { |
| 126 observations_.push_back(Observation(rtt_ms, timestamp, source)); |
| 127 } |
| 128 |
| 129 private: |
| 130 std::vector<Observation> observations_; |
| 131 }; |
| 132 |
| 133 class TestThroughputObserver |
| 134 : public net::NetworkQualityEstimator::ThroughputObserver { |
| 135 public: |
| 136 struct Observation { |
| 137 Observation(int32_t kbps, |
| 138 const base::TimeTicks& ts, |
| 139 net::NetworkQualityEstimator::ObservationSource src) |
| 140 : throughput_kbps(kbps), timestamp(ts), source(src) {} |
| 141 int32_t throughput_kbps; |
| 142 base::TimeTicks timestamp; |
| 143 net::NetworkQualityEstimator::ObservationSource source; |
| 144 }; |
| 145 |
| 146 std::vector<Observation>& observations() { return observations_; } |
| 147 |
| 148 // ThroughputObserver implementation: |
| 149 void OnThroughputObservation( |
| 150 int32_t throughput_kbps, |
| 151 const base::TimeTicks& timestamp, |
| 152 net::NetworkQualityEstimator::ObservationSource source) override { |
| 153 observations_.push_back(Observation(throughput_kbps, timestamp, source)); |
| 154 } |
| 155 |
| 156 private: |
| 157 std::vector<Observation> observations_; |
| 158 }; |
| 159 |
| 106 } // namespace | 160 } // namespace |
| 107 | 161 |
| 108 namespace net { | 162 namespace net { |
| 109 | 163 |
| 110 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 164 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 111 base::HistogramTester histogram_tester; | 165 base::HistogramTester histogram_tester; |
| 112 // Enable requests to local host to be used for network quality estimation. | 166 // Enable requests to local host to be used for network quality estimation. |
| 113 std::map<std::string, std::string> variation_params; | 167 std::map<std::string, std::string> variation_params; |
| 114 TestNetworkQualityEstimator estimator(variation_params); | 168 TestNetworkQualityEstimator estimator(variation_params); |
| 115 | 169 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // Network quality should be unavailable when no observations are available. | 297 // Network quality should be unavailable when no observations are available. |
| 244 base::TimeDelta rtt; | 298 base::TimeDelta rtt; |
| 245 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 299 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
| 246 int32_t kbps; | 300 int32_t kbps; |
| 247 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 301 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 248 | 302 |
| 249 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even | 303 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even |
| 250 // samples. This helps in verifying that the order of samples does not matter. | 304 // samples. This helps in verifying that the order of samples does not matter. |
| 251 for (int i = 1; i <= 99; i += 2) { | 305 for (int i = 1; i <= 99; i += 2) { |
| 252 estimator.downstream_throughput_kbps_observations_.AddObservation( | 306 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 253 NetworkQualityEstimator::Observation(i, now)); | 307 NetworkQualityEstimator::Observation( |
| 308 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 254 estimator.rtt_msec_observations_.AddObservation( | 309 estimator.rtt_msec_observations_.AddObservation( |
| 255 NetworkQualityEstimator::Observation(i, now)); | 310 NetworkQualityEstimator::Observation( |
| 311 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 256 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 312 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 257 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 313 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 258 } | 314 } |
| 259 | 315 |
| 260 for (int i = 2; i <= 100; i += 2) { | 316 for (int i = 2; i <= 100; i += 2) { |
| 261 estimator.downstream_throughput_kbps_observations_.AddObservation( | 317 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 262 NetworkQualityEstimator::Observation(i, now)); | 318 NetworkQualityEstimator::Observation( |
| 319 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 263 estimator.rtt_msec_observations_.AddObservation( | 320 estimator.rtt_msec_observations_.AddObservation( |
| 264 NetworkQualityEstimator::Observation(i, now)); | 321 NetworkQualityEstimator::Observation( |
| 322 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 265 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 323 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 266 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 324 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 267 } | 325 } |
| 268 | 326 |
| 269 for (int i = 0; i <= 100; ++i) { | 327 for (int i = 0; i <= 100; ++i) { |
| 270 // Checks if the difference between the two integers is less than 1. This is | 328 // Checks if the difference between the two integers is less than 1. This is |
| 271 // required because computed percentiles may be slightly different from | 329 // required because computed percentiles may be slightly different from |
| 272 // what is expected due to floating point computation errors and integer | 330 // what is expected due to floating point computation errors and integer |
| 273 // rounding off errors. | 331 // rounding off errors. |
| 274 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 332 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 295 // order. RTT percentiles must be in increasing order. | 353 // order. RTT percentiles must be in increasing order. |
| 296 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { | 354 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { |
| 297 std::map<std::string, std::string> variation_params; | 355 std::map<std::string, std::string> variation_params; |
| 298 TestNetworkQualityEstimator estimator(variation_params); | 356 TestNetworkQualityEstimator estimator(variation_params); |
| 299 base::TimeTicks now = base::TimeTicks::Now(); | 357 base::TimeTicks now = base::TimeTicks::Now(); |
| 300 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); | 358 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); |
| 301 | 359 |
| 302 // First 50 samples have very old timestamp. | 360 // First 50 samples have very old timestamp. |
| 303 for (int i = 1; i <= 50; ++i) { | 361 for (int i = 1; i <= 50; ++i) { |
| 304 estimator.downstream_throughput_kbps_observations_.AddObservation( | 362 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 305 NetworkQualityEstimator::Observation(i, very_old)); | 363 NetworkQualityEstimator::Observation( |
| 364 i, very_old, NetworkQualityEstimator::URL_REQUEST)); |
| 306 estimator.rtt_msec_observations_.AddObservation( | 365 estimator.rtt_msec_observations_.AddObservation( |
| 307 NetworkQualityEstimator::Observation(i, very_old)); | 366 NetworkQualityEstimator::Observation( |
| 367 i, very_old, NetworkQualityEstimator::URL_REQUEST)); |
| 308 } | 368 } |
| 309 | 369 |
| 310 // Next 50 (i.e., from 51 to 100) have recent timestamp. | 370 // Next 50 (i.e., from 51 to 100) have recent timestamp. |
| 311 for (int i = 51; i <= 100; ++i) { | 371 for (int i = 51; i <= 100; ++i) { |
| 312 estimator.downstream_throughput_kbps_observations_.AddObservation( | 372 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 313 NetworkQualityEstimator::Observation(i, now)); | 373 NetworkQualityEstimator::Observation( |
| 374 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 314 estimator.rtt_msec_observations_.AddObservation( | 375 estimator.rtt_msec_observations_.AddObservation( |
| 315 NetworkQualityEstimator::Observation(i, now)); | 376 NetworkQualityEstimator::Observation( |
| 377 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 316 } | 378 } |
| 317 | 379 |
| 318 // Older samples have very little weight. So, all percentiles are >= 51 | 380 // Older samples have very little weight. So, all percentiles are >= 51 |
| 319 // (lowest value among recent observations). | 381 // (lowest value among recent observations). |
| 320 for (int i = 1; i < 100; ++i) { | 382 for (int i = 1; i < 100; ++i) { |
| 321 // Checks if the difference between the two integers is less than 1. This is | 383 // Checks if the difference between the two integers is less than 1. This is |
| 322 // required because computed percentiles may be slightly different from | 384 // required because computed percentiles may be slightly different from |
| 323 // what is expected due to floating point computation errors and integer | 385 // what is expected due to floating point computation errors and integer |
| 324 // rounding off errors. | 386 // rounding off errors. |
| 325 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 387 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 // Test if the network estimates are cached when network change notification | 568 // Test if the network estimates are cached when network change notification |
| 507 // is invoked. | 569 // is invoked. |
| 508 TEST(NetworkQualityEstimatorTest, TestCaching) { | 570 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 509 std::map<std::string, std::string> variation_params; | 571 std::map<std::string, std::string> variation_params; |
| 510 TestNetworkQualityEstimator estimator(variation_params); | 572 TestNetworkQualityEstimator estimator(variation_params); |
| 511 size_t expected_cache_size = 0; | 573 size_t expected_cache_size = 0; |
| 512 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 574 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 513 | 575 |
| 514 // Cache entry will not be added for (NONE, ""). | 576 // Cache entry will not be added for (NONE, ""). |
| 515 estimator.downstream_throughput_kbps_observations_.AddObservation( | 577 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 516 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); | 578 NetworkQualityEstimator::Observation( |
| 579 1, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 517 estimator.rtt_msec_observations_.AddObservation( | 580 estimator.rtt_msec_observations_.AddObservation( |
| 518 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); | 581 NetworkQualityEstimator::Observation( |
| 582 1000, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 519 estimator.SimulateNetworkChangeTo( | 583 estimator.SimulateNetworkChangeTo( |
| 520 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | 584 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 521 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 585 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 522 | 586 |
| 523 // Entry will be added for (2G, "test1"). | 587 // Entry will be added for (2G, "test1"). |
| 524 // Also, set the network quality for (2G, "test1") so that it is stored in | 588 // Also, set the network quality for (2G, "test1") so that it is stored in |
| 525 // the cache. | 589 // the cache. |
| 526 estimator.downstream_throughput_kbps_observations_.AddObservation( | 590 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 527 NetworkQualityEstimator::Observation(1, base::TimeTicks::Now())); | 591 NetworkQualityEstimator::Observation( |
| 592 1, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 528 estimator.rtt_msec_observations_.AddObservation( | 593 estimator.rtt_msec_observations_.AddObservation( |
| 529 NetworkQualityEstimator::Observation(1000, base::TimeTicks::Now())); | 594 NetworkQualityEstimator::Observation( |
| 595 1000, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 530 | 596 |
| 531 estimator.SimulateNetworkChangeTo( | 597 estimator.SimulateNetworkChangeTo( |
| 532 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); | 598 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-1"); |
| 533 ++expected_cache_size; | 599 ++expected_cache_size; |
| 534 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 600 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 535 | 601 |
| 536 // Entry will be added for (3G, "test1"). | 602 // Entry will be added for (3G, "test1"). |
| 537 // 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 |
| 538 // the cache. | 604 // the cache. |
| 539 estimator.downstream_throughput_kbps_observations_.AddObservation( | 605 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 540 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | 606 NetworkQualityEstimator::Observation( |
| 607 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 541 estimator.rtt_msec_observations_.AddObservation( | 608 estimator.rtt_msec_observations_.AddObservation( |
| 542 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | 609 NetworkQualityEstimator::Observation( |
| 610 500, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 543 estimator.SimulateNetworkChangeTo( | 611 estimator.SimulateNetworkChangeTo( |
| 544 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); | 612 NetworkChangeNotifier::ConnectionType::CONNECTION_3G, "test-2"); |
| 545 ++expected_cache_size; | 613 ++expected_cache_size; |
| 546 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 614 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 547 | 615 |
| 548 // Entry will not be added for (3G, "test2"). | 616 // Entry will not be added for (3G, "test2"). |
| 549 estimator.SimulateNetworkChangeTo( | 617 estimator.SimulateNetworkChangeTo( |
| 550 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | 618 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); |
| 551 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 619 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| 552 | 620 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 std::string()); | 664 std::string()); |
| 597 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); | 665 EXPECT_EQ(0U, estimator.cached_network_qualities_.size()); |
| 598 | 666 |
| 599 // Add 100 more networks than the maximum size of the cache. | 667 // Add 100 more networks than the maximum size of the cache. |
| 600 size_t network_count = | 668 size_t network_count = |
| 601 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; | 669 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize + 100; |
| 602 | 670 |
| 603 base::TimeTicks update_time_of_network_100; | 671 base::TimeTicks update_time_of_network_100; |
| 604 for (size_t i = 0; i < network_count; ++i) { | 672 for (size_t i = 0; i < network_count; ++i) { |
| 605 estimator.downstream_throughput_kbps_observations_.AddObservation( | 673 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 606 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | 674 NetworkQualityEstimator::Observation( |
| 675 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 607 estimator.rtt_msec_observations_.AddObservation( | 676 estimator.rtt_msec_observations_.AddObservation( |
| 608 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | 677 NetworkQualityEstimator::Observation( |
| 678 500, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 609 | 679 |
| 610 if (i == 100) | 680 if (i == 100) |
| 611 update_time_of_network_100 = base::TimeTicks::Now(); | 681 update_time_of_network_100 = base::TimeTicks::Now(); |
| 612 | 682 |
| 613 estimator.SimulateNetworkChangeTo( | 683 estimator.SimulateNetworkChangeTo( |
| 614 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 684 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 615 base::SizeTToString(i)); | 685 base::SizeTToString(i)); |
| 616 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) | 686 if (i < NetworkQualityEstimator::kMaximumNetworkQualityCacheSize) |
| 617 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); | 687 EXPECT_EQ(i, estimator.cached_network_qualities_.size()); |
| 618 EXPECT_LE(estimator.cached_network_qualities_.size(), | 688 EXPECT_LE(estimator.cached_network_qualities_.size(), |
| 619 static_cast<size_t>( | 689 static_cast<size_t>( |
| 620 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); | 690 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize)); |
| 621 } | 691 } |
| 622 // One more call so that the last network is also written to cache. | 692 // One more call so that the last network is also written to cache. |
| 623 estimator.downstream_throughput_kbps_observations_.AddObservation( | 693 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 624 NetworkQualityEstimator::Observation(2, base::TimeTicks::Now())); | 694 NetworkQualityEstimator::Observation( |
| 695 2, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 625 estimator.rtt_msec_observations_.AddObservation( | 696 estimator.rtt_msec_observations_.AddObservation( |
| 626 NetworkQualityEstimator::Observation(500, base::TimeTicks::Now())); | 697 NetworkQualityEstimator::Observation( |
| 698 500, base::TimeTicks::Now(), NetworkQualityEstimator::URL_REQUEST)); |
| 627 estimator.SimulateNetworkChangeTo( | 699 estimator.SimulateNetworkChangeTo( |
| 628 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, | 700 net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, |
| 629 base::SizeTToString(network_count - 1)); | 701 base::SizeTToString(network_count - 1)); |
| 630 EXPECT_EQ(static_cast<size_t>( | 702 EXPECT_EQ(static_cast<size_t>( |
| 631 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), | 703 NetworkQualityEstimator::kMaximumNetworkQualityCacheSize), |
| 632 estimator.cached_network_qualities_.size()); | 704 estimator.cached_network_qualities_.size()); |
| 633 | 705 |
| 634 // Test that the cache is LRU by examining its contents. Networks in cache | 706 // Test that the cache is LRU by examining its contents. Networks in cache |
| 635 // must all be newer than the 100th network. | 707 // must all be newer than the 100th network. |
| 636 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = | 708 for (NetworkQualityEstimator::CachedNetworkQualities::iterator it = |
| 637 estimator.cached_network_qualities_.begin(); | 709 estimator.cached_network_qualities_.begin(); |
| 638 it != estimator.cached_network_qualities_.end(); ++it) { | 710 it != estimator.cached_network_qualities_.end(); ++it) { |
| 639 EXPECT_GE((it->second).last_update_time_, update_time_of_network_100); | 711 EXPECT_GE((it->second).last_update_time_, update_time_of_network_100); |
| 640 } | 712 } |
| 641 } | 713 } |
| 642 | 714 |
| 643 TEST(NetworkQualityEstimatorTest, TestGetMedianRTTSince) { | 715 TEST(NetworkQualityEstimatorTest, TestGetMedianRTTSince) { |
| 644 std::map<std::string, std::string> variation_params; | 716 std::map<std::string, std::string> variation_params; |
| 645 TestNetworkQualityEstimator estimator(variation_params); | 717 TestNetworkQualityEstimator estimator(variation_params); |
| 646 base::TimeTicks now = base::TimeTicks::Now(); | 718 base::TimeTicks now = base::TimeTicks::Now(); |
| 647 base::TimeTicks old = | 719 base::TimeTicks old = |
| 648 base::TimeTicks::Now() - base::TimeDelta::FromMilliseconds(1); | 720 base::TimeTicks::Now() - base::TimeDelta::FromMilliseconds(1); |
| 649 | 721 |
| 650 // First sample has very old timestamp. | 722 // First sample has very old timestamp. |
| 651 estimator.downstream_throughput_kbps_observations_.AddObservation( | 723 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 652 NetworkQualityEstimator::Observation(1, old)); | 724 NetworkQualityEstimator::Observation( |
| 725 1, old, NetworkQualityEstimator::URL_REQUEST)); |
| 653 estimator.rtt_msec_observations_.AddObservation( | 726 estimator.rtt_msec_observations_.AddObservation( |
| 654 NetworkQualityEstimator::Observation(1, old)); | 727 NetworkQualityEstimator::Observation( |
| 728 1, old, NetworkQualityEstimator::URL_REQUEST)); |
| 655 | 729 |
| 656 estimator.downstream_throughput_kbps_observations_.AddObservation( | 730 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 657 NetworkQualityEstimator::Observation(100, now)); | 731 NetworkQualityEstimator::Observation( |
| 732 100, now, NetworkQualityEstimator::URL_REQUEST)); |
| 658 estimator.rtt_msec_observations_.AddObservation( | 733 estimator.rtt_msec_observations_.AddObservation( |
| 659 NetworkQualityEstimator::Observation(100, now)); | 734 NetworkQualityEstimator::Observation( |
| 735 100, now, NetworkQualityEstimator::URL_REQUEST)); |
| 660 | 736 |
| 661 base::TimeDelta rtt; | 737 base::TimeDelta rtt; |
| 662 EXPECT_FALSE(estimator.GetRecentMedianRTT( | 738 EXPECT_FALSE(estimator.GetRecentMedianRTT( |
| 663 now + base::TimeDelta::FromSeconds(10), &rtt)); | 739 now + base::TimeDelta::FromSeconds(10), &rtt)); |
| 664 EXPECT_TRUE(estimator.GetRecentMedianRTT(now, &rtt)); | 740 EXPECT_TRUE(estimator.GetRecentMedianRTT(now, &rtt)); |
| 665 EXPECT_EQ(100, rtt.InMilliseconds()); | 741 EXPECT_EQ(100, rtt.InMilliseconds()); |
| 666 | 742 |
| 667 int32_t downstream_throughput_kbps; | 743 int32_t downstream_throughput_kbps; |
| 668 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( | 744 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( |
| 669 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); | 745 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 | 1003 |
| 928 scoped_ptr<URLRequest> request(context.CreateRequest( | 1004 scoped_ptr<URLRequest> request(context.CreateRequest( |
| 929 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1005 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 930 request->Start(); | 1006 request->Start(); |
| 931 base::RunLoop().Run(); | 1007 base::RunLoop().Run(); |
| 932 | 1008 |
| 933 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); | 1009 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); |
| 934 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | 1010 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 935 } | 1011 } |
| 936 | 1012 |
| 1013 TEST(NetworkQualityEstimatorTest, TestObservers) { |
| 1014 TestRTTObserver rtt_observer; |
| 1015 TestThroughputObserver throughput_observer; |
| 1016 std::map<std::string, std::string> variation_params; |
| 1017 TestNetworkQualityEstimator estimator(variation_params); |
| 1018 estimator.AddRTTObserver(&rtt_observer); |
| 1019 estimator.AddThroughputObserver(&throughput_observer); |
| 1020 |
| 1021 TestDelegate test_delegate; |
| 1022 TestURLRequestContext context(true); |
| 1023 context.set_network_quality_estimator(&estimator); |
| 1024 context.Init(); |
| 1025 |
| 1026 EXPECT_EQ(0U, rtt_observer.observations().size()); |
| 1027 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 1028 base::TimeTicks then = base::TimeTicks::Now(); |
| 1029 |
| 1030 scoped_ptr<URLRequest> request(context.CreateRequest( |
| 1031 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1032 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1033 request->Start(); |
| 1034 base::RunLoop().Run(); |
| 1035 |
| 1036 scoped_ptr<URLRequest> request2(context.CreateRequest( |
| 1037 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1038 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1039 request2->Start(); |
| 1040 base::RunLoop().Run(); |
| 1041 |
| 1042 // Both RTT and downstream throughput should be updated. |
| 1043 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), |
| 1044 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
| 1045 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, |
| 1046 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 1047 base::TimeTicks(), 100)); |
| 1048 |
| 1049 EXPECT_EQ(2U, rtt_observer.observations().size()); |
| 1050 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1051 for (auto observation : rtt_observer.observations()) { |
| 1052 EXPECT_LE(0, observation.rtt_ms); |
| 1053 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1054 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1055 } |
| 1056 for (auto observation : throughput_observer.observations()) { |
| 1057 EXPECT_LE(0, observation.throughput_kbps); |
| 1058 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1059 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1060 } |
| 1061 } |
| 1062 |
| 937 } // namespace net | 1063 } // namespace net |
| OLD | NEW |