| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/throughput_analyzer.h" | 5 #include "net/nqe/throughput_analyzer.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "net/base/host_port_pair.h" |
| 11 #include "net/base/network_activity_monitor.h" | 12 #include "net/base/network_activity_monitor.h" |
| 12 #include "net/base/url_util.h" | 13 #include "net/base/url_util.h" |
| 13 #include "net/nqe/network_quality_estimator_params.h" | 14 #include "net/nqe/network_quality_estimator_params.h" |
| 15 #include "net/nqe/network_quality_estimator_util.h" |
| 14 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" |
| 15 | 18 |
| 16 #if defined(OS_ANDROID) | 19 #if defined(OS_ANDROID) |
| 17 #include "net/android/traffic_stats.h" | 20 #include "net/android/traffic_stats.h" |
| 18 #endif // OS_ANDROID | 21 #endif // OS_ANDROID |
| 19 | 22 |
| 20 namespace net { | 23 namespace net { |
| 21 | 24 |
| 25 class HostResolver; |
| 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 // Maximum number of accuracy degrading requests, and requests that do not | 29 // Maximum number of accuracy degrading requests, and requests that do not |
| 25 // degrade accuracy held in the memory. | 30 // degrade accuracy held in the memory. |
| 26 static const size_t kMaxRequestsSize = 300; | 31 static const size_t kMaxRequestsSize = 300; |
| 27 | 32 |
| 28 // Tiny transfer sizes may give inaccurate throughput results. | 33 // Tiny transfer sizes may give inaccurate throughput results. |
| 29 // Minimum size of the transfer over which the throughput is computed. | 34 // Minimum size of the transfer over which the throughput is computed. |
| 30 static const int kMinTransferSizeInBits = 32 * 8 * 1000; | 35 static const int kMinTransferSizeInBits = 32 * 8 * 1000; |
| 31 | 36 |
| 32 } // namespace | 37 } // namespace |
| 33 | 38 |
| 34 namespace nqe { | 39 namespace nqe { |
| 35 | 40 |
| 36 namespace internal { | 41 namespace internal { |
| 37 | 42 |
| 38 ThroughputAnalyzer::ThroughputAnalyzer( | 43 ThroughputAnalyzer::ThroughputAnalyzer( |
| 39 const NetworkQualityEstimatorParams* params, | 44 const NetworkQualityEstimatorParams* params, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 41 ThroughputObservationCallback throughput_observation_callback, | 46 ThroughputObservationCallback throughput_observation_callback, |
| 42 bool use_local_host_requests_for_tests, | 47 bool use_local_host_requests_for_tests, |
| 43 bool use_smaller_responses_for_tests) | 48 bool use_smaller_responses_for_tests, |
| 49 const NetLogWithSource& net_log) |
| 44 : params_(params), | 50 : params_(params), |
| 45 task_runner_(task_runner), | 51 task_runner_(task_runner), |
| 46 throughput_observation_callback_(throughput_observation_callback), | 52 throughput_observation_callback_(throughput_observation_callback), |
| 47 last_connection_change_(base::TimeTicks::Now()), | 53 last_connection_change_(base::TimeTicks::Now()), |
| 48 window_start_time_(base::TimeTicks()), | 54 window_start_time_(base::TimeTicks()), |
| 49 bits_received_at_window_start_(0), | 55 bits_received_at_window_start_(0), |
| 50 disable_throughput_measurements_(false), | 56 disable_throughput_measurements_(false), |
| 51 use_localhost_requests_for_tests_(use_local_host_requests_for_tests), | 57 use_localhost_requests_for_tests_(use_local_host_requests_for_tests), |
| 52 use_small_responses_for_tests_(use_smaller_responses_for_tests) { | 58 use_small_responses_for_tests_(use_smaller_responses_for_tests), |
| 59 net_log_(net_log) { |
| 53 DCHECK(params_); | 60 DCHECK(params_); |
| 54 DCHECK(task_runner_); | 61 DCHECK(task_runner_); |
| 55 DCHECK(!IsCurrentlyTrackingThroughput()); | 62 DCHECK(!IsCurrentlyTrackingThroughput()); |
| 56 } | 63 } |
| 57 | 64 |
| 58 ThroughputAnalyzer::~ThroughputAnalyzer() { | 65 ThroughputAnalyzer::~ThroughputAnalyzer() { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 } | 67 } |
| 61 | 68 |
| 62 void ThroughputAnalyzer::MaybeStartThroughputObservationWindow() { | 69 void ThroughputAnalyzer::MaybeStartThroughputObservationWindow() { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 int64_t rx_bytes; | 264 int64_t rx_bytes; |
| 258 if (android::traffic_stats::GetCurrentUidRxBytes(&rx_bytes)) | 265 if (android::traffic_stats::GetCurrentUidRxBytes(&rx_bytes)) |
| 259 return static_cast<uint64_t>(rx_bytes * 8); | 266 return static_cast<uint64_t>(rx_bytes * 8); |
| 260 #endif | 267 #endif |
| 261 return NetworkActivityMonitor::GetInstance()->GetBytesReceived() * 8; | 268 return NetworkActivityMonitor::GetInstance()->GetBytesReceived() * 8; |
| 262 } | 269 } |
| 263 | 270 |
| 264 bool ThroughputAnalyzer::DegradesAccuracy(const URLRequest& request) const { | 271 bool ThroughputAnalyzer::DegradesAccuracy(const URLRequest& request) const { |
| 265 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK(thread_checker_.CalledOnValidThread()); |
| 266 | 273 |
| 267 return !(use_localhost_requests_for_tests_ || | 274 bool private_network_request = nqe::internal::IsPrivateHost( |
| 268 !IsLocalhost(request.url().host())) || | 275 request.context() ? request.context()->host_resolver() : nullptr, |
| 276 HostPortPair(request.url().host(), request.url().EffectiveIntPort()), |
| 277 net_log_); |
| 278 |
| 279 return !(use_localhost_requests_for_tests_ || !private_network_request) || |
| 269 request.creation_time() < last_connection_change_; | 280 request.creation_time() < last_connection_change_; |
| 270 } | 281 } |
| 271 | 282 |
| 272 void ThroughputAnalyzer::BoundRequestsSize() { | 283 void ThroughputAnalyzer::BoundRequestsSize() { |
| 273 if (accuracy_degrading_requests_.size() > kMaxRequestsSize) { | 284 if (accuracy_degrading_requests_.size() > kMaxRequestsSize) { |
| 274 // Clear |accuracy_degrading_requests_| since its size has exceeded its | 285 // Clear |accuracy_degrading_requests_| since its size has exceeded its |
| 275 // capacity. | 286 // capacity. |
| 276 accuracy_degrading_requests_.clear(); | 287 accuracy_degrading_requests_.clear(); |
| 277 // Disable throughput measurements since |this| has lost track of the | 288 // Disable throughput measurements since |this| has lost track of the |
| 278 // accuracy degrading requests. | 289 // accuracy degrading requests. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 297 // TODO(tbansal): crbug.com/609174 Add UMA to record how frequently this | 308 // TODO(tbansal): crbug.com/609174 Add UMA to record how frequently this |
| 298 // happens. | 309 // happens. |
| 299 } | 310 } |
| 300 } | 311 } |
| 301 | 312 |
| 302 } // namespace internal | 313 } // namespace internal |
| 303 | 314 |
| 304 } // namespace nqe | 315 } // namespace nqe |
| 305 | 316 |
| 306 } // namespace net | 317 } // namespace net |
| OLD | NEW |