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
3 // found in the LICENSE file.
5 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time/time.h"
14 #include "net/base/network_change_notifier.h"
18 struct NetworkQuality
;
20 // NetworkQualityEstimator provides network quality estimates (quality of the
21 // full paths to all origins that have been connected to).
22 // The estimates are based on the observed organic traffic.
23 // A NetworkQualityEstimator instance is attached to URLRequestContexts and
24 // observes the traffic of URLRequests spawned from the URLRequestContexts.
25 // A single instance of NQE can be attached to multiple URLRequestContexts,
26 // thereby increasing the single NQE instance's accuracy by providing more
27 // observed traffic characteristics.
28 class NET_EXPORT_PRIVATE NetworkQualityEstimator
29 : public NetworkChangeNotifier::ConnectionTypeObserver
{
31 // Creates a new NetworkQualityEstimator.
32 NetworkQualityEstimator();
34 ~NetworkQualityEstimator() override
;
36 // Returns an estimate of the current network quality.
37 // Virtualized for testing.
38 virtual NetworkQuality
GetEstimate() const;
40 // Notifies NetworkQualityEstimator that a response has been received.
41 // |prefilter_bytes_read| is the count of the bytes received prior to
42 // applying filters (e.g. decompression, SDCH) from request creation time
44 void NotifyDataReceived(const URLRequest
& request
,
45 int64_t prefilter_bytes_read
);
48 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest
,
49 TestPeakKbpsFastestRTTUpdates
);
50 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP
, NetworkQualityEstimator
);
52 // Tiny transfer sizes may give inaccurate throughput results.
53 // Minimum size of the transfer over which the throughput is computed.
54 static const int kMinTransferSizeInBytes
= 10000;
56 // Minimum duration (in microseconds) of the transfer over which the
57 // throughput is computed.
58 static const int kMinRequestDurationMicroseconds
= 1000;
60 // Construct a NetworkQualityEstimator instance allowing for test
62 // Registers for network type change notifications so estimates can be kept
64 // |allow_local_host_requests_for_tests| should only be true when testing
65 // against local HTTP server and allows the requests to local host to be
66 // used for network quality estimation.
67 explicit NetworkQualityEstimator(bool allow_local_host_requests_for_tests
);
69 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
70 void OnConnectionTypeChanged(
71 NetworkChangeNotifier::ConnectionType type
) override
;
73 // Determines if the requests to local host can be used in estimating the
74 // network quality. Set to true only for tests.
75 const bool allow_localhost_requests_
;
77 // Time when last connection change was observed.
78 base::TimeTicks last_connection_change_
;
80 // Last value passed to |OnConnectionTypeChanged|. This indicates the
81 // current connection type.
82 NetworkChangeNotifier::ConnectionType current_connection_type_
;
84 // Set if any network data has been received since last connectivity change.
85 bool bytes_read_since_last_connection_change_
;
87 // Fastest round-trip-time (RTT) since last connectivity change. RTT measured
88 // from URLRequest creation until first byte received.
89 base::TimeDelta fastest_RTT_since_last_connection_change_
;
91 // Rough measurement of downlink peak Kbps witnessed since last connectivity
92 // change. The accuracy is decreased by ignoring these factors:
93 // 1) Multiple URLRequests can occur concurrently.
94 // 2) The transfer time includes at least one RTT while no bytes are read.
95 uint64_t peak_kbps_since_last_connection_change_
;
97 base::ThreadChecker thread_checker_
;
99 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator
);
104 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_