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 <stddef.h> | |
| 7 #include <stdint.h> | 8 #include <stdint.h> |
|
bengr
2016/02/26 23:07:04
Add a blank line after.
tbansal1
2016/02/27 00:03:54
Done.
| |
| 8 #include <limits> | 9 #include <limits> |
| 9 #include <map> | 10 #include <map> |
| 10 #include <utility> | 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 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/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/metrics/histogram_samples.h" | 18 #include "base/metrics/histogram_samples.h" |
| 18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/test/histogram_tester.h" | 21 #include "base/test/histogram_tester.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "net/base/external_estimate_provider.h" | 24 #include "net/base/external_estimate_provider.h" |
| 24 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
| 27 #include "net/base/socket_performance_watcher_factory.h" | |
| 26 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 27 #include "net/test/embedded_test_server/embedded_test_server.h" | 29 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 28 #include "net/test/embedded_test_server/http_request.h" | 30 #include "net/test/embedded_test_server/http_request.h" |
| 29 #include "net/test/embedded_test_server/http_response.h" | 31 #include "net/test/embedded_test_server/http_response.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 int32_t throughput_kbps, | 152 int32_t throughput_kbps, |
| 151 const base::TimeTicks& timestamp, | 153 const base::TimeTicks& timestamp, |
| 152 net::NetworkQualityEstimator::ObservationSource source) override { | 154 net::NetworkQualityEstimator::ObservationSource source) override { |
| 153 observations_.push_back(Observation(throughput_kbps, timestamp, source)); | 155 observations_.push_back(Observation(throughput_kbps, timestamp, source)); |
| 154 } | 156 } |
| 155 | 157 |
| 156 private: | 158 private: |
| 157 std::vector<Observation> observations_; | 159 std::vector<Observation> observations_; |
| 158 }; | 160 }; |
| 159 | 161 |
| 162 class TestPacketLossObserver | |
| 163 : public net::NetworkQualityEstimator::PacketLossObserver { | |
| 164 public: | |
| 165 struct Observation { | |
| 166 Observation(size_t packets_lost, | |
| 167 size_t packets_received_in_order, | |
| 168 size_t packets_received_out_of_order, | |
| 169 const base::TimeTicks& ts, | |
| 170 net::NetworkQualityEstimator::ObservationSource src) | |
| 171 : packets_lost(packets_lost), | |
| 172 packets_received_in_order(packets_received_in_order), | |
| 173 packets_received_out_of_order(packets_received_out_of_order), | |
| 174 timestamp(ts), | |
| 175 source(src) {} | |
| 176 size_t packets_lost; | |
| 177 size_t packets_received_in_order; | |
| 178 size_t packets_received_out_of_order; | |
| 179 base::TimeTicks timestamp; | |
| 180 net::NetworkQualityEstimator::ObservationSource source; | |
| 181 }; | |
| 182 | |
| 183 std::vector<Observation>& observations() { return observations_; } | |
| 184 | |
| 185 // PacketLossObserver implementation: | |
| 186 void OnPacketLossObservation( | |
| 187 size_t packets_lost, | |
| 188 size_t packets_received_in_order, | |
| 189 size_t packets_received_out_of_order, | |
| 190 const base::TimeTicks& timestamp, | |
| 191 net::NetworkQualityEstimator::ObservationSource source) override { | |
| 192 observations_.push_back(Observation(packets_lost, packets_received_in_order, | |
| 193 packets_received_out_of_order, | |
| 194 timestamp, source)); | |
| 195 } | |
| 196 | |
| 197 private: | |
| 198 std::vector<Observation> observations_; | |
| 199 }; | |
| 200 | |
| 160 } // namespace | 201 } // namespace |
| 161 | 202 |
| 162 namespace net { | 203 namespace net { |
| 163 | 204 |
| 164 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 205 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { |
| 165 base::HistogramTester histogram_tester; | 206 base::HistogramTester histogram_tester; |
| 166 // Enable requests to local host to be used for network quality estimation. | 207 // Enable requests to local host to be used for network quality estimation. |
| 167 std::map<std::string, std::string> variation_params; | 208 std::map<std::string, std::string> variation_params; |
| 168 TestNetworkQualityEstimator estimator(variation_params); | 209 TestNetworkQualityEstimator estimator(variation_params); |
| 169 | 210 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 289 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 249 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 290 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
| 250 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 291 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 251 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 292 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 252 base::TimeTicks(), 100)); | 293 base::TimeTicks(), 100)); |
| 253 | 294 |
| 254 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 295 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
| 255 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 296 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 256 } | 297 } |
| 257 | 298 |
| 299 // Tests that packet loss rate is updated correctly. | |
| 300 TEST(NetworkQualityEstimatorTest, TestPacketLossRateUpdates) { | |
| 301 std::map<std::string, std::string> variation_params; | |
| 302 TestNetworkQualityEstimator estimator(variation_params); | |
| 303 | |
| 304 float packet_loss_rate; | |
| 305 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 306 | |
| 307 estimator.OnUpdatedPacketCountAvailable( | |
| 308 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); | |
| 309 | |
| 310 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 311 EXPECT_NEAR(0.0f, packet_loss_rate, 0.001f); | |
| 312 | |
| 313 estimator.SimulateNetworkChangeTo( | |
| 314 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); | |
| 315 | |
| 316 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 317 | |
| 318 // Expecting packet number i, but received packet number i+9. 1 out of 10 | |
| 319 // packets have been received. | |
| 320 estimator.OnUpdatedPacketCountAvailable( | |
| 321 NetworkQualityEstimator::PROTOCOL_QUIC, 9, 1, 0); | |
| 322 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 323 EXPECT_NEAR(9.0f / 10, packet_loss_rate, 0.001f); | |
| 324 | |
| 325 // Expecting packet number i+10, and received packet number i+10. 2 out | |
| 326 // of 11 packets have been received. | |
| 327 estimator.OnUpdatedPacketCountAvailable( | |
| 328 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); | |
| 329 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 330 EXPECT_NEAR(9.0f / 11, packet_loss_rate, 0.001f); | |
| 331 | |
| 332 // Expecting packet number i+11, and received packet number i+11. 3 out | |
| 333 // of 12 packets have been received. | |
| 334 estimator.OnUpdatedPacketCountAvailable( | |
| 335 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); | |
| 336 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 337 EXPECT_NEAR(9.0f / 12, packet_loss_rate, 0.001f); | |
| 338 | |
| 339 // Expecting packet number i+12, but received packet number i. 3 out of | |
| 340 // 12 packets have been received. | |
| 341 estimator.OnUpdatedPacketCountAvailable( | |
| 342 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 0, 1); | |
| 343 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 344 EXPECT_NEAR(9.0f / 12, packet_loss_rate, 0.001f); | |
| 345 } | |
| 346 | |
| 347 // Tests that packet loss UMA histogram is recorded properly. | |
| 348 TEST(NetworkQualityEstimatorTest, TestPacketLossRateHistogram) { | |
| 349 std::map<std::string, std::string> variation_params; | |
|
bengr
2016/02/26 23:07:04
#include <string>
tbansal1
2016/02/27 00:03:54
Done.
| |
| 350 TestNetworkQualityEstimator estimator(variation_params); | |
| 351 | |
| 352 TestDelegate test_delegate; | |
| 353 TestURLRequestContext context(true); | |
| 354 context.set_network_quality_estimator(&estimator); | |
| 355 context.Init(); | |
| 356 | |
| 357 const struct { | |
| 358 bool notify_packet_counts; | |
| 359 bool set_mainframe_flag; | |
| 360 } tests[] = { | |
| 361 { | |
| 362 false, false, | |
| 363 }, | |
| 364 { | |
| 365 false, true, | |
| 366 }, | |
| 367 { | |
| 368 true, false, | |
| 369 }, | |
| 370 { | |
| 371 true, true, | |
| 372 }, | |
| 373 }; | |
| 374 | |
| 375 for (auto test : tests) { | |
| 376 base::HistogramTester histogram_tester; | |
| 377 estimator.SimulateNetworkChangeTo( | |
| 378 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test"); | |
| 379 | |
| 380 float packet_loss_rate; | |
| 381 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 382 | |
| 383 if (test.notify_packet_counts) { | |
| 384 estimator.OnUpdatedPacketCountAvailable( | |
| 385 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); | |
| 386 } | |
| 387 EXPECT_EQ(test.notify_packet_counts, | |
| 388 estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 389 | |
| 390 for (size_t i = 0; i < 2; ++i) { | |
| 391 // Check UMA histograms. | |
| 392 scoped_ptr<URLRequest> request(context.CreateRequest( | |
| 393 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | |
| 394 if (test.set_mainframe_flag) { | |
| 395 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | |
| 396 } | |
| 397 request->Start(); | |
| 398 base::RunLoop().Run(); | |
| 399 size_t expect_histogram_count = | |
| 400 test.notify_packet_counts && test.set_mainframe_flag ? i + 1 : 0; | |
| 401 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.WiFi", | |
| 402 expect_histogram_count); | |
| 403 } | |
| 404 } | |
| 405 } | |
| 406 | |
| 258 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 407 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
| 259 std::map<std::string, std::string> variation_params; | 408 std::map<std::string, std::string> variation_params; |
| 260 TestNetworkQualityEstimator estimator(variation_params); | 409 TestNetworkQualityEstimator estimator(variation_params); |
| 261 | 410 |
| 262 TestDelegate test_delegate; | 411 TestDelegate test_delegate; |
| 263 TestURLRequestContext context(true); | 412 TestURLRequestContext context(true); |
| 264 context.set_network_quality_estimator(&estimator); | 413 context.set_network_quality_estimator(&estimator); |
| 265 context.Init(); | 414 context.Init(); |
| 266 | 415 |
| 267 // Push 10 more observations than the maximum buffer size. | 416 // Push 10 more observations than the maximum buffer size. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 292 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { | 441 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { |
| 293 std::map<std::string, std::string> variation_params; | 442 std::map<std::string, std::string> variation_params; |
| 294 TestNetworkQualityEstimator estimator(variation_params); | 443 TestNetworkQualityEstimator estimator(variation_params); |
| 295 base::TimeTicks now = base::TimeTicks::Now(); | 444 base::TimeTicks now = base::TimeTicks::Now(); |
| 296 | 445 |
| 297 // Network quality should be unavailable when no observations are available. | 446 // Network quality should be unavailable when no observations are available. |
| 298 base::TimeDelta rtt; | 447 base::TimeDelta rtt; |
| 299 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 448 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
| 300 int32_t kbps; | 449 int32_t kbps; |
| 301 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 450 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 451 float packet_loss_rate; | |
| 452 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 302 | 453 |
| 303 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even | 454 // 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. | 455 // samples. This helps in verifying that the order of samples does not matter. |
| 305 for (int i = 1; i <= 99; i += 2) { | 456 for (int i = 1; i <= 99; i += 2) { |
| 306 estimator.downstream_throughput_kbps_observations_.AddObservation( | 457 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 307 NetworkQualityEstimator::ThroughputObservation( | 458 NetworkQualityEstimator::ThroughputObservation( |
| 308 i, now, NetworkQualityEstimator::URL_REQUEST)); | 459 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 309 estimator.rtt_msec_observations_.AddObservation( | 460 estimator.rtt_msec_observations_.AddObservation( |
| 310 NetworkQualityEstimator::RttObservation( | 461 NetworkQualityEstimator::RttObservation( |
| 311 base::TimeDelta::FromMilliseconds(i), now, | 462 base::TimeDelta::FromMilliseconds(i), now, |
| 312 NetworkQualityEstimator::URL_REQUEST)); | 463 NetworkQualityEstimator::URL_REQUEST)); |
| 464 estimator.packet_loss_rate_observations_.AddObservation( | |
| 465 NetworkQualityEstimator::PacketLossRateObservation( | |
| 466 0.0f, now, NetworkQualityEstimator::QUIC)); | |
| 313 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 467 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 314 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 468 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 469 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 315 } | 470 } |
| 316 | 471 |
| 317 for (int i = 2; i <= 100; i += 2) { | 472 for (int i = 2; i <= 100; i += 2) { |
| 318 estimator.downstream_throughput_kbps_observations_.AddObservation( | 473 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 319 NetworkQualityEstimator::ThroughputObservation( | 474 NetworkQualityEstimator::ThroughputObservation( |
| 320 i, now, NetworkQualityEstimator::URL_REQUEST)); | 475 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 321 estimator.rtt_msec_observations_.AddObservation( | 476 estimator.rtt_msec_observations_.AddObservation( |
| 322 NetworkQualityEstimator::RttObservation( | 477 NetworkQualityEstimator::RttObservation( |
| 323 base::TimeDelta::FromMilliseconds(i), now, | 478 base::TimeDelta::FromMilliseconds(i), now, |
| 324 NetworkQualityEstimator::URL_REQUEST)); | 479 NetworkQualityEstimator::URL_REQUEST)); |
| 480 estimator.packet_loss_rate_observations_.AddObservation( | |
| 481 NetworkQualityEstimator::PacketLossRateObservation( | |
| 482 1.0, now, NetworkQualityEstimator::QUIC)); | |
| 325 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 483 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 326 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 484 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 485 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 327 } | 486 } |
| 328 | 487 |
| 329 for (int i = 0; i <= 100; ++i) { | 488 for (int i = 0; i <= 100; ++i) { |
| 330 // Checks if the difference between the two integers is less than 1. This is | 489 // Checks if the difference between the two integers is less than 1. This is |
| 331 // required because computed percentiles may be slightly different from | 490 // required because computed percentiles may be slightly different from |
| 332 // what is expected due to floating point computation errors and integer | 491 // what is expected due to floating point computation errors and integer |
| 333 // rounding off errors. | 492 // rounding off errors. |
| 334 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 493 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 335 base::TimeTicks(), i), | 494 base::TimeTicks(), i), |
| 336 100 - i, 1); | 495 100 - i, 1); |
| 337 EXPECT_NEAR( | 496 EXPECT_NEAR( |
| 338 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 497 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
| 339 i, 1); | 498 i, 1); |
| 340 } | 499 } |
| 341 | 500 |
| 342 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 501 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 343 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 502 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 344 // |network_quality| should be equal to the 50 percentile value. | 503 // |network_quality| should be equal to the 50 percentile value. |
| 345 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 504 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 346 base::TimeTicks(), 50) > 0); | 505 base::TimeTicks(), 50) > 0); |
| 347 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != | 506 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != |
| 348 NetworkQualityEstimator::InvalidRTT()); | 507 NetworkQualityEstimator::InvalidRTT()); |
| 508 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 509 EXPECT_NEAR(0.5f, packet_loss_rate, 0.001f); | |
| 349 } | 510 } |
| 350 | 511 |
| 351 // Verifies that the percentiles are correctly computed. Observations have | 512 // Verifies that the percentiles are correctly computed. Observations have |
| 352 // different timestamps with half the observations being very old and the rest | 513 // different timestamps with half the observations being very old and the rest |
| 353 // of them being very recent. Percentiles should factor in recent observations | 514 // of them being very recent. Percentiles should factor in recent observations |
| 354 // much more heavily than older samples. Kbps percentiles must be in decreasing | 515 // much more heavily than older samples. Kbps percentiles must be in decreasing |
| 355 // order. RTT percentiles must be in increasing order. | 516 // order. RTT percentiles must be in increasing order. |
| 356 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { | 517 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { |
| 357 std::map<std::string, std::string> variation_params; | 518 std::map<std::string, std::string> variation_params; |
| 358 TestNetworkQualityEstimator estimator(variation_params); | 519 TestNetworkQualityEstimator estimator(variation_params); |
| 359 base::TimeTicks now = base::TimeTicks::Now(); | 520 base::TimeTicks now = base::TimeTicks::Now(); |
| 360 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); | 521 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); |
| 361 | 522 |
| 523 float packet_loss_rate; | |
| 524 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 525 | |
| 362 // First 50 samples have very old timestamp. | 526 // First 50 samples have very old timestamp. |
| 363 for (int i = 1; i <= 50; ++i) { | 527 for (int i = 1; i <= 50; ++i) { |
| 364 estimator.downstream_throughput_kbps_observations_.AddObservation( | 528 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 365 NetworkQualityEstimator::ThroughputObservation( | 529 NetworkQualityEstimator::ThroughputObservation( |
| 366 i, very_old, NetworkQualityEstimator::URL_REQUEST)); | 530 i, very_old, NetworkQualityEstimator::URL_REQUEST)); |
| 367 estimator.rtt_msec_observations_.AddObservation( | 531 estimator.rtt_msec_observations_.AddObservation( |
| 368 NetworkQualityEstimator::RttObservation( | 532 NetworkQualityEstimator::RttObservation( |
| 369 base::TimeDelta::FromMilliseconds(i), very_old, | 533 base::TimeDelta::FromMilliseconds(i), very_old, |
| 370 NetworkQualityEstimator::URL_REQUEST)); | 534 NetworkQualityEstimator::URL_REQUEST)); |
| 535 estimator.packet_loss_rate_observations_.AddObservation( | |
| 536 NetworkQualityEstimator::PacketLossRateObservation( | |
| 537 0.0f, very_old, NetworkQualityEstimator::QUIC)); | |
| 371 } | 538 } |
| 372 | 539 |
| 373 // Next 50 (i.e., from 51 to 100) have recent timestamp. | 540 // Next 50 (i.e., from 51 to 100) have recent timestamp. |
| 374 for (int i = 51; i <= 100; ++i) { | 541 for (int i = 51; i <= 100; ++i) { |
| 375 estimator.downstream_throughput_kbps_observations_.AddObservation( | 542 estimator.downstream_throughput_kbps_observations_.AddObservation( |
| 376 NetworkQualityEstimator::ThroughputObservation( | 543 NetworkQualityEstimator::ThroughputObservation( |
| 377 i, now, NetworkQualityEstimator::URL_REQUEST)); | 544 i, now, NetworkQualityEstimator::URL_REQUEST)); |
| 378 estimator.rtt_msec_observations_.AddObservation( | 545 estimator.rtt_msec_observations_.AddObservation( |
| 379 NetworkQualityEstimator::RttObservation( | 546 NetworkQualityEstimator::RttObservation( |
| 380 base::TimeDelta::FromMilliseconds(i), now, | 547 base::TimeDelta::FromMilliseconds(i), now, |
| 381 NetworkQualityEstimator::URL_REQUEST)); | 548 NetworkQualityEstimator::URL_REQUEST)); |
| 549 estimator.packet_loss_rate_observations_.AddObservation( | |
| 550 NetworkQualityEstimator::PacketLossRateObservation( | |
| 551 1.0, now, NetworkQualityEstimator::QUIC)); | |
| 382 } | 552 } |
| 383 | 553 |
| 384 // Older samples have very little weight. So, all percentiles are >= 51 | 554 // Older samples have very little weight. So, all percentiles are >= 51 |
| 385 // (lowest value among recent observations). | 555 // (lowest value among recent observations). |
| 386 for (int i = 1; i < 100; ++i) { | 556 for (int i = 1; i < 100; ++i) { |
| 387 // Checks if the difference between the two integers is less than 1. This is | 557 // Checks if the difference between the two integers is less than 1. This is |
| 388 // required because computed percentiles may be slightly different from | 558 // required because computed percentiles may be slightly different from |
| 389 // what is expected due to floating point computation errors and integer | 559 // what is expected due to floating point computation errors and integer |
| 390 // rounding off errors. | 560 // rounding off errors. |
| 391 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 561 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 392 base::TimeTicks(), i), | 562 base::TimeTicks(), i), |
| 393 51 + 0.49 * (100 - i), 1); | 563 51 + 0.49 * (100 - i), 1); |
| 394 EXPECT_NEAR( | 564 EXPECT_NEAR( |
| 395 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 565 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
| 396 51 + 0.49 * i, 1); | 566 51 + 0.49 * i, 1); |
| 397 } | 567 } |
| 398 | 568 |
| 399 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 569 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| 400 estimator.GetRTTEstimateInternal( | 570 estimator.GetRTTEstimateInternal( |
| 401 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 571 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
| 402 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 572 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
| 403 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 573 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 404 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 574 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
| 575 | |
| 576 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 577 EXPECT_NEAR(1.0, packet_loss_rate, 0.001f); | |
| 405 } | 578 } |
| 406 | 579 |
| 407 // This test notifies NetworkQualityEstimator of received data. Next, | 580 // This test notifies NetworkQualityEstimator of received data. Next, |
| 408 // throughput and RTT percentiles are checked for correctness by doing simple | 581 // throughput and RTT percentiles are checked for correctness by doing simple |
| 409 // verifications. | 582 // verifications. |
| 410 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 583 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { |
| 411 std::map<std::string, std::string> variation_params; | 584 std::map<std::string, std::string> variation_params; |
| 412 TestNetworkQualityEstimator estimator(variation_params); | 585 TestNetworkQualityEstimator estimator(variation_params); |
| 413 | 586 |
| 414 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 587 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 } | 702 } |
| 530 | 703 |
| 531 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { | 704 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { |
| 532 // Verifies if |weight_multiplier_per_second_| is set to correct value for | 705 // Verifies if |weight_multiplier_per_second_| is set to correct value for |
| 533 // various values of half life parameter. | 706 // various values of half life parameter. |
| 534 std::map<std::string, std::string> variation_params; | 707 std::map<std::string, std::string> variation_params; |
| 535 { | 708 { |
| 536 // Half life parameter is not set. Default value of | 709 // Half life parameter is not set. Default value of |
| 537 // |weight_multiplier_per_second_| should be used. | 710 // |weight_multiplier_per_second_| should be used. |
| 538 TestNetworkQualityEstimator estimator(variation_params); | 711 TestNetworkQualityEstimator estimator(variation_params); |
| 539 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | 712 EXPECT_NEAR(0.988f, estimator.downstream_throughput_kbps_observations_ |
| 540 .weight_multiplier_per_second_, | 713 .weight_multiplier_per_second_, |
| 541 0.001); | 714 0.001f); |
| 542 } | 715 } |
| 543 | 716 |
| 544 variation_params["HalfLifeSeconds"] = "-100"; | 717 variation_params["HalfLifeSeconds"] = "-100"; |
| 545 { | 718 { |
| 546 // Half life parameter is set to a negative value. Default value of | 719 // Half life parameter is set to a negative value. Default value of |
| 547 // |weight_multiplier_per_second_| should be used. | 720 // |weight_multiplier_per_second_| should be used. |
| 548 TestNetworkQualityEstimator estimator(variation_params); | 721 TestNetworkQualityEstimator estimator(variation_params); |
| 549 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | 722 EXPECT_NEAR(0.988f, estimator.downstream_throughput_kbps_observations_ |
| 550 .weight_multiplier_per_second_, | 723 .weight_multiplier_per_second_, |
| 551 0.001); | 724 0.001f); |
| 552 } | 725 } |
| 553 | 726 |
| 554 variation_params["HalfLifeSeconds"] = "0"; | 727 variation_params["HalfLifeSeconds"] = "0"; |
| 555 { | 728 { |
| 556 // Half life parameter is set to zero. Default value of | 729 // Half life parameter is set to zero. Default value of |
| 557 // |weight_multiplier_per_second_| should be used. | 730 // |weight_multiplier_per_second_| should be used. |
| 558 TestNetworkQualityEstimator estimator(variation_params); | 731 TestNetworkQualityEstimator estimator(variation_params); |
| 559 EXPECT_NEAR(0.988, estimator.downstream_throughput_kbps_observations_ | 732 EXPECT_NEAR(0.988f, estimator.downstream_throughput_kbps_observations_ |
| 560 .weight_multiplier_per_second_, | 733 .weight_multiplier_per_second_, |
| 561 0.001); | 734 0.001f); |
| 562 } | 735 } |
| 563 | 736 |
| 564 variation_params["HalfLifeSeconds"] = "10"; | 737 variation_params["HalfLifeSeconds"] = "10"; |
| 565 { | 738 { |
| 566 // Half life parameter is set to a valid value. | 739 // Half life parameter is set to a valid value. |
| 567 TestNetworkQualityEstimator estimator(variation_params); | 740 TestNetworkQualityEstimator estimator(variation_params); |
| 568 EXPECT_NEAR(0.933, estimator.downstream_throughput_kbps_observations_ | 741 EXPECT_NEAR(0.933f, estimator.downstream_throughput_kbps_observations_ |
| 569 .weight_multiplier_per_second_, | 742 .weight_multiplier_per_second_, |
| 570 0.001); | 743 0.001f); |
| 571 } | 744 } |
| 572 } | 745 } |
| 573 | 746 |
| 574 // Test if the network estimates are cached when network change notification | 747 // Test if the network estimates are cached when network change notification |
| 575 // is invoked. | 748 // is invoked. |
| 576 TEST(NetworkQualityEstimatorTest, TestCaching) { | 749 TEST(NetworkQualityEstimatorTest, TestCaching) { |
| 577 std::map<std::string, std::string> variation_params; | 750 std::map<std::string, std::string> variation_params; |
| 578 TestNetworkQualityEstimator estimator(variation_params); | 751 TestNetworkQualityEstimator estimator(variation_params); |
| 579 size_t expected_cache_size = 0; | 752 size_t expected_cache_size = 0; |
| 580 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); | 753 EXPECT_EQ(expected_cache_size, estimator.cached_network_qualities_.size()); |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1019 request->Start(); | 1192 request->Start(); |
| 1020 base::RunLoop().Run(); | 1193 base::RunLoop().Run(); |
| 1021 | 1194 |
| 1022 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); | 1195 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); |
| 1023 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | 1196 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); |
| 1024 } | 1197 } |
| 1025 | 1198 |
| 1026 TEST(NetworkQualityEstimatorTest, TestObservers) { | 1199 TEST(NetworkQualityEstimatorTest, TestObservers) { |
| 1027 TestRTTObserver rtt_observer; | 1200 TestRTTObserver rtt_observer; |
| 1028 TestThroughputObserver throughput_observer; | 1201 TestThroughputObserver throughput_observer; |
| 1202 TestPacketLossObserver packet_loss_observer; | |
| 1029 std::map<std::string, std::string> variation_params; | 1203 std::map<std::string, std::string> variation_params; |
| 1030 TestNetworkQualityEstimator estimator(variation_params); | 1204 TestNetworkQualityEstimator estimator(variation_params); |
| 1031 estimator.AddRTTObserver(&rtt_observer); | 1205 estimator.AddRTTObserver(&rtt_observer); |
| 1032 estimator.AddThroughputObserver(&throughput_observer); | 1206 estimator.AddThroughputObserver(&throughput_observer); |
| 1207 estimator.AddPacketLossObserver(&packet_loss_observer); | |
| 1033 | 1208 |
| 1034 TestDelegate test_delegate; | 1209 TestDelegate test_delegate; |
| 1035 TestURLRequestContext context(true); | 1210 TestURLRequestContext context(true); |
| 1036 context.set_network_quality_estimator(&estimator); | 1211 context.set_network_quality_estimator(&estimator); |
| 1037 context.Init(); | 1212 context.Init(); |
| 1038 | 1213 |
| 1039 EXPECT_EQ(0U, rtt_observer.observations().size()); | 1214 EXPECT_EQ(0U, rtt_observer.observations().size()); |
| 1040 EXPECT_EQ(0U, throughput_observer.observations().size()); | 1215 EXPECT_EQ(0U, throughput_observer.observations().size()); |
| 1041 base::TimeTicks then = base::TimeTicks::Now(); | 1216 base::TimeTicks then = base::TimeTicks::Now(); |
| 1042 | 1217 |
| 1043 scoped_ptr<URLRequest> request(context.CreateRequest( | 1218 scoped_ptr<URLRequest> request(context.CreateRequest( |
| 1044 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1219 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1045 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1220 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1046 request->Start(); | 1221 request->Start(); |
| 1047 base::RunLoop().Run(); | 1222 base::RunLoop().Run(); |
| 1048 | 1223 |
| 1049 scoped_ptr<URLRequest> request2(context.CreateRequest( | 1224 scoped_ptr<URLRequest> request2(context.CreateRequest( |
| 1050 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1225 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1051 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1226 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
| 1052 request2->Start(); | 1227 request2->Start(); |
| 1053 base::RunLoop().Run(); | 1228 base::RunLoop().Run(); |
| 1054 | 1229 |
| 1230 float packet_loss_rate; | |
| 1231 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 1232 | |
| 1233 estimator.OnUpdatedPacketCountAvailable( | |
| 1234 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); | |
| 1235 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); | |
| 1236 EXPECT_NEAR(0.0f, packet_loss_rate, 0.001f); | |
| 1237 | |
| 1055 // Both RTT and downstream throughput should be updated. | 1238 // Both RTT and downstream throughput should be updated. |
| 1056 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), | 1239 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), |
| 1057 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 1240 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
| 1058 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, | 1241 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, |
| 1059 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 1242 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
| 1060 base::TimeTicks(), 100)); | 1243 base::TimeTicks(), 100)); |
| 1061 | 1244 |
| 1062 EXPECT_EQ(2U, rtt_observer.observations().size()); | 1245 EXPECT_EQ(2U, rtt_observer.observations().size()); |
| 1063 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1246 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1247 ASSERT_EQ(1U, packet_loss_observer.observations().size()); | |
| 1064 for (auto observation : rtt_observer.observations()) { | 1248 for (auto observation : rtt_observer.observations()) { |
| 1065 EXPECT_LE(0, observation.rtt_ms); | 1249 EXPECT_LE(0, observation.rtt_ms); |
| 1066 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1250 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1067 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1251 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1068 } | 1252 } |
| 1069 for (auto observation : throughput_observer.observations()) { | 1253 for (auto observation : throughput_observer.observations()) { |
| 1070 EXPECT_LE(0, observation.throughput_kbps); | 1254 EXPECT_LE(0, observation.throughput_kbps); |
| 1071 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1255 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1072 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1256 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
| 1073 } | 1257 } |
| 1258 for (auto observation : packet_loss_observer.observations()) { | |
| 1259 EXPECT_LE(0u, observation.packets_lost); | |
| 1260 EXPECT_LE(1u, observation.packets_received_in_order); | |
| 1261 EXPECT_LE(0u, observation.packets_received_out_of_order); | |
| 1262 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | |
| 1263 EXPECT_EQ(NetworkQualityEstimator::QUIC, observation.source); | |
| 1264 } | |
| 1074 | 1265 |
| 1075 // Verify that observations from TCP and QUIC are passed on to the observers. | 1266 // Verify that observations from TCP and QUIC are passed on to the observers. |
| 1076 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); | 1267 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); |
| 1077 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); | 1268 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); |
| 1078 | 1269 |
| 1079 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = | 1270 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = |
| 1080 estimator.CreateSocketPerformanceWatcher( | 1271 estimator.CreateSocketPerformanceWatcher( |
| 1081 SocketPerformanceWatcherFactory::PROTOCOL_TCP); | 1272 SocketPerformanceWatcherFactory::PROTOCOL_TCP); |
| 1082 scoped_ptr<SocketPerformanceWatcher> quic_watcher = | 1273 scoped_ptr<SocketPerformanceWatcher> quic_watcher = |
| 1083 estimator.CreateSocketPerformanceWatcher( | 1274 estimator.CreateSocketPerformanceWatcher( |
| 1084 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); | 1275 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); |
| 1085 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); | 1276 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); |
| 1086 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); | 1277 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); |
| 1087 | 1278 |
| 1088 EXPECT_EQ(4U, rtt_observer.observations().size()); | 1279 EXPECT_EQ(4U, rtt_observer.observations().size()); |
| 1089 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1280 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1090 | 1281 |
| 1091 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); | 1282 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); |
| 1092 EXPECT_EQ(quic_rtt.InMilliseconds(), | 1283 EXPECT_EQ(quic_rtt.InMilliseconds(), |
| 1093 rtt_observer.observations().at(3).rtt_ms); | 1284 rtt_observer.observations().at(3).rtt_ms); |
| 1094 } | 1285 } |
| 1095 | 1286 |
| 1096 } // namespace net | 1287 } // namespace net |
| OLD | NEW |