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 = NetworkQualityEstimator::kInvalidPacketLossRate; |
| 306 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 307 |
| 308 estimator.OnUpdatedPacketCountAvailable( |
| 309 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); |
| 310 |
| 311 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 312 EXPECT_NEAR(0.0, packet_loss_rate, 0.001); |
| 313 |
| 314 // Check UMA histograms. |
| 315 estimator.SimulateNetworkChangeTo( |
| 316 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 317 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.Unknown", 1); |
| 318 |
| 319 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 320 |
| 321 // Expecting packet number p, but received packet number p+9. 1 out of 10 |
| 322 // packets were received. |
| 323 estimator.OnUpdatedPacketCountAvailable( |
| 324 NetworkQualityEstimator::PROTOCOL_QUIC, 9, 1, 0); |
| 325 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 326 EXPECT_NEAR(9.0 / 10, packet_loss_rate, 0.001); |
| 327 |
| 328 // Expecting packet number p+10, and received packet number p+10. Now, 2 out |
| 329 // of 11 packets were received. |
| 330 estimator.OnUpdatedPacketCountAvailable( |
| 331 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); |
| 332 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 333 EXPECT_NEAR(9.0 / 11, packet_loss_rate, 0.001); |
| 334 |
| 335 // Expecting packet number p+11, and received packet number p+11. Now, 3 out |
| 336 // of 12 packets were received. |
| 337 estimator.OnUpdatedPacketCountAvailable( |
| 338 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 0); |
| 339 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 340 EXPECT_NEAR(9.0 / 12, packet_loss_rate, 0.001); |
| 341 |
| 342 // Expecting packet number p+12, but received packet number p. Still, 3 out of |
| 343 // 12 packets were received. |
| 344 estimator.OnUpdatedPacketCountAvailable( |
| 345 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 0, 1); |
| 346 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 347 EXPECT_NEAR(9.0 / 12, packet_loss_rate, 0.001); |
| 348 |
| 349 estimator.SimulateNetworkChangeTo( |
| 350 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string()); |
| 351 histogram_tester.ExpectTotalCount("NQE.PacketLossRate.WiFi", 1); |
| 352 |
| 353 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 354 } |
| 355 |
258 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 356 TEST(NetworkQualityEstimatorTest, StoreObservations) { |
259 std::map<std::string, std::string> variation_params; | 357 std::map<std::string, std::string> variation_params; |
260 TestNetworkQualityEstimator estimator(variation_params); | 358 TestNetworkQualityEstimator estimator(variation_params); |
261 | 359 |
262 TestDelegate test_delegate; | 360 TestDelegate test_delegate; |
263 TestURLRequestContext context(true); | 361 TestURLRequestContext context(true); |
264 context.set_network_quality_estimator(&estimator); | 362 context.set_network_quality_estimator(&estimator); |
265 context.Init(); | 363 context.Init(); |
266 | 364 |
267 // Push 10 more observations than the maximum buffer size. | 365 // Push 10 more observations than the maximum buffer size. |
(...skipping 24 matching lines...) Expand all Loading... |
292 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { | 390 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { |
293 std::map<std::string, std::string> variation_params; | 391 std::map<std::string, std::string> variation_params; |
294 TestNetworkQualityEstimator estimator(variation_params); | 392 TestNetworkQualityEstimator estimator(variation_params); |
295 base::TimeTicks now = base::TimeTicks::Now(); | 393 base::TimeTicks now = base::TimeTicks::Now(); |
296 | 394 |
297 // Network quality should be unavailable when no observations are available. | 395 // Network quality should be unavailable when no observations are available. |
298 base::TimeDelta rtt; | 396 base::TimeDelta rtt; |
299 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 397 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); |
300 int32_t kbps; | 398 int32_t kbps; |
301 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 399 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 400 float packet_loss_rate = NetworkQualityEstimator::kInvalidPacketLossRate; |
| 401 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
302 | 402 |
303 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even | 403 // 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. | 404 // samples. This helps in verifying that the order of samples does not matter. |
305 for (int i = 1; i <= 99; i += 2) { | 405 for (int i = 1; i <= 99; i += 2) { |
306 estimator.downstream_throughput_kbps_observations_.AddObservation( | 406 estimator.downstream_throughput_kbps_observations_.AddObservation( |
307 NetworkQualityEstimator::ThroughputObservation( | 407 NetworkQualityEstimator::ThroughputObservation( |
308 i, now, NetworkQualityEstimator::URL_REQUEST)); | 408 i, now, NetworkQualityEstimator::URL_REQUEST)); |
309 estimator.rtt_msec_observations_.AddObservation( | 409 estimator.rtt_msec_observations_.AddObservation( |
310 NetworkQualityEstimator::RttObservation( | 410 NetworkQualityEstimator::RttObservation( |
311 base::TimeDelta::FromMilliseconds(i), now, | 411 base::TimeDelta::FromMilliseconds(i), now, |
312 NetworkQualityEstimator::URL_REQUEST)); | 412 NetworkQualityEstimator::URL_REQUEST)); |
| 413 estimator.packet_loss_rate_observations_.AddObservation( |
| 414 NetworkQualityEstimator::PacketLossObservation( |
| 415 0.0, now, NetworkQualityEstimator::QUIC)); |
313 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 416 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
314 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 417 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 418 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
315 } | 419 } |
316 | 420 |
317 for (int i = 2; i <= 100; i += 2) { | 421 for (int i = 2; i <= 100; i += 2) { |
318 estimator.downstream_throughput_kbps_observations_.AddObservation( | 422 estimator.downstream_throughput_kbps_observations_.AddObservation( |
319 NetworkQualityEstimator::ThroughputObservation( | 423 NetworkQualityEstimator::ThroughputObservation( |
320 i, now, NetworkQualityEstimator::URL_REQUEST)); | 424 i, now, NetworkQualityEstimator::URL_REQUEST)); |
321 estimator.rtt_msec_observations_.AddObservation( | 425 estimator.rtt_msec_observations_.AddObservation( |
322 NetworkQualityEstimator::RttObservation( | 426 NetworkQualityEstimator::RttObservation( |
323 base::TimeDelta::FromMilliseconds(i), now, | 427 base::TimeDelta::FromMilliseconds(i), now, |
324 NetworkQualityEstimator::URL_REQUEST)); | 428 NetworkQualityEstimator::URL_REQUEST)); |
| 429 estimator.packet_loss_rate_observations_.AddObservation( |
| 430 NetworkQualityEstimator::PacketLossObservation( |
| 431 1.0, now, NetworkQualityEstimator::QUIC)); |
325 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 432 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
326 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 433 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 434 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
327 } | 435 } |
328 | 436 |
329 for (int i = 0; i <= 100; ++i) { | 437 for (int i = 0; i <= 100; ++i) { |
330 // Checks if the difference between the two integers is less than 1. This is | 438 // Checks if the difference between the two integers is less than 1. This is |
331 // required because computed percentiles may be slightly different from | 439 // required because computed percentiles may be slightly different from |
332 // what is expected due to floating point computation errors and integer | 440 // what is expected due to floating point computation errors and integer |
333 // rounding off errors. | 441 // rounding off errors. |
334 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 442 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
335 base::TimeTicks(), i), | 443 base::TimeTicks(), i), |
336 100 - i, 1); | 444 100 - i, 1); |
337 EXPECT_NEAR( | 445 EXPECT_NEAR( |
338 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 446 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
339 i, 1); | 447 i, 1); |
340 } | 448 } |
341 | 449 |
342 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 450 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
343 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 451 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
344 // |network_quality| should be equal to the 50 percentile value. | 452 // |network_quality| should be equal to the 50 percentile value. |
345 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 453 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
346 base::TimeTicks(), 50) > 0); | 454 base::TimeTicks(), 50) > 0); |
347 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != | 455 EXPECT_TRUE(estimator.GetRTTEstimateInternal(base::TimeTicks(), 50) != |
348 NetworkQualityEstimator::InvalidRTT()); | 456 NetworkQualityEstimator::InvalidRTT()); |
| 457 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 458 EXPECT_NEAR(0.5, packet_loss_rate, 0.001); |
349 } | 459 } |
350 | 460 |
351 // Verifies that the percentiles are correctly computed. Observations have | 461 // Verifies that the percentiles are correctly computed. Observations have |
352 // different timestamps with half the observations being very old and the rest | 462 // different timestamps with half the observations being very old and the rest |
353 // of them being very recent. Percentiles should factor in recent observations | 463 // of them being very recent. Percentiles should factor in recent observations |
354 // much more heavily than older samples. Kbps percentiles must be in decreasing | 464 // much more heavily than older samples. Kbps percentiles must be in decreasing |
355 // order. RTT percentiles must be in increasing order. | 465 // order. RTT percentiles must be in increasing order. |
356 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { | 466 TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) { |
357 std::map<std::string, std::string> variation_params; | 467 std::map<std::string, std::string> variation_params; |
358 TestNetworkQualityEstimator estimator(variation_params); | 468 TestNetworkQualityEstimator estimator(variation_params); |
359 base::TimeTicks now = base::TimeTicks::Now(); | 469 base::TimeTicks now = base::TimeTicks::Now(); |
360 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); | 470 base::TimeTicks very_old = base::TimeTicks::UnixEpoch(); |
361 | 471 |
| 472 float packet_loss_rate = NetworkQualityEstimator::kInvalidPacketLossRate; |
| 473 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 474 |
362 // First 50 samples have very old timestamp. | 475 // First 50 samples have very old timestamp. |
363 for (int i = 1; i <= 50; ++i) { | 476 for (int i = 1; i <= 50; ++i) { |
364 estimator.downstream_throughput_kbps_observations_.AddObservation( | 477 estimator.downstream_throughput_kbps_observations_.AddObservation( |
365 NetworkQualityEstimator::ThroughputObservation( | 478 NetworkQualityEstimator::ThroughputObservation( |
366 i, very_old, NetworkQualityEstimator::URL_REQUEST)); | 479 i, very_old, NetworkQualityEstimator::URL_REQUEST)); |
367 estimator.rtt_msec_observations_.AddObservation( | 480 estimator.rtt_msec_observations_.AddObservation( |
368 NetworkQualityEstimator::RttObservation( | 481 NetworkQualityEstimator::RttObservation( |
369 base::TimeDelta::FromMilliseconds(i), very_old, | 482 base::TimeDelta::FromMilliseconds(i), very_old, |
370 NetworkQualityEstimator::URL_REQUEST)); | 483 NetworkQualityEstimator::URL_REQUEST)); |
| 484 estimator.packet_loss_rate_observations_.AddObservation( |
| 485 NetworkQualityEstimator::PacketLossObservation( |
| 486 0.0, very_old, NetworkQualityEstimator::QUIC)); |
371 } | 487 } |
372 | 488 |
373 // Next 50 (i.e., from 51 to 100) have recent timestamp. | 489 // Next 50 (i.e., from 51 to 100) have recent timestamp. |
374 for (int i = 51; i <= 100; ++i) { | 490 for (int i = 51; i <= 100; ++i) { |
375 estimator.downstream_throughput_kbps_observations_.AddObservation( | 491 estimator.downstream_throughput_kbps_observations_.AddObservation( |
376 NetworkQualityEstimator::ThroughputObservation( | 492 NetworkQualityEstimator::ThroughputObservation( |
377 i, now, NetworkQualityEstimator::URL_REQUEST)); | 493 i, now, NetworkQualityEstimator::URL_REQUEST)); |
378 estimator.rtt_msec_observations_.AddObservation( | 494 estimator.rtt_msec_observations_.AddObservation( |
379 NetworkQualityEstimator::RttObservation( | 495 NetworkQualityEstimator::RttObservation( |
380 base::TimeDelta::FromMilliseconds(i), now, | 496 base::TimeDelta::FromMilliseconds(i), now, |
381 NetworkQualityEstimator::URL_REQUEST)); | 497 NetworkQualityEstimator::URL_REQUEST)); |
| 498 estimator.packet_loss_rate_observations_.AddObservation( |
| 499 NetworkQualityEstimator::PacketLossObservation( |
| 500 1.0, now, NetworkQualityEstimator::QUIC)); |
382 } | 501 } |
383 | 502 |
384 // Older samples have very little weight. So, all percentiles are >= 51 | 503 // Older samples have very little weight. So, all percentiles are >= 51 |
385 // (lowest value among recent observations). | 504 // (lowest value among recent observations). |
386 for (int i = 1; i < 100; ++i) { | 505 for (int i = 1; i < 100; ++i) { |
387 // Checks if the difference between the two integers is less than 1. This is | 506 // Checks if the difference between the two integers is less than 1. This is |
388 // required because computed percentiles may be slightly different from | 507 // required because computed percentiles may be slightly different from |
389 // what is expected due to floating point computation errors and integer | 508 // what is expected due to floating point computation errors and integer |
390 // rounding off errors. | 509 // rounding off errors. |
391 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 510 EXPECT_NEAR(estimator.GetDownlinkThroughputKbpsEstimateInternal( |
392 base::TimeTicks(), i), | 511 base::TimeTicks(), i), |
393 51 + 0.49 * (100 - i), 1); | 512 51 + 0.49 * (100 - i), 1); |
394 EXPECT_NEAR( | 513 EXPECT_NEAR( |
395 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), | 514 estimator.GetRTTEstimateInternal(base::TimeTicks(), i).InMilliseconds(), |
396 51 + 0.49 * i, 1); | 515 51 + 0.49 * i, 1); |
397 } | 516 } |
398 | 517 |
399 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 518 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
400 estimator.GetRTTEstimateInternal( | 519 estimator.GetRTTEstimateInternal( |
401 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 520 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
402 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 521 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, |
403 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 522 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
404 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 523 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); |
| 524 |
| 525 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 526 EXPECT_NEAR(1.0, packet_loss_rate, 0.001); |
405 } | 527 } |
406 | 528 |
407 // This test notifies NetworkQualityEstimator of received data. Next, | 529 // This test notifies NetworkQualityEstimator of received data. Next, |
408 // throughput and RTT percentiles are checked for correctness by doing simple | 530 // throughput and RTT percentiles are checked for correctness by doing simple |
409 // verifications. | 531 // verifications. |
410 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 532 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { |
411 std::map<std::string, std::string> variation_params; | 533 std::map<std::string, std::string> variation_params; |
412 TestNetworkQualityEstimator estimator(variation_params); | 534 TestNetworkQualityEstimator estimator(variation_params); |
413 | 535 |
414 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 536 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1019 request->Start(); | 1141 request->Start(); |
1020 base::RunLoop().Run(); | 1142 base::RunLoop().Run(); |
1021 | 1143 |
1022 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); | 1144 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); |
1023 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | 1145 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); |
1024 } | 1146 } |
1025 | 1147 |
1026 TEST(NetworkQualityEstimatorTest, TestObservers) { | 1148 TEST(NetworkQualityEstimatorTest, TestObservers) { |
1027 TestRTTObserver rtt_observer; | 1149 TestRTTObserver rtt_observer; |
1028 TestThroughputObserver throughput_observer; | 1150 TestThroughputObserver throughput_observer; |
| 1151 TestPacketLossObserver packet_loss_observer; |
1029 std::map<std::string, std::string> variation_params; | 1152 std::map<std::string, std::string> variation_params; |
1030 TestNetworkQualityEstimator estimator(variation_params); | 1153 TestNetworkQualityEstimator estimator(variation_params); |
1031 estimator.AddRTTObserver(&rtt_observer); | 1154 estimator.AddRTTObserver(&rtt_observer); |
1032 estimator.AddThroughputObserver(&throughput_observer); | 1155 estimator.AddThroughputObserver(&throughput_observer); |
| 1156 estimator.AddPacketLossObserver(&packet_loss_observer); |
1033 | 1157 |
1034 TestDelegate test_delegate; | 1158 TestDelegate test_delegate; |
1035 TestURLRequestContext context(true); | 1159 TestURLRequestContext context(true); |
1036 context.set_network_quality_estimator(&estimator); | 1160 context.set_network_quality_estimator(&estimator); |
1037 context.Init(); | 1161 context.Init(); |
1038 | 1162 |
1039 EXPECT_EQ(0U, rtt_observer.observations().size()); | 1163 EXPECT_EQ(0U, rtt_observer.observations().size()); |
1040 EXPECT_EQ(0U, throughput_observer.observations().size()); | 1164 EXPECT_EQ(0U, throughput_observer.observations().size()); |
1041 base::TimeTicks then = base::TimeTicks::Now(); | 1165 base::TimeTicks then = base::TimeTicks::Now(); |
1042 | 1166 |
1043 scoped_ptr<URLRequest> request(context.CreateRequest( | 1167 scoped_ptr<URLRequest> request(context.CreateRequest( |
1044 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1168 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
1045 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1169 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
1046 request->Start(); | 1170 request->Start(); |
1047 base::RunLoop().Run(); | 1171 base::RunLoop().Run(); |
1048 | 1172 |
1049 scoped_ptr<URLRequest> request2(context.CreateRequest( | 1173 scoped_ptr<URLRequest> request2(context.CreateRequest( |
1050 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 1174 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
1051 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 1175 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); |
1052 request2->Start(); | 1176 request2->Start(); |
1053 base::RunLoop().Run(); | 1177 base::RunLoop().Run(); |
1054 | 1178 |
| 1179 float packet_loss_rate = NetworkQualityEstimator::kInvalidPacketLossRate; |
| 1180 EXPECT_FALSE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 1181 |
| 1182 estimator.OnUpdatedPacketCountAvailable( |
| 1183 NetworkQualityEstimator::PROTOCOL_QUIC, 0, 1, 1); |
| 1184 EXPECT_TRUE(estimator.GetPacketLossRateEstimate(&packet_loss_rate)); |
| 1185 EXPECT_NEAR(0.0, packet_loss_rate, 0.001); |
| 1186 |
1055 // Both RTT and downstream throughput should be updated. | 1187 // Both RTT and downstream throughput should be updated. |
1056 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), | 1188 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), |
1057 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 1189 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); |
1058 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, | 1190 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, |
1059 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 1191 estimator.GetDownlinkThroughputKbpsEstimateInternal( |
1060 base::TimeTicks(), 100)); | 1192 base::TimeTicks(), 100)); |
1061 | 1193 |
1062 EXPECT_EQ(2U, rtt_observer.observations().size()); | 1194 EXPECT_EQ(2U, rtt_observer.observations().size()); |
1063 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1195 EXPECT_EQ(2U, throughput_observer.observations().size()); |
| 1196 ASSERT_EQ(1U, packet_loss_observer.observations().size()); |
1064 for (auto observation : rtt_observer.observations()) { | 1197 for (auto observation : rtt_observer.observations()) { |
1065 EXPECT_LE(0, observation.rtt_ms); | 1198 EXPECT_LE(0, observation.rtt_ms); |
1066 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1199 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
1067 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1200 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
1068 } | 1201 } |
1069 for (auto observation : throughput_observer.observations()) { | 1202 for (auto observation : throughput_observer.observations()) { |
1070 EXPECT_LE(0, observation.throughput_kbps); | 1203 EXPECT_LE(0, observation.throughput_kbps); |
1071 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); | 1204 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
1072 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); | 1205 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); |
1073 } | 1206 } |
| 1207 for (auto observation : packet_loss_observer.observations()) { |
| 1208 EXPECT_LE(0u, observation.num_packets_lost); |
| 1209 EXPECT_LE(1u, observation.num_packets_received_in_order); |
| 1210 EXPECT_LE(0u, observation.num_packets_received_not_in_order); |
| 1211 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); |
| 1212 EXPECT_EQ(NetworkQualityEstimator::QUIC, observation.source); |
| 1213 } |
1074 | 1214 |
1075 // Verify that observations from TCP and QUIC are passed on to the observers. | 1215 // Verify that observations from TCP and QUIC are passed on to the observers. |
1076 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); | 1216 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); |
1077 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); | 1217 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); |
1078 | 1218 |
1079 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = | 1219 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = |
1080 estimator.CreateSocketPerformanceWatcher( | 1220 estimator.CreateSocketPerformanceWatcher( |
1081 SocketPerformanceWatcherFactory::PROTOCOL_TCP); | 1221 SocketPerformanceWatcherFactory::PROTOCOL_TCP); |
1082 scoped_ptr<SocketPerformanceWatcher> quic_watcher = | 1222 scoped_ptr<SocketPerformanceWatcher> quic_watcher = |
1083 estimator.CreateSocketPerformanceWatcher( | 1223 estimator.CreateSocketPerformanceWatcher( |
1084 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); | 1224 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); |
1085 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); | 1225 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); |
1086 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); | 1226 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); |
1087 | 1227 |
1088 EXPECT_EQ(4U, rtt_observer.observations().size()); | 1228 EXPECT_EQ(4U, rtt_observer.observations().size()); |
1089 EXPECT_EQ(2U, throughput_observer.observations().size()); | 1229 EXPECT_EQ(2U, throughput_observer.observations().size()); |
1090 | 1230 |
1091 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); | 1231 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); |
1092 EXPECT_EQ(quic_rtt.InMilliseconds(), | 1232 EXPECT_EQ(quic_rtt.InMilliseconds(), |
1093 rtt_observer.observations().at(3).rtt_ms); | 1233 rtt_observer.observations().at(3).rtt_ms); |
1094 } | 1234 } |
1095 | 1235 |
1096 } // namespace net | 1236 } // namespace net |
OLD | NEW |