| 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 <float.h> | 7 #include <float.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 variations_value >= kMinimumThroughputVariationParameterKbps) { | 198 variations_value >= kMinimumThroughputVariationParameterKbps) { |
| 199 default_observations_[i] = | 199 default_observations_[i] = |
| 200 NetworkQuality(default_observations_[i].rtt(), variations_value); | 200 NetworkQuality(default_observations_[i].rtt(), variations_value); |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 void NetworkQualityEstimator::AddDefaultEstimates() { | 205 void NetworkQualityEstimator::AddDefaultEstimates() { |
| 206 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 if (default_observations_[current_network_id_.type].rtt() != InvalidRTT()) { | 207 if (default_observations_[current_network_id_.type].rtt() != InvalidRTT()) { |
| 208 rtt_msec_observations_.AddObservation(Observation( | 208 Observation rtt_observation( |
| 209 default_observations_[current_network_id_.type].rtt().InMilliseconds(), | 209 default_observations_[current_network_id_.type].rtt().InMilliseconds(), |
| 210 base::TimeTicks::Now())); | 210 base::TimeTicks::Now(), OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM); |
| 211 rtt_msec_observations_.AddObservation(rtt_observation); |
| 212 NotifyObserversOfRTT(rtt_observation); |
| 211 } | 213 } |
| 212 if (default_observations_[current_network_id_.type] | 214 if (default_observations_[current_network_id_.type] |
| 213 .downstream_throughput_kbps() != kInvalidThroughput) { | 215 .downstream_throughput_kbps() != kInvalidThroughput) { |
| 216 Observation throughput_observation( |
| 217 default_observations_[current_network_id_.type] |
| 218 .downstream_throughput_kbps(), |
| 219 base::TimeTicks::Now(), OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM); |
| 214 downstream_throughput_kbps_observations_.AddObservation( | 220 downstream_throughput_kbps_observations_.AddObservation( |
| 215 Observation(default_observations_[current_network_id_.type] | 221 throughput_observation); |
| 216 .downstream_throughput_kbps(), | 222 NotifyObserversOfThroughput(throughput_observation); |
| 217 base::TimeTicks::Now())); | |
| 218 } | 223 } |
| 219 } | 224 } |
| 220 | 225 |
| 221 NetworkQualityEstimator::~NetworkQualityEstimator() { | 226 NetworkQualityEstimator::~NetworkQualityEstimator() { |
| 222 DCHECK(thread_checker_.CalledOnValidThread()); | 227 DCHECK(thread_checker_.CalledOnValidThread()); |
| 223 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 228 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 224 } | 229 } |
| 225 | 230 |
| 226 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { | 231 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { |
| 227 DCHECK(thread_checker_.CalledOnValidThread()); | 232 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 255 | 260 |
| 256 // Duration between when the resource was requested and when response | 261 // Duration between when the resource was requested and when response |
| 257 // headers were received. | 262 // headers were received. |
| 258 base::TimeDelta observed_rtt = headers_received_time - request_start_time; | 263 base::TimeDelta observed_rtt = headers_received_time - request_start_time; |
| 259 DCHECK_GE(observed_rtt, base::TimeDelta()); | 264 DCHECK_GE(observed_rtt, base::TimeDelta()); |
| 260 if (observed_rtt < peak_network_quality_.rtt()) { | 265 if (observed_rtt < peak_network_quality_.rtt()) { |
| 261 peak_network_quality_ = NetworkQuality( | 266 peak_network_quality_ = NetworkQuality( |
| 262 observed_rtt, peak_network_quality_.downstream_throughput_kbps()); | 267 observed_rtt, peak_network_quality_.downstream_throughput_kbps()); |
| 263 } | 268 } |
| 264 | 269 |
| 265 rtt_msec_observations_.AddObservation( | 270 Observation rtt_observation(observed_rtt.InMilliseconds(), now, |
| 266 Observation(observed_rtt.InMilliseconds(), now)); | 271 OBSERVATION_SOURCE_URL_REQUEST); |
| 272 rtt_msec_observations_.AddObservation(rtt_observation); |
| 273 NotifyObserversOfRTT(rtt_observation); |
| 267 | 274 |
| 268 // Compare the RTT observation with the estimated value and record it. | 275 // Compare the RTT observation with the estimated value and record it. |
| 269 if (estimated_median_network_quality_.rtt() != InvalidRTT()) { | 276 if (estimated_median_network_quality_.rtt() != InvalidRTT()) { |
| 270 RecordRTTUMA(estimated_median_network_quality_.rtt().InMilliseconds(), | 277 RecordRTTUMA(estimated_median_network_quality_.rtt().InMilliseconds(), |
| 271 observed_rtt.InMilliseconds()); | 278 observed_rtt.InMilliseconds()); |
| 272 } | 279 } |
| 273 } | 280 } |
| 274 | 281 |
| 275 void NetworkQualityEstimator::NotifyRequestCompleted( | 282 void NetworkQualityEstimator::NotifyRequestCompleted( |
| 276 const URLRequest& request) { | 283 const URLRequest& request) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 // connection. | 330 // connection. |
| 324 if (downstream_kbps - downstream_kbps_as_integer > 0) | 331 if (downstream_kbps - downstream_kbps_as_integer > 0) |
| 325 downstream_kbps_as_integer++; | 332 downstream_kbps_as_integer++; |
| 326 | 333 |
| 327 DCHECK_GT(downstream_kbps_as_integer, 0.0); | 334 DCHECK_GT(downstream_kbps_as_integer, 0.0); |
| 328 if (downstream_kbps_as_integer > | 335 if (downstream_kbps_as_integer > |
| 329 peak_network_quality_.downstream_throughput_kbps()) | 336 peak_network_quality_.downstream_throughput_kbps()) |
| 330 peak_network_quality_ = | 337 peak_network_quality_ = |
| 331 NetworkQuality(peak_network_quality_.rtt(), downstream_kbps_as_integer); | 338 NetworkQuality(peak_network_quality_.rtt(), downstream_kbps_as_integer); |
| 332 | 339 |
| 340 Observation throughput_observation(downstream_kbps_as_integer, now, |
| 341 OBSERVATION_SOURCE_URL_REQUEST); |
| 333 downstream_throughput_kbps_observations_.AddObservation( | 342 downstream_throughput_kbps_observations_.AddObservation( |
| 334 Observation(downstream_kbps_as_integer, now)); | 343 throughput_observation); |
| 344 NotifyObserversOfThroughput(throughput_observation); |
| 345 } |
| 346 |
| 347 void NetworkQualityEstimator::AddRTTObserver(RTTObserver* rtt_observer) { |
| 348 DCHECK(thread_checker_.CalledOnValidThread()); |
| 349 rtt_observer_list_.AddObserver(rtt_observer); |
| 350 } |
| 351 |
| 352 void NetworkQualityEstimator::RemoveRTTObserver(RTTObserver* rtt_observer) { |
| 353 DCHECK(thread_checker_.CalledOnValidThread()); |
| 354 rtt_observer_list_.RemoveObserver(rtt_observer); |
| 355 } |
| 356 |
| 357 void NetworkQualityEstimator::AddThroughputObserver( |
| 358 ThroughputObserver* throughput_observer) { |
| 359 DCHECK(thread_checker_.CalledOnValidThread()); |
| 360 throughput_observer_list_.AddObserver(throughput_observer); |
| 361 } |
| 362 |
| 363 void NetworkQualityEstimator::RemoveThroughputObserver( |
| 364 ThroughputObserver* throughput_observer) { |
| 365 DCHECK(thread_checker_.CalledOnValidThread()); |
| 366 throughput_observer_list_.RemoveObserver(throughput_observer); |
| 367 } |
| 368 |
| 369 void NetworkQualityEstimator::ConfigureForTests(bool allow_local_host_requests, |
| 370 bool allow_smaller_responses) { |
| 371 allow_localhost_requests_ = allow_local_host_requests, |
| 372 allow_small_responses_ = allow_smaller_responses; |
| 335 } | 373 } |
| 336 | 374 |
| 337 void NetworkQualityEstimator::RecordRTTUMA(int32_t estimated_value_msec, | 375 void NetworkQualityEstimator::RecordRTTUMA(int32_t estimated_value_msec, |
| 338 int32_t actual_value_msec) const { | 376 int32_t actual_value_msec) const { |
| 339 DCHECK(thread_checker_.CalledOnValidThread()); | 377 DCHECK(thread_checker_.CalledOnValidThread()); |
| 340 | 378 |
| 341 // Record the difference between the actual and the estimated value. | 379 // Record the difference between the actual and the estimated value. |
| 342 if (estimated_value_msec >= actual_value_msec) { | 380 if (estimated_value_msec >= actual_value_msec) { |
| 343 base::HistogramBase* difference_rtt = | 381 base::HistogramBase* difference_rtt = |
| 344 GetHistogram("DifferenceRTTEstimatedAndActual.", | 382 GetHistogram("DifferenceRTTEstimatedAndActual.", |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 bool NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( | 580 bool NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( |
| 543 const base::TimeTicks& begin_timestamp, | 581 const base::TimeTicks& begin_timestamp, |
| 544 int32_t* kbps) const { | 582 int32_t* kbps) const { |
| 545 DCHECK(thread_checker_.CalledOnValidThread()); | 583 DCHECK(thread_checker_.CalledOnValidThread()); |
| 546 DCHECK(kbps); | 584 DCHECK(kbps); |
| 547 *kbps = GetDownlinkThroughputKbpsEstimateInternal(begin_timestamp, 50); | 585 *kbps = GetDownlinkThroughputKbpsEstimateInternal(begin_timestamp, 50); |
| 548 return (*kbps != kInvalidThroughput); | 586 return (*kbps != kInvalidThroughput); |
| 549 } | 587 } |
| 550 | 588 |
| 551 NetworkQualityEstimator::Observation::Observation(int32_t value, | 589 NetworkQualityEstimator::Observation::Observation(int32_t value, |
| 552 base::TimeTicks timestamp) | 590 base::TimeTicks timestamp, |
| 553 : value(value), timestamp(timestamp) { | 591 ObservationSource source) |
| 592 : value(value), timestamp(timestamp), source(source) { |
| 554 DCHECK_GE(value, 0); | 593 DCHECK_GE(value, 0); |
| 555 DCHECK(!timestamp.is_null()); | 594 DCHECK(!timestamp.is_null()); |
| 556 } | 595 } |
| 557 | 596 |
| 558 NetworkQualityEstimator::Observation::~Observation() { | 597 NetworkQualityEstimator::Observation::~Observation() { |
| 559 } | 598 } |
| 560 | 599 |
| 561 NetworkQualityEstimator::ObservationBuffer::ObservationBuffer( | 600 NetworkQualityEstimator::ObservationBuffer::ObservationBuffer( |
| 562 double weight_multiplier_per_second) | 601 double weight_multiplier_per_second) |
| 563 : weight_multiplier_per_second_(weight_multiplier_per_second) { | 602 : weight_multiplier_per_second_(weight_multiplier_per_second) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 cached_network_qualities_.find(current_network_id_); | 797 cached_network_qualities_.find(current_network_id_); |
| 759 | 798 |
| 760 if (it == cached_network_qualities_.end()) | 799 if (it == cached_network_qualities_.end()) |
| 761 return false; | 800 return false; |
| 762 | 801 |
| 763 NetworkQuality network_quality(it->second.network_quality()); | 802 NetworkQuality network_quality(it->second.network_quality()); |
| 764 | 803 |
| 765 DCHECK_NE(InvalidRTT(), network_quality.rtt()); | 804 DCHECK_NE(InvalidRTT(), network_quality.rtt()); |
| 766 DCHECK_NE(kInvalidThroughput, network_quality.downstream_throughput_kbps()); | 805 DCHECK_NE(kInvalidThroughput, network_quality.downstream_throughput_kbps()); |
| 767 | 806 |
| 768 downstream_throughput_kbps_observations_.AddObservation(Observation( | 807 Observation througphput_observation( |
| 769 network_quality.downstream_throughput_kbps(), base::TimeTicks::Now())); | 808 network_quality.downstream_throughput_kbps(), base::TimeTicks::Now(), |
| 770 rtt_msec_observations_.AddObservation(Observation( | 809 OBSERVATION_SOURCE_CACHED_ESTIMATE); |
| 771 network_quality.rtt().InMilliseconds(), base::TimeTicks::Now())); | 810 downstream_throughput_kbps_observations_.AddObservation( |
| 811 througphput_observation); |
| 812 NotifyObserversOfThroughput(througphput_observation); |
| 813 |
| 814 Observation rtt_observation(network_quality.rtt().InMilliseconds(), |
| 815 base::TimeTicks::Now(), |
| 816 OBSERVATION_SOURCE_CACHED_ESTIMATE); |
| 817 rtt_msec_observations_.AddObservation(rtt_observation); |
| 818 NotifyObserversOfRTT(rtt_observation); |
| 819 |
| 772 return true; | 820 return true; |
| 773 } | 821 } |
| 774 | 822 |
| 775 void NetworkQualityEstimator::OnUpdatedEstimateAvailable() { | 823 void NetworkQualityEstimator::OnUpdatedEstimateAvailable() { |
| 776 DCHECK(thread_checker_.CalledOnValidThread()); | 824 DCHECK(thread_checker_.CalledOnValidThread()); |
| 777 DCHECK(external_estimates_provider_); | 825 DCHECK(external_estimates_provider_); |
| 778 // TODO(tbansal): Query provider for the recent value. | 826 // TODO(tbansal): Query provider for the recent value. |
| 779 } | 827 } |
| 780 | 828 |
| 781 void NetworkQualityEstimator::CacheNetworkQualityEstimate() { | 829 void NetworkQualityEstimator::CacheNetworkQualityEstimate() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 810 } | 858 } |
| 811 DCHECK_LT(cached_network_qualities_.size(), | 859 DCHECK_LT(cached_network_qualities_.size(), |
| 812 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); | 860 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); |
| 813 | 861 |
| 814 cached_network_qualities_.insert(std::make_pair( | 862 cached_network_qualities_.insert(std::make_pair( |
| 815 current_network_id_, CachedNetworkQuality(network_quality))); | 863 current_network_id_, CachedNetworkQuality(network_quality))); |
| 816 DCHECK_LE(cached_network_qualities_.size(), | 864 DCHECK_LE(cached_network_qualities_.size(), |
| 817 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); | 865 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); |
| 818 } | 866 } |
| 819 | 867 |
| 868 void NetworkQualityEstimator::NotifyObserversOfRTT( |
| 869 const Observation& observation) { |
| 870 FOR_EACH_OBSERVER(RTTObserver, rtt_observer_list_, |
| 871 OnRTTObservation(observation.value, observation.timestamp, |
| 872 observation.source)); |
| 873 } |
| 874 |
| 875 void NetworkQualityEstimator::NotifyObserversOfThroughput( |
| 876 const Observation& observation) { |
| 877 FOR_EACH_OBSERVER( |
| 878 ThroughputObserver, throughput_observer_list_, |
| 879 OnThroughputObservation(observation.value, observation.timestamp, |
| 880 observation.source)); |
| 881 } |
| 882 |
| 820 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( | 883 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( |
| 821 const NetworkQuality& network_quality) | 884 const NetworkQuality& network_quality) |
| 822 : last_update_time_(base::TimeTicks::Now()), | 885 : last_update_time_(base::TimeTicks::Now()), |
| 823 network_quality_(network_quality) { | 886 network_quality_(network_quality) { |
| 824 } | 887 } |
| 825 | 888 |
| 826 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( | 889 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( |
| 827 const CachedNetworkQuality& other) | 890 const CachedNetworkQuality& other) |
| 828 : last_update_time_(other.last_update_time_), | 891 : last_update_time_(other.last_update_time_), |
| 829 network_quality_(other.network_quality_) { | 892 network_quality_(other.network_quality_) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 857 | 920 |
| 858 NetworkQualityEstimator::NetworkQuality& | 921 NetworkQualityEstimator::NetworkQuality& |
| 859 NetworkQualityEstimator::NetworkQuality:: | 922 NetworkQualityEstimator::NetworkQuality:: |
| 860 operator=(const NetworkQuality& other) { | 923 operator=(const NetworkQuality& other) { |
| 861 rtt_ = other.rtt_; | 924 rtt_ = other.rtt_; |
| 862 downstream_throughput_kbps_ = other.downstream_throughput_kbps_; | 925 downstream_throughput_kbps_ = other.downstream_throughput_kbps_; |
| 863 return *this; | 926 return *this; |
| 864 } | 927 } |
| 865 | 928 |
| 866 } // namespace net | 929 } // namespace net |
| OLD | NEW |