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