tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_NQE_THROUGHPUT_ANALYZER_H_ |
| 6 | #define NET_NQE_THROUGHPUT_ANALYZER_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/callback.h" |
| 11 | #include "base/containers/hash_tables.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/threading/thread_checker.h" |
| 15 | #include "base/time/time.h" |
| 16 | #include "net/base/net_export.h" |
| 17 | |
| 18 | namespace { |
| 19 | typedef base::Callback<void(int32_t)> ThroughputObservationCallback; |
| 20 | } |
| 21 | |
| 22 | namespace base { |
| 23 | class SingleThreadTaskRunner; |
| 24 | } |
| 25 | |
| 26 | namespace net { |
| 27 | |
| 28 | class URLRequest; |
| 29 | |
| 30 | namespace nqe { |
| 31 | |
| 32 | namespace internal { |
| 33 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame^] | 34 | class NetworkQualityEstimatorParams; |
| 35 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 36 | // Makes throughput observations. Polls NetworkActivityMonitor |
| 37 | // (TrafficStats on Android) to count number of bits received over throughput |
| 38 | // observation windows in accordance with the following rules: |
| 39 | // (1) A new window of observation begins any time a URL request header is |
| 40 | // about to be sent, or a request completes or is destroyed. |
| 41 | // (2) A request is "active" if its headers are sent, but it hasn't completed, |
| 42 | // and "local" if destined to local host. If at any time during a |
| 43 | // throughput observation window there is an active, local request, the |
| 44 | // window is discarded. |
| 45 | // (3) If less than 32KB is received over the network during a window of |
| 46 | // observation, that window is discarded. |
| 47 | class NET_EXPORT_PRIVATE ThroughputAnalyzer { |
| 48 | public: |
| 49 | // |throughput_observation_callback| is called on the |task_runner| when |
| 50 | // |this| has a new throughput observation. |
| 51 | // |use_local_host_requests_for_tests| should only be true when testing |
| 52 | // against local HTTP server and allows the requests to local host to be |
| 53 | // used for network quality estimation. |use_smaller_responses_for_tests| |
| 54 | // should only be true when testing, and allows the responses smaller than |
| 55 | // |kMinTransferSizeInBits| or shorter than |
| 56 | // |kMinRequestDurationMicroseconds| to be used for network quality |
| 57 | // estimation. |
| 58 | // Virtualized for testing. |
| 59 | ThroughputAnalyzer( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame^] | 60 | const NetworkQualityEstimatorParams* params, |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 61 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 62 | ThroughputObservationCallback throughput_observation_callback, |
| 63 | bool use_local_host_requests_for_tests, |
| 64 | bool use_smaller_responses_for_tests); |
| 65 | virtual ~ThroughputAnalyzer(); |
| 66 | |
| 67 | // Notifies |this| that the headers of |request| are about to be sent. |
| 68 | void NotifyStartTransaction(const URLRequest& request); |
| 69 | |
| 70 | // Notifies |this| that |request| has completed. |
| 71 | void NotifyRequestCompleted(const URLRequest& request); |
| 72 | |
| 73 | // Notifies |this| of a change in connection type. |
| 74 | void OnConnectionTypeChanged(); |
| 75 | |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 76 | // |use_localhost_requests| should only be true when testing against local |
| 77 | // HTTP server and allows the requests to local host to be used for network |
| 78 | // quality estimation. |
| 79 | void SetUseLocalHostRequestsForTesting(bool use_localhost_requests); |
| 80 | |
| 81 | // |use_smaller_responses_for_tests| should only be true when testing, and |
| 82 | // allows the responses smaller than |kMinTransferSizeInBits| or shorter than |
| 83 | // |kMinRequestDurationMicroseconds| to be used for network quality |
| 84 | // estimation. |
| 85 | void SetUseSmallResponsesForTesting(bool use_small_responses); |
| 86 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 87 | protected: |
| 88 | // Exposed for testing. |
| 89 | bool disable_throughput_measurements() const { |
| 90 | return disable_throughput_measurements_; |
| 91 | } |
| 92 | |
| 93 | // Returns the number of bits received by Chromium so far. The count may not |
| 94 | // start from zero, so the caller should only look at difference from a prior |
| 95 | // call. The count is obtained by polling TrafficStats on Android, and |
| 96 | // net::NetworkActivityMonitor on all other platforms. Virtualized for |
| 97 | // testing. |
| 98 | virtual int64_t GetBitsReceived() const; |
| 99 | |
| 100 | private: |
| 101 | friend class TestThroughputAnalyzer; |
| 102 | |
| 103 | typedef base::hash_set<const URLRequest*> Requests; |
| 104 | |
| 105 | // Returns true if downstream throughput can be recorded. In that case, |
| 106 | // |downstream_kbps| is set to the computed downstream throughput (in |
| 107 | // kilobits per second). If a downstream throughput observation is taken, |
| 108 | // then the throughput observation window is reset so as to continue |
| 109 | // tracking throughput. A throughput observation can be taken only if the |
| 110 | // time-window is currently active, and enough bytes have accumulated in |
| 111 | // that window. |downstream_kbps| should not be null. |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame^] | 112 | bool MaybeGetThroughputObservation(int32_t* downstream_kbps); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 113 | |
| 114 | // Starts the throughput observation window that keeps track of network |
| 115 | // bytes if the following conditions are true: |
| 116 | // (i) All active requests are non-local; |
| 117 | // (ii) There is at least one active, non-local request; and, |
| 118 | // (iii) The throughput observation window is not already tracking |
| 119 | // throughput. The window is started by setting the |start_| and |
| 120 | // |bits_received_|. |
| 121 | void MaybeStartThroughputObservationWindow(); |
| 122 | |
| 123 | // EndThroughputObservationWindow ends the throughput observation window. |
| 124 | void EndThroughputObservationWindow(); |
| 125 | |
| 126 | // Returns true if throughput is currently tracked by a throughput |
| 127 | // observation window. |
| 128 | bool IsCurrentlyTrackingThroughput() const; |
| 129 | |
| 130 | // Returns true if the |request| degrades the accuracy of the throughput |
| 131 | // observation window. A local request or a request that spans a connection |
| 132 | // change degrades the accuracy of the throughput computation. |
| 133 | bool DegradesAccuracy(const URLRequest& request) const; |
| 134 | |
| 135 | // Bounds |accuracy_degrading_requests_| and |requests_| to ensure their sizes |
| 136 | // do not exceed their capacities. |
| 137 | void BoundRequestsSize(); |
| 138 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame^] | 139 | const NetworkQualityEstimatorParams* params_; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 140 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 141 | |
| 142 | // Called every time a new throughput observation is available. |
| 143 | ThroughputObservationCallback throughput_observation_callback_; |
| 144 | |
| 145 | // Time when last connection change was observed. |
| 146 | base::TimeTicks last_connection_change_; |
| 147 | |
| 148 | // Start time of the current throughput observation window. Set to null if |
| 149 | // the window is not currently active. |
| 150 | base::TimeTicks window_start_time_; |
| 151 | |
| 152 | // Number of bits received prior to |start_| as reported by |
| 153 | // NetworkActivityMonitor. |
| 154 | int64_t bits_received_at_window_start_; |
| 155 | |
| 156 | // Container that holds active requests that reduce the accuracy of |
| 157 | // throughput computation. These requests are not used in throughput |
| 158 | // computation. |
| 159 | Requests accuracy_degrading_requests_; |
| 160 | |
| 161 | // Container that holds active requests that do not reduce the accuracy of |
| 162 | // throughput computation. These requests are used in throughput computation. |
| 163 | Requests requests_; |
| 164 | |
| 165 | // If true, then |this| throughput analyzer stops tracking the throughput |
| 166 | // observations until Chromium is restarted. This may happen if the throughput |
| 167 | // analyzer has lost track of the requests that degrade throughput computation |
| 168 | // accuracy. |
| 169 | bool disable_throughput_measurements_; |
| 170 | |
| 171 | // Determines if the requests to local host can be used in estimating the |
| 172 | // network quality. Set to true only for tests. |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 173 | bool use_localhost_requests_for_tests_; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 174 | |
| 175 | // Determines if the responses smaller than |kMinTransferSizeInBits| |
| 176 | // or shorter than |kMinTransferSizeInBits| can be used in estimating the |
| 177 | // network quality. Set to true only for tests. |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 178 | bool use_small_responses_for_tests_; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 179 | |
| 180 | base::ThreadChecker thread_checker_; |
| 181 | |
| 182 | DISALLOW_COPY_AND_ASSIGN(ThroughputAnalyzer); |
| 183 | }; |
| 184 | |
| 185 | } // namespace internal |
| 186 | |
| 187 | } // namespace nqe |
| 188 | |
| 189 | } // namespace net |
| 190 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 191 | #endif // NET_NQE_THROUGHPUT_ANALYZER_H_ |