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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/metrics/histogram_samples.h" | 17 #include "base/metrics/histogram_samples.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "net/base/external_estimate_provider.h" | 23 #include "net/base/external_estimate_provider.h" |
| 24 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 25 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
| 26 #include "net/base/socket_performance_watcher_factory.h" | |
| 26 #include "net/http/http_status_code.h" | 27 #include "net/http/http_status_code.h" |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 29 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 30 #include "net/test/embedded_test_server/http_response.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 int32_t throughput_kbps, | 151 int32_t throughput_kbps, |
| 151 const base::TimeTicks& timestamp, | 152 const base::TimeTicks& timestamp, |
| 152 net::NetworkQualityEstimator::ObservationSource source) override { | 153 net::NetworkQualityEstimator::ObservationSource source) override { |
| 153 observations_.push_back(Observation(throughput_kbps, timestamp, source)); | 154 observations_.push_back(Observation(throughput_kbps, timestamp, source)); |
| 154 } | 155 } |
| 155 | 156 |
| 156 private: | 157 private: |
| 157 std::vector<Observation> observations_; | 158 std::vector<Observation> observations_; |
| 158 }; | 159 }; |
| 159 | 160 |
| 161 class TestPacketLossObserver | |
| 162 : public net::NetworkQualityEstimator::PacketLossObserver { | |
| 163 public: | |
| 164 struct Observation { | |
| 165 Observation(uint64_t num_packets_lost, | |
| 166 uint64_t num_packets_received_in_order, | |
| 167 uint64_t num_packets_received_not_in_order, | |
| 168 const base::TimeTicks& ts, | |
| 169 net::NetworkQualityEstimator::ObservationSource src) | |
| 170 : num_packets_lost(num_packets_lost), | |
| 171 num_packets_received_in_order(num_packets_received_in_order), | |
| 172 num_packets_received_not_in_order(num_packets_received_not_in_order), | |
| 173 timestamp(ts), | |
| 174 source(src) {} | |
| 175 uint64_t num_packets_lost; | |
| 176 uint64_t num_packets_received_in_order; | |
| 177 uint64_t num_packets_received_not_in_order; | |
| 178 base::TimeTicks timestamp; | |
| 179 net::NetworkQualityEstimator::ObservationSource source; | |
| 180 }; | |
| 181 | |
| 182 std::vector<Observation>& observations() { return observations_; } | |
| 183 | |
| 184 // PacketLossObserver implementation: | |
| 185 void OnPacketLossObservation( | |
| 186 uint64_t num_packets_lost, | |
| 187 uint64_t num_packets_received_in_order, | |
| 188 uint64_t num_packets_received_not_in_order, | |
| 189 const base::TimeTicks& timestamp, | |
| 190 net::NetworkQualityEstimator::ObservationSource source) override { | |
| 191 observations_.push_back( | |
| 192 Observation(num_packets_lost, num_packets_received_in_order, | |
| 193 num_packets_received_not_in_order, timestamp, source)); | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 std::vector<Observation> observations_; | |
| 198 }; | |
| 199 | |
| 160 } // namespace | 200 } // namespace |
| 161 | 201 |
| 162 namespace net { | 202 namespace net { |
| 163 | 203 |
| 164 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 204 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 165 base::HistogramTester histogram_tester; | 205 base::HistogramTester histogram_tester; |
| 166 // Enable requests to local host to be used for network quality estimation. | 206 // Enable requests to local host to be used for network quality estimation. |
| 167 std::map<std::string, std::string> variation_params; | 207 std::map<std::string, std::string> variation_params; |
| 168 TestNetworkQualityEstimator estimator(variation_params); | 208 TestNetworkQualityEstimator estimator(variation_params); |
| 169 | 209 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 288 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 249 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 289 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
| 250 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 290 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 251 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 291 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 252 base::TimeTicks(), 100)); | 292 base::TimeTicks(), 100)); |
| 253 | 293 |
| 254 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 294 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
| 255 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 295 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 256 } | 296 } |
| 257 | 297 |
| 298 // Tests that packet loss rate is updated correctly. | |
| 299 TEST(NetworkQualityEstimatorTest, TestPacketLossRateUpdates) { | |
| 300 base::HistogramTester histogram_tester; | |
| 301 std::map<std::string, std::string> variation_params; | |
| 302 TestNetworkQualityEstimator estimator(variation_params); | |
| 303 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.Unknown", 0); | |
| 304 | |
| 305 float packet_loss_rate = | |
| 306 NetworkQualityEstimator::kMinimumValidPacketLossRate - 1; | |
| 307 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 308 | |
| 309 estimator.OnUpdatedPacketCountAvailable( | |
| 310 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); | |
| 311 | |
| 312 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 313 EXPECT_NEAR(0.0, packet_loss_rate, 0.001); | |
| 314 | |
| 315 // Check UMA histograms. | |
| 316 estimator.SimulateNetworkChangeTo( | |
| 317 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); | |
| 318 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.Unknown", 1); | |
| 319 | |
| 320 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 321 | |
| 322 // Expecting packet number p, but received packet number p+9. 1 out of 10 | |
|
bengr
2016/02/19 00:45:08
p is a letter. :)
tbansal1
2016/02/26 01:15:00
Done.
| |
| 323 // packets were received. | |
| 324 estimator.OnUpdatedPacketCountAvailable( | |
| 325 NetworkQualityEstimator::PROTOCOL_QUIC, 9, 1, 0); | |
| 326 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 327 EXPECT_NEAR(9.0 / 10, packet_loss_rate, 0.001); | |
| 328 | |
| 329 // Expecting packet number p+10, and received packet number p+10. Now, 2 out | |
| 330 // of 11 packets were received. | |
| 331 estimator.OnUpdatedPacketCountAvailable( | |
| 332 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); | |
| 333 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 334 EXPECT_NEAR(9.0 / 11, packet_loss_rate, 0.001); | |
| 335 | |
| 336 // Expecting packet number p+11, and received packet number p+11. Now, 3 out | |
| 337 // of 12 packets were received. | |
| 338 estimator.OnUpdatedPacketCountAvailable( | |
| 339 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); | |
| 340 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 341 EXPECT_NEAR(9.0 / 12, packet_loss_rate, 0.001); | |
| 342 | |
| 343 // Expecting packet number p+12, but received packet number p. Still, 3 out of | |
| 344 // 12 packets were received. | |
| 345 estimator.OnUpdatedPacketCountAvailable( | |
| 346 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 0, 1); | |
| 347 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 348 EXPECT_NEAR(9.0 / 12, packet_loss_rate, 0.001); | |
| 349 | |
| 350 estimator.SimulateNetworkChangeTo( | |
| 351 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); | |
| 352 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.WiFi", 1); | |
| 353 | |
| 354 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 355 } | |
| 356 | |
| 258 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 357 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 259 std::map<std::string, std::string> variation_params; | 358 std::map<std::string, std::string> variation_params; |
| 260 TestNetworkQualityEstimator estimator(variation_params); | 359 TestNetworkQualityEstimator estimator(variation_params); |
| 261 | 360 |
| 262 TestDelegate test_delegate; | 361 TestDelegate test_delegate; |
| 263 TestURLRequestContext context(true); | 362 TestURLRequestContext context(true); |
| 264 context.set_network_quality_estimator(&estimator); | 363 context.set_network_quality_estimator(&estimator); |
| 265 context.Init(); | 364 context.Init(); |
| 266 | 365 |
| 267 // Push 10 more observations than the maximum buffer size. | 366 // Push 10 more observations than the maximum buffer size. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 292 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { | 391 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { |
| 293 std::map<std::string, std::string> variation_params; | 392 std::map<std::string, std::string> variation_params; |
| 294 TestNetworkQualityEstimator estimator(variation_params); | 393 TestNetworkQualityEstimator estimator(variation_params); |
| 295 base::TimeTicks now = base::TimeTicks::Now(); | 394 base::TimeTicks now = base::TimeTicks::Now(); |
| 296 | 395 |
| 297 // Network quality should be unavailable when no observations are available. | 396 // Network quality should be unavailable when no observations are available. |
| 298 base::TimeDelta rtt; | 397 base::TimeDelta rtt; |
| 299 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 398 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
| 300 int32_t kbps; | 399 int32_t kbps; |
| 301 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 400 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 401 float packet_loss_rate = | |
| 402 NetworkQualityEstimator::kMinimumValidPacketLossRate - 1; | |
| 403 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 302 | 404 |
| 303 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even | 405 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even |
| 304 // samples. This helps in verifying that the order of samples does not matter. | 406 // samples. This helps in verifying that the order of samples does not matter. |
| 305 for (int i = 1; i <= 99; i += 2) { | 407 for (int i = 1; i <= 99; i += 2) { |
| 306 estimator.downstream_throughput_kbps_observations_.AddObservation( | 408 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 307 NetworkQualityEstimator::ThroughputObservation( | 409 NetworkQualityEstimator::ThroughputObservation( |
| 308 i, now, NetworkQualityEstimator::URL_REQUEST)); | 410 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 309 estimator.rtt_msec_observations_.AddObservation( | 411 estimator.rtt_msec_observations_.AddObservation( |
| 310 NetworkQualityEstimator::RttObservation( | 412 NetworkQualityEstimator::RttObservation( |
| 311 base::TimeDelta::FromMilliseconds(i), now, | 413 base::TimeDelta::FromMilliseconds(i), now, |
| 312 NetworkQualityEstimator::URL_REQUEST)); | 414 NetworkQualityEstimator::URL_REQUEST)); |
| 415 estimator.packet_loss_rate_observations_.AddObservation( | |
| 416 NetworkQualityEstimator::PacketLossRateObservation( | |
| 417 0.0, now, NetworkQualityEstimator::QUIC)); | |
| 313 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 418 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 314 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 419 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 420 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 315 } | 421 } |
| 316 | 422 |
| 317 for (int i = 2; i <= 100; i += 2) { | 423 for (int i = 2; i <= 100; i += 2) { |
| 318 estimator.downstream_throughput_kbps_observations_.AddObservation( | 424 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 319 NetworkQualityEstimator::ThroughputObservation( | 425 NetworkQualityEstimator::ThroughputObservation( |
| 320 i, now, NetworkQualityEstimator::URL_REQUEST)); | 426 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 321 estimator.rtt_msec_observations_.AddObservation( | 427 estimator.rtt_msec_observations_.AddObservation( |
| 322 NetworkQualityEstimator::RttObservation( | 428 NetworkQualityEstimator::RttObservation( |
| 323 base::TimeDelta::FromMilliseconds(i), now, | 429 base::TimeDelta::FromMilliseconds(i), now, |
| 324 NetworkQualityEstimator::URL_REQUEST)); | 430 NetworkQualityEstimator::URL_REQUEST)); |
| 431 estimator.packet_loss_rate_observations_.AddObservation( | |
| 432 NetworkQualityEstimator::PacketLossRateObservation( | |
| 433 1.0, now, NetworkQualityEstimator::QUIC)); | |
| 325 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 434 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 326 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 435 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 436 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 327 } | 437 } |
| 328 | 438 |
| 329 for (int i = 0; i <= 100; ++i) { | 439 for (int i = 0; i <= 100; ++i) { |
| 330 // Checks if the difference between the two integers is less than 1. This is | 440 // Checks if the difference between the two integers is less than 1. This is |
| 331 // required because computed percentiles may be slightly different from | 441 // required because computed percentiles may be slightly different from |
| 332 // what is expected due to floating point computation errors and integer | 442 // what is expected due to floating point computation errors and integer |
| 333 // rounding off errors. | 443 // rounding off errors. |
| 334 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 444 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 335 base::TimeTicks(), i), | 445 base::TimeTicks(), i), |
| 336 100 - i, 1); | 446 100 - i, 1); |
| 337 EXPECT_NEAR( | 447 EXPECT_NEAR( |
| 338 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 448 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
| 339 i, 1); | 449 i, 1); |
| 340 } | 450 } |
| 341 | 451 |
| 342 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 452 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 343 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 453 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 344 // |network_quality| should be equal to the 50 percentile value. | 454 // |network_quality| should be equal to the 50 percentile value. |
| 345 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 455 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 346 base::TimeTicks(), 50) > 0); | 456 base::TimeTicks(), 50) > 0); |
| 347 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != | 457 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != |
| 348 NetworkQualityEstimator::InvalidRTT()); | 458 NetworkQualityEstimator::InvalidRTT()); |
| 459 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 460 EXPECT_NEAR(0.5, packet_loss_rate, 0.001); | |
| 349 } | 461 } |
| 350 | 462 |
| 351 // Verifies that the percentiles are correctly computed. Observations have | 463 // Verifies that the percentiles are correctly computed. Observations have |
| 352 // different timestamps with half the observations being very old and the rest | 464 // different timestamps with half the observations being very old and the rest |
| 353 // of them being very recent. Percentiles should factor in recent observations | 465 // of them being very recent. Percentiles should factor in recent observations |
| 354 // much more heavily than older samples. Kbps percentiles must be in decreasing | 466 // much more heavily than older samples. Kbps percentiles must be in decreasing |
| 355 // order. RTT percentiles must be in increasing order. | 467 // order. RTT percentiles must be in increasing order. |
| 356 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { | 468 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { |
| 357 std::map<std::string, std::string> variation_params; | 469 std::map<std::string, std::string> variation_params; |
| 358 TestNetworkQualityEstimator estimator(variation_params); | 470 TestNetworkQualityEstimator estimator(variation_params); |
| 359 base::TimeTicks now = base::TimeTicks::Now(); | 471 base::TimeTicks now = base::TimeTicks::Now(); |
| 360 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); | 472 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); |
| 361 | 473 |
| 474 float packet_loss_rate = | |
|
bengr
2016/02/19 00:45:08
I don't think you need to initialize here, but if
tbansal1
2016/02/26 01:15:00
Done.
| |
| 475 NetworkQualityEstimator::kMinimumValidPacketLossRate - 1; | |
| 476 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 477 | |
| 362 // First 50 samples have very old timestamp. | 478 // First 50 samples have very old timestamp. |
| 363 for (int i = 1; i <= 50; ++i) { | 479 for (int i = 1; i <= 50; ++i) { |
| 364 estimator.downstream_throughput_kbps_observations_.AddObservation( | 480 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 365 NetworkQualityEstimator::ThroughputObservation( | 481 NetworkQualityEstimator::ThroughputObservation( |
| 366 i, very_old, NetworkQualityEstimator::URL_REQUEST)); | 482 i, very_old, NetworkQualityEstimator::URL_REQUEST)); |
| 367 estimator.rtt_msec_observations_.AddObservation( | 483 estimator.rtt_msec_observations_.AddObservation( |
| 368 NetworkQualityEstimator::RttObservation( | 484 NetworkQualityEstimator::RttObservation( |
| 369 base::TimeDelta::FromMilliseconds(i), very_old, | 485 base::TimeDelta::FromMilliseconds(i), very_old, |
| 370 NetworkQualityEstimator::URL_REQUEST)); | 486 NetworkQualityEstimator::URL_REQUEST)); |
| 487 estimator.packet_loss_rate_observations_.AddObservation( | |
| 488 NetworkQualityEstimator::PacketLossRateObservation( | |
| 489 0.0, very_old, NetworkQualityEstimator::QUIC)); | |
| 371 } | 490 } |
| 372 | 491 |
| 373 // Next 50 (i.e., from 51 to 100) have recent timestamp. | 492 // Next 50 (i.e., from 51 to 100) have recent timestamp. |
| 374 for (int i = 51; i <= 100; ++i) { | 493 for (int i = 51; i <= 100; ++i) { |
| 375 estimator.downstream_throughput_kbps_observations_.AddObservation( | 494 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 376 NetworkQualityEstimator::ThroughputObservation( | 495 NetworkQualityEstimator::ThroughputObservation( |
| 377 i, now, NetworkQualityEstimator::URL_REQUEST)); | 496 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 378 estimator.rtt_msec_observations_.AddObservation( | 497 estimator.rtt_msec_observations_.AddObservation( |
| 379 NetworkQualityEstimator::RttObservation( | 498 NetworkQualityEstimator::RttObservation( |
| 380 base::TimeDelta::FromMilliseconds(i), now, | 499 base::TimeDelta::FromMilliseconds(i), now, |
| 381 NetworkQualityEstimator::URL_REQUEST)); | 500 NetworkQualityEstimator::URL_REQUEST)); |
| 501 estimator.packet_loss_rate_observations_.AddObservation( | |
| 502 NetworkQualityEstimator::PacketLossRateObservation( | |
| 503 1.0, now, NetworkQualityEstimator::QUIC)); | |
| 382 } | 504 } |
| 383 | 505 |
| 384 // Older samples have very little weight. So, all percentiles are >= 51 | 506 // Older samples have very little weight. So, all percentiles are >= 51 |
| 385 // (lowest value among recent observations). | 507 // (lowest value among recent observations). |
| 386 for (int i = 1; i < 100; ++i) { | 508 for (int i = 1; i < 100; ++i) { |
| 387 // Checks if the difference between the two integers is less than 1. This is | 509 // Checks if the difference between the two integers is less than 1. This is |
| 388 // required because computed percentiles may be slightly different from | 510 // required because computed percentiles may be slightly different from |
| 389 // what is expected due to floating point computation errors and integer | 511 // what is expected due to floating point computation errors and integer |
| 390 // rounding off errors. | 512 // rounding off errors. |
| 391 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 513 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 392 base::TimeTicks(), i), | 514 base::TimeTicks(), i), |
| 393 51 + 0.49 * (100 - i), 1); | 515 51 + 0.49 * (100 - i), 1); |
| 394 EXPECT_NEAR( | 516 EXPECT_NEAR( |
| 395 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 517 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
| 396 51 + 0.49 * i, 1); | 518 51 + 0.49 * i, 1); |
| 397 } | 519 } |
| 398 | 520 |
| 399 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 521 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 400 estimator.GetRTTEstimateInternal( | 522 estimator.GetRTTEstimateInternal( |
| 401 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 523 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
| 402 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 524 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 403 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 525 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 404 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 526 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
| 527 | |
| 528 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 529 EXPECT_NEAR(1.0, packet_loss_rate, 0.001); | |
| 405 } | 530 } |
| 406 | 531 |
| 407 // This test notifies NetworkQualityEstimator of received data. Next, | 532 // This test notifies NetworkQualityEstimator of received data. Next, |
| 408 // throughput and RTT percentiles are checked for correctness by doing simple | 533 // throughput and RTT percentiles are checked for correctness by doing simple |
| 409 // verifications. | 534 // verifications. |
| 410 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 535 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { |
| 411 std::map<std::string, std::string> variation_params; | 536 std::map<std::string, std::string> variation_params; |
| 412 TestNetworkQualityEstimator estimator(variation_params); | 537 TestNetworkQualityEstimator estimator(variation_params); |
| 413 | 538 |
| 414 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 539 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1019 request->Start(); | 1144 request->Start(); |
| 1020 base::RunLoop().Run(); | 1145 base::RunLoop().Run(); |
| 1021 | 1146 |
| 1022 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); | 1147 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); |
| 1023 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | 1148 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 1024 } | 1149 } |
| 1025 | 1150 |
| 1026 TEST(NetworkQualityEstimatorTest, TestObservers) { | 1151 TEST(NetworkQualityEstimatorTest, TestObservers) { |
| 1027 TestRTTObserver rtt_observer; | 1152 TestRTTObserver rtt_observer; |
| 1028 TestThroughputObserver throughput_observer; | 1153 TestThroughputObserver throughput_observer; |
| 1154 TestPacketLossObserver packet_loss_observer; | |
| 1029 std::map<std::string, std::string> variation_params; | 1155 std::map<std::string, std::string> variation_params; |
| 1030 TestNetworkQualityEstimator estimator(variation_params); | 1156 TestNetworkQualityEstimator estimator(variation_params); |
| 1031 estimator.AddRTTObserver(&rtt_observer); | 1157 estimator.AddRTTObserver(&rtt_observer); |
| 1032 estimator.AddThroughputObserver(&throughput_observer); | 1158 estimator.AddThroughputObserver(&throughput_observer); |
| 1159 estimator.AddPacketLossObserver(&packet_loss_observer); | |
| 1033 | 1160 |
| 1034 TestDelegate test_delegate; | 1161 TestDelegate test_delegate; |
| 1035 TestURLRequestContext context(true); | 1162 TestURLRequestContext context(true); |
| 1036 context.set_network_quality_estimator(&estimator); | 1163 context.set_network_quality_estimator(&estimator); |
| 1037 context.Init(); | 1164 context.Init(); |
| 1038 | 1165 |
| 1039 EXPECT_EQ(0U, rtt_observer.observations().size()); | 1166 EXPECT_EQ(0U, rtt_observer.observations().size()); |
| 1040 EXPECT_EQ(0U, throughput_observer.observations().size()); | 1167 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 1041 base::TimeTicks then = base::TimeTicks::Now(); | 1168 base::TimeTicks then = base::TimeTicks::Now(); |
| 1042 | 1169 |
| 1043 scoped_ptr<URLRequest> request(context.CreateRequest( | 1170 scoped_ptr<URLRequest> request(context.CreateRequest( |
| 1044 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1171 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1045 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1172 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1046 request->Start(); | 1173 request->Start(); |
| 1047 base::RunLoop().Run(); | 1174 base::RunLoop().Run(); |
| 1048 | 1175 |
| 1049 scoped_ptr<URLRequest> request2(context.CreateRequest( | 1176 scoped_ptr<URLRequest> request2(context.CreateRequest( |
| 1050 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1177 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1051 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1178 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1052 request2->Start(); | 1179 request2->Start(); |
| 1053 base::RunLoop().Run(); | 1180 base::RunLoop().Run(); |
| 1054 | 1181 |
| 1182 float packet_loss_rate = | |
| 1183 NetworkQualityEstimator::kMinimumValidPacketLossRate - 1; | |
| 1184 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 1185 | |
| 1186 estimator.OnUpdatedPacketCountAvailable( | |
| 1187 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); | |
| 1188 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 1189 EXPECT_NEAR(0.0, packet_loss_rate, 0.001); | |
| 1190 | |
| 1055 // Both RTT and downstream throughput should be updated. | 1191 // Both RTT and downstream throughput should be updated. |
| 1056 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), | 1192 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), |
| 1057 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 1193 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
| 1058 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, | 1194 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, |
| 1059 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 1195 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 1060 base::TimeTicks(), 100)); | 1196 base::TimeTicks(), 100)); |
| 1061 | 1197 |
| 1062 EXPECT_EQ(2U, rtt_observer.observations().size()); | 1198 EXPECT_EQ(2U, rtt_observer.observations().size()); |
| 1063 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1199 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1200 ASSERT_EQ(1U, packet_loss_observer.observations().size()); | |
| 1064 for (auto observation : rtt_observer.observations()) { | 1201 for (auto observation : rtt_observer.observations()) { |
| 1065 EXPECT_LE(0, observation.rtt_ms); | 1202 EXPECT_LE(0, observation.rtt_ms); |
| 1066 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1203 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1067 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1204 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1068 } | 1205 } |
| 1069 for (auto observation : throughput_observer.observations()) { | 1206 for (auto observation : throughput_observer.observations()) { |
| 1070 EXPECT_LE(0, observation.throughput_kbps); | 1207 EXPECT_LE(0, observation.throughput_kbps); |
| 1071 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1208 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1072 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1209 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1073 } | 1210 } |
| 1211 for (auto observation : packet_loss_observer.observations()) { | |
| 1212 EXPECT_LE(0u, observation.num_packets_lost); | |
| 1213 EXPECT_LE(1u, observation.num_packets_received_in_order); | |
| 1214 EXPECT_LE(0u, observation.num_packets_received_not_in_order); | |
| 1215 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | |
| 1216 EXPECT_EQ(NetworkQualityEstimator::QUIC, observation.source); | |
| 1217 } | |
| 1074 | 1218 |
| 1075 // Verify that observations from TCP and QUIC are passed on to the observers. | 1219 // Verify that observations from TCP and QUIC are passed on to the observers. |
| 1076 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); | 1220 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); |
| 1077 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); | 1221 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); |
| 1078 | 1222 |
| 1079 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = | 1223 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = |
| 1080 estimator.CreateSocketPerformanceWatcher( | 1224 estimator.CreateSocketPerformanceWatcher( |
| 1081 SocketPerformanceWatcherFactory::PROTOCOL_TCP); | 1225 SocketPerformanceWatcherFactory::PROTOCOL_TCP); |
| 1082 scoped_ptr<SocketPerformanceWatcher> quic_watcher = | 1226 scoped_ptr<SocketPerformanceWatcher> quic_watcher = |
| 1083 estimator.CreateSocketPerformanceWatcher( | 1227 estimator.CreateSocketPerformanceWatcher( |
| 1084 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); | 1228 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); |
| 1085 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); | 1229 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); |
| 1086 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); | 1230 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); |
| 1087 | 1231 |
| 1088 EXPECT_EQ(4U, rtt_observer.observations().size()); | 1232 EXPECT_EQ(4U, rtt_observer.observations().size()); |
| 1089 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1233 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1090 | 1234 |
| 1091 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); | 1235 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); |
| 1092 EXPECT_EQ(quic_rtt.InMilliseconds(), | 1236 EXPECT_EQ(quic_rtt.InMilliseconds(), |
| 1093 rtt_observer.observations().at(3).rtt_ms); | 1237 rtt_observer.observations().at(3).rtt_ms); |
| 1094 } | 1238 } |
| 1095 | 1239 |
| 1096 } // namespace net | 1240 } // namespace net |
| OLD | NEW |