| 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/nqe/network_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 return accuracy_recording_intervals_; | 395 return accuracy_recording_intervals_; |
| 396 } | 396 } |
| 397 | 397 |
| 398 void NetworkQualityEstimator::NotifyStartTransaction( | 398 void NetworkQualityEstimator::NotifyStartTransaction( |
| 399 const URLRequest& request) { | 399 const URLRequest& request) { |
| 400 DCHECK(thread_checker_.CalledOnValidThread()); | 400 DCHECK(thread_checker_.CalledOnValidThread()); |
| 401 | 401 |
| 402 if (!RequestSchemeIsHTTPOrHTTPS(request)) | 402 if (!RequestSchemeIsHTTPOrHTTPS(request)) |
| 403 return; | 403 return; |
| 404 | 404 |
| 405 throughput_analyzer_->NotifyStartTransaction(request); |
| 406 } |
| 407 |
| 408 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { |
| 409 TRACE_EVENT0(kNetTracingCategory, |
| 410 "NetworkQualityEstimator::NotifyHeadersReceived"); |
| 411 DCHECK(thread_checker_.CalledOnValidThread()); |
| 412 |
| 413 if (!RequestSchemeIsHTTPOrHTTPS(request) || |
| 414 !RequestProvidesRTTObservation(request)) { |
| 415 return; |
| 416 } |
| 417 |
| 418 const base::TimeTicks now = tick_clock_->NowTicks(); |
| 419 |
| 405 // Update |estimated_quality_at_last_main_frame_| if this is a main frame | 420 // Update |estimated_quality_at_last_main_frame_| if this is a main frame |
| 406 // request. | 421 // request. |
| 407 // TODO(tbansal): Refactor this to a separate method. | 422 // TODO(tbansal): Refactor this to a separate method. |
| 408 if (request.load_flags() & LOAD_MAIN_FRAME_DEPRECATED) { | 423 if (request.load_flags() & LOAD_MAIN_FRAME_DEPRECATED) { |
| 409 base::TimeTicks now = tick_clock_->NowTicks(); | |
| 410 last_main_frame_request_ = now; | 424 last_main_frame_request_ = now; |
| 411 | 425 |
| 412 ComputeEffectiveConnectionType(); | 426 ComputeEffectiveConnectionType(); |
| 413 effective_connection_type_at_last_main_frame_ = effective_connection_type_; | 427 effective_connection_type_at_last_main_frame_ = effective_connection_type_; |
| 414 estimated_quality_at_last_main_frame_ = network_quality_; | 428 estimated_quality_at_last_main_frame_ = network_quality_; |
| 415 | 429 |
| 416 RecordMetricsOnMainFrameRequest(); | 430 RecordMetricsOnMainFrameRequest(); |
| 417 MaybeQueryExternalEstimateProvider(); | 431 MaybeQueryExternalEstimateProvider(); |
| 418 | 432 |
| 419 // Post the tasks which will run in the future and record the estimation | 433 // Post the tasks which will run in the future and record the estimation |
| 420 // accuracy based on the observations received between now and the time of | 434 // accuracy based on the observations received between now and the time of |
| 421 // task execution. Posting the task at different intervals makes it | 435 // task execution. Posting the task at different intervals makes it |
| 422 // possible to measure the accuracy by comparing the estimate with the | 436 // possible to measure the accuracy by comparing the estimate with the |
| 423 // observations received over intervals of varying durations. | 437 // observations received over intervals of varying durations. |
| 424 for (const base::TimeDelta& measuring_delay : | 438 for (const base::TimeDelta& measuring_delay : |
| 425 GetAccuracyRecordingIntervals()) { | 439 GetAccuracyRecordingIntervals()) { |
| 426 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 440 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 427 FROM_HERE, | 441 FROM_HERE, |
| 428 base::Bind(&NetworkQualityEstimator::RecordAccuracyAfterMainFrame, | 442 base::Bind(&NetworkQualityEstimator::RecordAccuracyAfterMainFrame, |
| 429 weak_ptr_factory_.GetWeakPtr(), measuring_delay), | 443 weak_ptr_factory_.GetWeakPtr(), measuring_delay), |
| 430 measuring_delay); | 444 measuring_delay); |
| 431 } | 445 } |
| 432 } | 446 } |
| 433 throughput_analyzer_->NotifyStartTransaction(request); | |
| 434 } | |
| 435 | |
| 436 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { | |
| 437 TRACE_EVENT0(kNetTracingCategory, | |
| 438 "NetworkQualityEstimator::NotifyHeadersReceived"); | |
| 439 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 440 | |
| 441 if (!RequestSchemeIsHTTPOrHTTPS(request) || | |
| 442 !RequestProvidesRTTObservation(request)) { | |
| 443 return; | |
| 444 } | |
| 445 | |
| 446 // Update |estimated_quality_at_last_main_frame_| if this is a main frame | |
| 447 // request. | |
| 448 // TODO(tbansal): Refactor this to a separate method. | |
| 449 if (request.load_flags() & LOAD_MAIN_FRAME_DEPRECATED) { | |
| 450 ComputeEffectiveConnectionType(); | |
| 451 } | |
| 452 | 447 |
| 453 LoadTimingInfo load_timing_info; | 448 LoadTimingInfo load_timing_info; |
| 454 request.GetLoadTimingInfo(&load_timing_info); | 449 request.GetLoadTimingInfo(&load_timing_info); |
| 455 | 450 |
| 456 // If the load timing info is unavailable, it probably means that the request | 451 // If the load timing info is unavailable, it probably means that the request |
| 457 // did not go over the network. | 452 // did not go over the network. |
| 458 if (load_timing_info.send_start.is_null() || | 453 if (load_timing_info.send_start.is_null() || |
| 459 load_timing_info.receive_headers_end.is_null()) { | 454 load_timing_info.receive_headers_end.is_null()) { |
| 460 return; | 455 return; |
| 461 } | 456 } |
| 462 DCHECK(!request.response_info().was_cached); | 457 DCHECK(!request.response_info().was_cached); |
| 463 | 458 |
| 464 // Duration between when the resource was requested and when the response | 459 // Duration between when the resource was requested and when the response |
| 465 // headers were received. | 460 // headers were received. |
| 466 base::TimeDelta observed_http_rtt = | 461 base::TimeDelta observed_http_rtt = |
| 467 load_timing_info.receive_headers_end - load_timing_info.send_start; | 462 load_timing_info.receive_headers_end - load_timing_info.send_start; |
| 468 DCHECK_GE(observed_http_rtt, base::TimeDelta()); | 463 DCHECK_GE(observed_http_rtt, base::TimeDelta()); |
| 469 if (observed_http_rtt < peak_network_quality_.http_rtt() || | 464 if (observed_http_rtt < peak_network_quality_.http_rtt() || |
| 470 peak_network_quality_.http_rtt() == nqe::internal::InvalidRTT()) { | 465 peak_network_quality_.http_rtt() == nqe::internal::InvalidRTT()) { |
| 471 peak_network_quality_ = nqe::internal::NetworkQuality( | 466 peak_network_quality_ = nqe::internal::NetworkQuality( |
| 472 observed_http_rtt, peak_network_quality_.transport_rtt(), | 467 observed_http_rtt, peak_network_quality_.transport_rtt(), |
| 473 peak_network_quality_.downstream_throughput_kbps()); | 468 peak_network_quality_.downstream_throughput_kbps()); |
| 474 } | 469 } |
| 475 | 470 |
| 476 RttObservation http_rtt_observation( | 471 RttObservation http_rtt_observation(observed_http_rtt, now, |
| 477 observed_http_rtt, tick_clock_->NowTicks(), signal_strength_dbm_, | 472 signal_strength_dbm_, |
| 478 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP); | 473 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP); |
| 479 rtt_observations_.AddObservation(http_rtt_observation); | 474 rtt_observations_.AddObservation(http_rtt_observation); |
| 480 NotifyObserversOfRTT(http_rtt_observation); | 475 NotifyObserversOfRTT(http_rtt_observation); |
| 481 } | 476 } |
| 482 | 477 |
| 483 void NetworkQualityEstimator::RecordAccuracyAfterMainFrame( | 478 void NetworkQualityEstimator::RecordAccuracyAfterMainFrame( |
| 484 base::TimeDelta measuring_duration) const { | 479 base::TimeDelta measuring_duration) const { |
| 485 DCHECK(thread_checker_.CalledOnValidThread()); | 480 DCHECK(thread_checker_.CalledOnValidThread()); |
| 486 DCHECK_EQ(0, measuring_duration.InMilliseconds() % 1000); | 481 DCHECK_EQ(0, measuring_duration.InMilliseconds() % 1000); |
| 487 DCHECK(ContainsValue(GetAccuracyRecordingIntervals(), measuring_duration)); | 482 DCHECK(ContainsValue(GetAccuracyRecordingIntervals(), measuring_duration)); |
| 488 | 483 |
| (...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1752 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE); | 1747 NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP_CACHED_ESTIMATE); |
| 1753 downstream_throughput_kbps_observations_.AddObservation( | 1748 downstream_throughput_kbps_observations_.AddObservation( |
| 1754 throughput_observation); | 1749 throughput_observation); |
| 1755 NotifyObserversOfThroughput(throughput_observation); | 1750 NotifyObserversOfThroughput(throughput_observation); |
| 1756 } | 1751 } |
| 1757 | 1752 |
| 1758 ComputeEffectiveConnectionType(); | 1753 ComputeEffectiveConnectionType(); |
| 1759 } | 1754 } |
| 1760 | 1755 |
| 1761 } // namespace net | 1756 } // namespace net |
| OLD | NEW |