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 | #include "net/nqe/throughput_analyzer.h" |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 9 | #include <map> |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 10 | #include <memory> |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 11 | #include <string> |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 12 | #include <utility> |
| 13 | #include <vector> |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 14 | |
| 15 | #include "base/bind.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 16 | #include "base/callback_helpers.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 17 | #include "base/containers/circular_deque.h" |
Wez | a3ec3fe | 2020-02-17 12:52:16 | [diff] [blame] | 18 | #include "base/location.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 19 | #include "base/run_loop.h" |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 21 | #include "base/task/single_thread_task_runner.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 22 | #include "base/test/metrics/histogram_tester.h" |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 23 | #include "base/test/scoped_feature_list.h" |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 24 | #include "base/test/simple_test_tick_clock.h" |
Wez | 92e1e133 | 2019-09-19 21:57:56 | [diff] [blame] | 25 | #include "base/test/test_timeouts.h" |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 26 | #include "base/threading/platform_thread.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 27 | #include "base/threading/thread_task_runner_handle.h" |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 28 | #include "base/time/default_tick_clock.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 29 | #include "base/time/time.h" |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 30 | #include "build/build_config.h" |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 31 | #include "net/base/features.h" |
Matt Menke | 3ab9c7b | 2020-04-01 22:39:57 | [diff] [blame] | 32 | #include "net/base/isolation_info.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 33 | #include "net/base/schemeful_site.h" |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 34 | #include "net/dns/mock_host_resolver.h" |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 35 | #include "net/nqe/network_quality_estimator.h" |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 36 | #include "net/nqe/network_quality_estimator_params.h" |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 37 | #include "net/nqe/network_quality_estimator_test_util.h" |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 38 | #include "net/nqe/network_quality_estimator_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 39 | #include "net/test/test_with_task_environment.h" |
rhalavati | 9ebaba7e | 2017-04-27 06:16:29 | [diff] [blame] | 40 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 41 | #include "net/url_request/url_request.h" |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 42 | #include "net/url_request/url_request_context.h" |
| 43 | #include "net/url_request/url_request_context_builder.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 44 | #include "net/url_request/url_request_test_util.h" |
| 45 | #include "testing/gtest/include/gtest/gtest.h" |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 46 | #include "url/gurl.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 47 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 48 | namespace net::nqe { |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 49 | |
| 50 | namespace { |
| 51 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 52 | // Creates a mock resolver mapping example.com to a public IP address. |
| 53 | std::unique_ptr<HostResolver> CreateMockHostResolver() { |
| 54 | auto host_resolver = std::make_unique<MockCachingHostResolver>( |
| 55 | /*cache_invalidation_num=*/0, |
| 56 | /*default_result=*/ERR_NAME_NOT_RESOLVED); |
| 57 | |
| 58 | // local.com resolves to a private IP address. |
| 59 | host_resolver->rules()->AddRule("local.com", "127.0.0.1"); |
| 60 | host_resolver->LoadIntoCache(HostPortPair("local.com", 80), |
| 61 | NetworkIsolationKey(), absl::nullopt); |
| 62 | // Hosts not listed here (e.g., "example.com") are treated as external. See |
| 63 | // ThroughputAnalyzerTest.PrivateHost below. |
| 64 | |
| 65 | return host_resolver; |
| 66 | } |
| 67 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 68 | class TestThroughputAnalyzer : public internal::ThroughputAnalyzer { |
| 69 | public: |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 70 | TestThroughputAnalyzer(NetworkQualityEstimator* network_quality_estimator, |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 71 | NetworkQualityEstimatorParams* params, |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 72 | const base::TickClock* tick_clock) |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 73 | : internal::ThroughputAnalyzer( |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 74 | network_quality_estimator, |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 75 | params, |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 76 | base::ThreadTaskRunnerHandle::Get(), |
Anna Malova | 2592bd4a | 2020-03-03 15:52:16 | [diff] [blame] | 77 | base::BindRepeating( |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 78 | &TestThroughputAnalyzer::OnNewThroughputObservationAvailable, |
| 79 | base::Unretained(this)), |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 80 | tick_clock, |
Tsuyoshi Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame] | 81 | NetLogWithSource::Make(NetLogSourceType::NONE)) {} |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 82 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 83 | TestThroughputAnalyzer(const TestThroughputAnalyzer&) = delete; |
| 84 | TestThroughputAnalyzer& operator=(const TestThroughputAnalyzer&) = delete; |
| 85 | |
Chris Watkins | f3c65a6 | 2017-12-01 02:06:41 | [diff] [blame] | 86 | ~TestThroughputAnalyzer() override = default; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 87 | |
| 88 | int32_t throughput_observations_received() const { |
| 89 | return throughput_observations_received_; |
| 90 | } |
| 91 | |
| 92 | void OnNewThroughputObservationAvailable(int32_t downstream_kbps) { |
| 93 | throughput_observations_received_++; |
| 94 | } |
| 95 | |
| 96 | int64_t GetBitsReceived() const override { return bits_received_; } |
| 97 | |
| 98 | void IncrementBitsReceived(int64_t additional_bits_received) { |
| 99 | bits_received_ += additional_bits_received; |
| 100 | } |
| 101 | |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 102 | using internal::ThroughputAnalyzer::CountActiveInFlightRequests; |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 103 | using internal::ThroughputAnalyzer:: |
| 104 | disable_throughput_measurements_for_testing; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 105 | using internal::ThroughputAnalyzer::EraseHangingRequests; |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 106 | using internal::ThroughputAnalyzer::IsHangingWindow; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 107 | |
| 108 | private: |
Tsuyoshi Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame] | 109 | int throughput_observations_received_ = 0; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 110 | |
Tsuyoshi Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame] | 111 | int64_t bits_received_ = 0; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 112 | }; |
| 113 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 114 | using ThroughputAnalyzerTest = TestWithTaskEnvironment; |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 115 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 116 | TEST_F(ThroughputAnalyzerTest, PrivateHost) { |
| 117 | auto host_resolver = CreateMockHostResolver(); |
| 118 | EXPECT_FALSE(nqe::internal::IsPrivateHostForTesting( |
| 119 | host_resolver.get(), HostPortPair("example.com", 80), |
| 120 | NetworkIsolationKey())); |
| 121 | EXPECT_TRUE(nqe::internal::IsPrivateHostForTesting( |
| 122 | host_resolver.get(), HostPortPair("local.com", 80), |
| 123 | NetworkIsolationKey())); |
| 124 | } |
| 125 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 126 | #if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID) |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 127 | // Flaky on iOS: crbug.com/672917. |
Egor Pasko | 0198f25 | 2021-09-09 15:09:17 | [diff] [blame] | 128 | // Flaky on Android: crbug.com/1223950. |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 129 | #define MAYBE_MaximumRequests DISABLED_MaximumRequests |
| 130 | #else |
| 131 | #define MAYBE_MaximumRequests MaximumRequests |
| 132 | #endif |
| 133 | TEST_F(ThroughputAnalyzerTest, MAYBE_MaximumRequests) { |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 134 | const struct TestCase { |
| 135 | GURL url; |
| 136 | bool is_local; |
| 137 | } kTestCases[] = { |
| 138 | {GURL("http://127.0.0.1/test.html"), true /* is_local */}, |
| 139 | {GURL("http://example.com/test.html"), false /* is_local */}, |
| 140 | {GURL("http://local.com/test.html"), true /* is_local */}, |
| 141 | }; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 142 | |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 143 | for (const auto& test_case : kTestCases) { |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 144 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 145 | TestNetworkQualityEstimator network_quality_estimator; |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 146 | std::map<std::string, std::string> variation_params; |
tbansal | 4a4305a | 2017-06-08 05:03:19 | [diff] [blame] | 147 | NetworkQualityEstimatorParams params(variation_params); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 148 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 149 | ¶ms, tick_clock); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 150 | |
| 151 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 152 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 153 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 154 | auto context = context_builder->Build(); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 155 | |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 156 | ASSERT_FALSE( |
| 157 | throughput_analyzer.disable_throughput_measurements_for_testing()); |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 158 | base::circular_deque<std::unique_ptr<URLRequest>> requests; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 159 | |
| 160 | // Start more requests than the maximum number of requests that can be held |
| 161 | // in the memory. |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 162 | EXPECT_EQ(test_case.is_local, |
| 163 | nqe::internal::IsPrivateHostForTesting( |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 164 | context->host_resolver(), |
| 165 | HostPortPair::FromURL(test_case.url), NetworkIsolationKey())); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 166 | for (size_t i = 0; i < 1000; ++i) { |
| 167 | std::unique_ptr<URLRequest> request( |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 168 | context->CreateRequest(test_case.url, DEFAULT_PRIORITY, |
| 169 | &test_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 170 | throughput_analyzer.NotifyStartTransaction(*(request.get())); |
| 171 | requests.push_back(std::move(request)); |
| 172 | } |
| 173 | // Too many local requests should cause the |throughput_analyzer| to disable |
| 174 | // throughput measurements. |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 175 | EXPECT_NE(test_case.is_local, |
| 176 | throughput_analyzer.IsCurrentlyTrackingThroughput()); |
| 177 | } |
| 178 | } |
| 179 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 180 | #if BUILDFLAG(IS_IOS) |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 181 | // Flaky on iOS: crbug.com/672917. |
| 182 | #define MAYBE_MaximumRequestsWithNetworkIsolationKey \ |
| 183 | DISABLED_MaximumRequestsWithNetworkIsolationKey |
| 184 | #else |
| 185 | #define MAYBE_MaximumRequestsWithNetworkIsolationKey \ |
| 186 | MaximumRequestsWithNetworkIsolationKey |
| 187 | #endif |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 188 | // Make sure that the NetworkIsolationKey is respected when resolving a host |
| 189 | // from the cache. |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 190 | TEST_F(ThroughputAnalyzerTest, MAYBE_MaximumRequestsWithNetworkIsolationKey) { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 191 | const SchemefulSite kSite(GURL("https://foo.test/")); |
| 192 | const net::NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 193 | const GURL kUrl = GURL("http://foo.test/test.html"); |
| 194 | |
| 195 | base::test::ScopedFeatureList feature_list; |
| 196 | feature_list.InitAndEnableFeature( |
| 197 | features::kSplitHostCacheByNetworkIsolationKey); |
| 198 | |
| 199 | for (bool use_network_isolation_key : {false, true}) { |
| 200 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
| 201 | TestNetworkQualityEstimator network_quality_estimator; |
| 202 | std::map<std::string, std::string> variation_params; |
| 203 | NetworkQualityEstimatorParams params(variation_params); |
| 204 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 205 | ¶ms, tick_clock); |
| 206 | |
| 207 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 208 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 209 | auto mock_host_resolver = std::make_unique<MockCachingHostResolver>(); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 210 | |
| 211 | // Add an entry to the host cache mapping kUrl to non-local IP when using an |
| 212 | // empty NetworkIsolationKey. |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 213 | mock_host_resolver->rules()->AddRule(kUrl.host(), "1.2.3.4"); |
| 214 | mock_host_resolver->LoadIntoCache(HostPortPair::FromURL(kUrl), |
| 215 | NetworkIsolationKey(), absl::nullopt); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 216 | |
| 217 | // Add an entry to the host cache mapping kUrl to local IP when using |
| 218 | // kNetworkIsolationKey. |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 219 | mock_host_resolver->rules()->ClearRules(); |
| 220 | mock_host_resolver->rules()->AddRule(kUrl.host(), "127.0.0.1"); |
| 221 | mock_host_resolver->LoadIntoCache(HostPortPair::FromURL(kUrl), |
| 222 | kNetworkIsolationKey, absl::nullopt); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 223 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 224 | context_builder->set_host_resolver(std::move(mock_host_resolver)); |
| 225 | auto context = context_builder->Build(); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 226 | ASSERT_FALSE( |
| 227 | throughput_analyzer.disable_throughput_measurements_for_testing()); |
| 228 | base::circular_deque<std::unique_ptr<URLRequest>> requests; |
| 229 | |
| 230 | // Start more requests than the maximum number of requests that can be held |
| 231 | // in the memory. |
| 232 | EXPECT_EQ(use_network_isolation_key, |
| 233 | nqe::internal::IsPrivateHostForTesting( |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 234 | context->host_resolver(), HostPortPair::FromURL(kUrl), |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 235 | use_network_isolation_key ? kNetworkIsolationKey |
| 236 | : NetworkIsolationKey())); |
| 237 | for (size_t i = 0; i < 1000; ++i) { |
| 238 | std::unique_ptr<URLRequest> request( |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 239 | context->CreateRequest(kUrl, DEFAULT_PRIORITY, &test_delegate, |
| 240 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 241 | if (use_network_isolation_key) |
Matt Menke | 3ab9c7b | 2020-04-01 22:39:57 | [diff] [blame] | 242 | request->set_isolation_info(IsolationInfo::CreatePartial( |
shivanigithub | 4e78015f59 | 2020-10-21 13:26:23 | [diff] [blame] | 243 | IsolationInfo::RequestType::kOther, kNetworkIsolationKey)); |
Matt Menke | a7e69712 | 2019-11-06 00:47:57 | [diff] [blame] | 244 | throughput_analyzer.NotifyStartTransaction(*(request.get())); |
| 245 | requests.push_back(std::move(request)); |
| 246 | } |
| 247 | // Too many local requests should cause the |throughput_analyzer| to disable |
| 248 | // throughput measurements. |
| 249 | EXPECT_NE(use_network_isolation_key, |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 250 | throughput_analyzer.IsCurrentlyTrackingThroughput()); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 254 | // Tests that the throughput observation is taken only if there are sufficient |
| 255 | // number of requests in-flight. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 256 | TEST_F(ThroughputAnalyzerTest, TestMinRequestsForThroughputSample) { |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 257 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 258 | TestNetworkQualityEstimator network_quality_estimator; |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 259 | std::map<std::string, std::string> variation_params; |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 260 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "-1"; |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 261 | NetworkQualityEstimatorParams params(variation_params); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 262 | // Set HTTP RTT to a large value so that the throughput observation window |
| 263 | // is not detected as hanging. In practice, this would be provided by |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 264 | // |network_quality_estimator| based on the recent observations. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 265 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(100)); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 266 | |
| 267 | for (size_t num_requests = 1; |
| 268 | num_requests <= params.throughput_min_requests_in_flight() + 1; |
| 269 | ++num_requests) { |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 270 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 271 | ¶ms, tick_clock); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 272 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 273 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 274 | auto context = context_builder->Build(); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 275 | std::vector<std::unique_ptr<URLRequest>> requests_not_local; |
| 276 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 277 | std::vector<TestDelegate> not_local_test_delegates(num_requests); |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 278 | for (auto& delegate : not_local_test_delegates) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 279 | // We don't care about completion, except for the first one (see below). |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 280 | delegate.set_on_complete(base::DoNothing()); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 281 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 282 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &delegate, |
| 283 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 284 | request_not_local->Start(); |
| 285 | requests_not_local.push_back(std::move(request_not_local)); |
| 286 | } |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 287 | not_local_test_delegates[0].RunUntilComplete(); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 288 | |
| 289 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 290 | |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 291 | for (const auto& request : requests_not_local) { |
| 292 | throughput_analyzer.NotifyStartTransaction(*request); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Increment the bytes received count to emulate the bytes received for |
| 296 | // |request_local| and |requests_not_local|. |
| 297 | throughput_analyzer.IncrementBitsReceived(100 * 1000 * 8); |
| 298 | |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 299 | for (const auto& request : requests_not_local) { |
| 300 | throughput_analyzer.NotifyRequestCompleted(*request); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 301 | } |
| 302 | base::RunLoop().RunUntilIdle(); |
| 303 | |
| 304 | int expected_throughput_observations = |
| 305 | num_requests >= params.throughput_min_requests_in_flight() ? 1 : 0; |
| 306 | EXPECT_EQ(expected_throughput_observations, |
| 307 | throughput_analyzer.throughput_observations_received()); |
| 308 | } |
| 309 | } |
| 310 | |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 311 | // Tests that the hanging requests are dropped from the |requests_|, and |
| 312 | // throughput observation window is ended. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 313 | TEST_F(ThroughputAnalyzerTest, TestHangingRequests) { |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 314 | static const struct { |
| 315 | int hanging_request_duration_http_rtt_multiplier; |
| 316 | base::TimeDelta http_rtt; |
| 317 | base::TimeDelta requests_hang_duration; |
| 318 | bool expect_throughput_observation; |
| 319 | } tests[] = { |
| 320 | { |
| 321 | // |requests_hang_duration| is less than 5 times the HTTP RTT. |
| 322 | // Requests should not be marked as hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 323 | 5, |
| 324 | base::Milliseconds(1000), |
| 325 | base::Milliseconds(3000), |
| 326 | true, |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 327 | }, |
| 328 | { |
| 329 | // |requests_hang_duration| is more than 5 times the HTTP RTT. |
| 330 | // Requests should be marked as hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 331 | 5, |
| 332 | base::Milliseconds(200), |
| 333 | base::Milliseconds(3000), |
| 334 | false, |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 335 | }, |
| 336 | { |
| 337 | // |requests_hang_duration| is less than |
| 338 | // |hanging_request_min_duration_msec|. Requests should not be marked |
| 339 | // as hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 340 | 1, |
| 341 | base::Milliseconds(100), |
| 342 | base::Milliseconds(100), |
| 343 | true, |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 344 | }, |
| 345 | { |
| 346 | // |requests_hang_duration| is more than |
| 347 | // |hanging_request_min_duration_msec|. Requests should be marked as |
| 348 | // hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 349 | 1, |
| 350 | base::Milliseconds(2000), |
| 351 | base::Milliseconds(3100), |
| 352 | false, |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 353 | }, |
| 354 | { |
| 355 | // |requests_hang_duration| is less than 5 times the HTTP RTT. |
| 356 | // Requests should not be marked as hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 357 | 5, |
| 358 | base::Seconds(2), |
| 359 | base::Seconds(1), |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 360 | true, |
| 361 | }, |
| 362 | { |
| 363 | // HTTP RTT is unavailable. Requests should not be marked as hanging. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 364 | 5, |
| 365 | base::Seconds(-1), |
| 366 | base::Seconds(-1), |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 367 | true, |
| 368 | }, |
| 369 | }; |
| 370 | |
| 371 | for (const auto& test : tests) { |
| 372 | base::HistogramTester histogram_tester; |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 373 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 374 | TestNetworkQualityEstimator network_quality_estimator; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 375 | if (test.http_rtt >= base::TimeDelta()) |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 376 | network_quality_estimator.SetStartTimeNullHttpRtt(test.http_rtt); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 377 | std::map<std::string, std::string> variation_params; |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 378 | // Set the transport RTT multiplier to a large value so that the hanging |
| 379 | // request decision is made only on the basis of the HTTP RTT. |
| 380 | variation_params |
| 381 | ["hanging_request_http_rtt_upper_bound_transport_rtt_multiplier"] = |
| 382 | "10000"; |
| 383 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "-1"; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 384 | variation_params["hanging_request_duration_http_rtt_multiplier"] = |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 385 | base::NumberToString(test.hanging_request_duration_http_rtt_multiplier); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 386 | |
| 387 | NetworkQualityEstimatorParams params(variation_params); |
| 388 | |
| 389 | const size_t num_requests = params.throughput_min_requests_in_flight(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 390 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 391 | ¶ms, tick_clock); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 392 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 393 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 394 | auto context = context_builder->Build(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 395 | std::vector<std::unique_ptr<URLRequest>> requests_not_local; |
| 396 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 397 | std::vector<TestDelegate> not_local_test_delegates(num_requests); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 398 | for (size_t i = 0; i < num_requests; ++i) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 399 | // We don't care about completion, except for the first one (see below). |
| 400 | not_local_test_delegates[i].set_on_complete(base::DoNothing()); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 401 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 402 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 403 | ¬_local_test_delegates[i], TRAFFIC_ANNOTATION_FOR_TESTS)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 404 | request_not_local->Start(); |
| 405 | requests_not_local.push_back(std::move(request_not_local)); |
| 406 | } |
| 407 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 408 | not_local_test_delegates[0].RunUntilComplete(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 409 | |
| 410 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 411 | |
| 412 | for (size_t i = 0; i < num_requests; ++i) { |
| 413 | throughput_analyzer.NotifyStartTransaction(*requests_not_local.at(i)); |
| 414 | } |
| 415 | |
| 416 | // Increment the bytes received count to emulate the bytes received for |
| 417 | // |request_local| and |requests_not_local|. |
| 418 | throughput_analyzer.IncrementBitsReceived(100 * 1000 * 8); |
| 419 | |
| 420 | // Mark in-flight requests as hanging requests (if specified in the test |
| 421 | // params). |
| 422 | if (test.requests_hang_duration >= base::TimeDelta()) |
| 423 | base::PlatformThread::Sleep(test.requests_hang_duration); |
| 424 | |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 425 | EXPECT_EQ(num_requests, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 426 | |
| 427 | for (size_t i = 0; i < num_requests; ++i) { |
| 428 | throughput_analyzer.NotifyRequestCompleted(*requests_not_local.at(i)); |
| 429 | if (!test.expect_throughput_observation) { |
| 430 | // All in-flight requests should be marked as hanging, and thus should |
| 431 | // be deleted from the set of in-flight requests. |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 432 | EXPECT_EQ(0u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 433 | } else { |
| 434 | // One request should be deleted at one time. |
| 435 | EXPECT_EQ(requests_not_local.size() - i - 1, |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 436 | throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | base::RunLoop().RunUntilIdle(); |
| 441 | |
| 442 | EXPECT_EQ(test.expect_throughput_observation, |
| 443 | throughput_analyzer.throughput_observations_received() > 0); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | // Tests that the check for hanging requests is done at most once per second. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 448 | TEST_F(ThroughputAnalyzerTest, TestHangingRequestsCheckedOnlyPeriodically) { |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 449 | base::SimpleTestTickClock tick_clock; |
| 450 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 451 | TestNetworkQualityEstimator network_quality_estimator; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 452 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(1)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 453 | std::map<std::string, std::string> variation_params; |
| 454 | variation_params["hanging_request_duration_http_rtt_multiplier"] = "5"; |
| 455 | variation_params["hanging_request_min_duration_msec"] = "2000"; |
| 456 | NetworkQualityEstimatorParams params(variation_params); |
| 457 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 458 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 459 | ¶ms, &tick_clock); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 460 | |
| 461 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 462 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 463 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 464 | auto context = context_builder->Build(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 465 | std::vector<std::unique_ptr<URLRequest>> requests_not_local; |
| 466 | |
| 467 | for (size_t i = 0; i < 2; ++i) { |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 468 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 469 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 470 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 471 | request_not_local->Start(); |
| 472 | requests_not_local.push_back(std::move(request_not_local)); |
| 473 | } |
| 474 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 475 | std::unique_ptr<URLRequest> some_other_request(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 476 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 477 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 478 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 479 | test_delegate.RunUntilComplete(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 480 | |
| 481 | // First request starts at t=1. The second request starts at t=2. The first |
| 482 | // request would be marked as hanging at t=6, and the second request at t=7 |
| 483 | // seconds. |
| 484 | for (size_t i = 0; i < 2; ++i) { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 485 | tick_clock.Advance(base::Milliseconds(1000)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 486 | throughput_analyzer.NotifyStartTransaction(*requests_not_local.at(i)); |
| 487 | } |
| 488 | |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 489 | EXPECT_EQ(2u, throughput_analyzer.CountActiveInFlightRequests()); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 490 | tick_clock.Advance(base::Milliseconds(3500)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 491 | // Current time is t = 5.5 seconds. |
| 492 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 493 | EXPECT_EQ(2u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 494 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 495 | tick_clock.Advance(base::Milliseconds(1000)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 496 | // Current time is t = 6.5 seconds. One request should be marked as hanging. |
| 497 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 498 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 499 | |
| 500 | // Current time is t = 6.5 seconds. Calling NotifyBytesRead again should not |
| 501 | // run the hanging request checker since the last check was at t=6.5 seconds. |
| 502 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 503 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 504 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 505 | tick_clock.Advance(base::Milliseconds(600)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 506 | // Current time is t = 7.1 seconds. Calling NotifyBytesRead again should not |
| 507 | // run the hanging request checker since the last check was at t=6.5 seconds |
| 508 | // (less than 1 second ago). |
| 509 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 510 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 511 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 512 | tick_clock.Advance(base::Milliseconds(400)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 513 | // Current time is t = 7.5 seconds. Calling NotifyBytesRead again should run |
| 514 | // the hanging request checker since the last check was at t=6.5 seconds (at |
| 515 | // least 1 second ago). |
| 516 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 517 | EXPECT_EQ(0u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // Tests that the last received time for a request is updated when data is |
| 521 | // received for that request. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 522 | TEST_F(ThroughputAnalyzerTest, TestLastReceivedTimeIsUpdated) { |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 523 | base::SimpleTestTickClock tick_clock; |
| 524 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 525 | TestNetworkQualityEstimator network_quality_estimator; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 526 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(1)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 527 | std::map<std::string, std::string> variation_params; |
| 528 | variation_params["hanging_request_duration_http_rtt_multiplier"] = "5"; |
| 529 | variation_params["hanging_request_min_duration_msec"] = "2000"; |
| 530 | NetworkQualityEstimatorParams params(variation_params); |
| 531 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 532 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 533 | ¶ms, &tick_clock); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 534 | |
| 535 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 536 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 537 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 538 | auto context = context_builder->Build(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 539 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 540 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 541 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 542 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 543 | request_not_local->Start(); |
| 544 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 545 | test_delegate.RunUntilComplete(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 546 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 547 | std::unique_ptr<URLRequest> some_other_request(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 548 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 549 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 550 | |
| 551 | // Start time for the request is t=0 second. The request will be marked as |
| 552 | // hanging at t=5 seconds. |
| 553 | throughput_analyzer.NotifyStartTransaction(*request_not_local); |
| 554 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 555 | tick_clock.Advance(base::Milliseconds(4000)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 556 | // Current time is t=4.0 seconds. |
| 557 | |
| 558 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 559 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 560 | |
| 561 | // The request will be marked as hanging at t=9 seconds. |
| 562 | throughput_analyzer.NotifyBytesRead(*request_not_local); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 563 | tick_clock.Advance(base::Milliseconds(4000)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 564 | // Current time is t=8 seconds. |
| 565 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 566 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 567 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 568 | tick_clock.Advance(base::Milliseconds(2000)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 569 | // Current time is t=10 seconds. |
| 570 | throughput_analyzer.EraseHangingRequests(*some_other_request); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 571 | EXPECT_EQ(0u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // Test that a request that has been hanging for a long time is deleted |
| 575 | // immediately when EraseHangingRequests is called even if the last hanging |
| 576 | // request check was done recently. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 577 | TEST_F(ThroughputAnalyzerTest, TestRequestDeletedImmediately) { |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 578 | base::SimpleTestTickClock tick_clock; |
| 579 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 580 | TestNetworkQualityEstimator network_quality_estimator; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 581 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(1)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 582 | std::map<std::string, std::string> variation_params; |
| 583 | variation_params["hanging_request_duration_http_rtt_multiplier"] = "2"; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 584 | NetworkQualityEstimatorParams params(variation_params); |
| 585 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 586 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 587 | ¶ms, &tick_clock); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 588 | |
| 589 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 590 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 591 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 592 | auto context = context_builder->Build(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 593 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 594 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 595 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 596 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 597 | request_not_local->Start(); |
| 598 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 599 | test_delegate.RunUntilComplete(); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 600 | |
| 601 | // Start time for the request is t=0 second. The request will be marked as |
| 602 | // hanging at t=2 seconds. |
| 603 | throughput_analyzer.NotifyStartTransaction(*request_not_local); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 604 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 605 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 606 | tick_clock.Advance(base::Milliseconds(2900)); |
Tarun Bansal | 5b7386a6 | 2018-09-24 21:54:30 | [diff] [blame] | 607 | // Current time is t=2.9 seconds. |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 608 | |
| 609 | throughput_analyzer.EraseHangingRequests(*request_not_local); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 610 | EXPECT_EQ(1u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 611 | |
| 612 | // |request_not_local| should be deleted since it has been idle for 2.4 |
| 613 | // seconds. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 614 | tick_clock.Advance(base::Milliseconds(500)); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 615 | throughput_analyzer.NotifyBytesRead(*request_not_local); |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 616 | EXPECT_EQ(0u, throughput_analyzer.CountActiveInFlightRequests()); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 617 | } |
| 618 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 619 | #if BUILDFLAG(IS_IOS) |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 620 | // Flaky on iOS: crbug.com/672917. |
| 621 | #define MAYBE_TestThroughputWithMultipleRequestsOverlap \ |
| 622 | DISABLED_TestThroughputWithMultipleRequestsOverlap |
| 623 | #else |
| 624 | #define MAYBE_TestThroughputWithMultipleRequestsOverlap \ |
| 625 | TestThroughputWithMultipleRequestsOverlap |
| 626 | #endif |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 627 | // Tests if the throughput observation is taken correctly when local and network |
| 628 | // requests overlap. |
Justin Cohen | 8bfb6b7 | 2020-07-27 18:22:25 | [diff] [blame] | 629 | TEST_F(ThroughputAnalyzerTest, |
| 630 | MAYBE_TestThroughputWithMultipleRequestsOverlap) { |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 631 | static const struct { |
| 632 | bool start_local_request; |
| 633 | bool local_request_completes_first; |
| 634 | bool expect_throughput_observation; |
| 635 | } tests[] = { |
| 636 | { |
| 637 | false, false, true, |
| 638 | }, |
| 639 | { |
| 640 | true, false, false, |
| 641 | }, |
| 642 | { |
| 643 | true, true, true, |
| 644 | }, |
| 645 | }; |
| 646 | |
| 647 | for (const auto& test : tests) { |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 648 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 649 | TestNetworkQualityEstimator network_quality_estimator; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 650 | // Localhost requests are not allowed for estimation purposes. |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 651 | std::map<std::string, std::string> variation_params; |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 652 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "-1"; |
tbansal | 4a4305a | 2017-06-08 05:03:19 | [diff] [blame] | 653 | NetworkQualityEstimatorParams params(variation_params); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 654 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 655 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 656 | ¶ms, tick_clock); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 657 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 658 | TestDelegate local_delegate; |
| 659 | local_delegate.set_on_complete(base::DoNothing()); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 660 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 661 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 662 | auto context = context_builder->Build(); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 663 | std::unique_ptr<URLRequest> request_local; |
| 664 | |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 665 | std::vector<std::unique_ptr<URLRequest>> requests_not_local; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 666 | std::vector<TestDelegate> not_local_test_delegates( |
| 667 | params.throughput_min_requests_in_flight()); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 668 | for (size_t i = 0; i < params.throughput_min_requests_in_flight(); ++i) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 669 | // We don't care about completion, except for the first one (see below). |
| 670 | not_local_test_delegates[i].set_on_complete(base::DoNothing()); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 671 | std::unique_ptr<URLRequest> request_not_local(context->CreateRequest( |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 672 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 673 | ¬_local_test_delegates[i], TRAFFIC_ANNOTATION_FOR_TESTS)); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 674 | request_not_local->Start(); |
| 675 | requests_not_local.push_back(std::move(request_not_local)); |
| 676 | } |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 677 | |
| 678 | if (test.start_local_request) { |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 679 | request_local = context->CreateRequest(GURL("http://127.0.0.1/echo.html"), |
| 680 | DEFAULT_PRIORITY, &local_delegate, |
| 681 | TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 682 | request_local->Start(); |
| 683 | } |
| 684 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 685 | // Wait until the first not-local request completes. |
| 686 | not_local_test_delegates[0].RunUntilComplete(); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 687 | |
| 688 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 689 | |
| 690 | // If |test.start_local_request| is true, then |request_local| starts |
| 691 | // before |request_not_local|, and ends after |request_not_local|. Thus, |
| 692 | // network quality estimator should not get a chance to record throughput |
| 693 | // observation from |request_not_local| because of ongoing local request |
| 694 | // at all times. |
| 695 | if (test.start_local_request) |
| 696 | throughput_analyzer.NotifyStartTransaction(*request_local); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 697 | |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 698 | for (const auto& request : requests_not_local) { |
| 699 | throughput_analyzer.NotifyStartTransaction(*request); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 700 | } |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 701 | |
| 702 | if (test.local_request_completes_first) { |
| 703 | ASSERT_TRUE(test.start_local_request); |
| 704 | throughput_analyzer.NotifyRequestCompleted(*request_local); |
| 705 | } |
| 706 | |
| 707 | // Increment the bytes received count to emulate the bytes received for |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 708 | // |request_local| and |requests_not_local|. |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 709 | throughput_analyzer.IncrementBitsReceived(100 * 1000 * 8); |
| 710 | |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame^] | 711 | for (const auto& request : requests_not_local) { |
| 712 | throughput_analyzer.NotifyRequestCompleted(*request); |
Tarun Bansal | fc13d598 | 2017-09-12 01:58:25 | [diff] [blame] | 713 | } |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 714 | if (test.start_local_request && !test.local_request_completes_first) |
| 715 | throughput_analyzer.NotifyRequestCompleted(*request_local); |
| 716 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 717 | // Pump the message loop to let analyzer tasks get processed. |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 718 | base::RunLoop().RunUntilIdle(); |
| 719 | |
| 720 | int expected_throughput_observations = |
| 721 | test.expect_throughput_observation ? 1 : 0; |
| 722 | EXPECT_EQ(expected_throughput_observations, |
| 723 | throughput_analyzer.throughput_observations_received()); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // Tests if the throughput observation is taken correctly when two network |
| 728 | // requests overlap. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 729 | TEST_F(ThroughputAnalyzerTest, TestThroughputWithNetworkRequestsOverlap) { |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 730 | static const struct { |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 731 | size_t throughput_min_requests_in_flight; |
| 732 | size_t number_requests_in_flight; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 733 | int64_t increment_bits; |
| 734 | bool expect_throughput_observation; |
| 735 | } tests[] = { |
| 736 | { |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 737 | 1, 2, 100 * 1000 * 8, true, |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 738 | }, |
| 739 | { |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 740 | 3, 1, 100 * 1000 * 8, false, |
| 741 | }, |
| 742 | { |
| 743 | 3, 2, 100 * 1000 * 8, false, |
| 744 | }, |
| 745 | { |
| 746 | 3, 3, 100 * 1000 * 8, true, |
| 747 | }, |
| 748 | { |
| 749 | 3, 4, 100 * 1000 * 8, true, |
| 750 | }, |
| 751 | { |
| 752 | 1, 2, 1, false, |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 753 | }, |
| 754 | }; |
| 755 | |
| 756 | for (const auto& test : tests) { |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 757 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 758 | TestNetworkQualityEstimator network_quality_estimator; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 759 | // Localhost requests are not allowed for estimation purposes. |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 760 | std::map<std::string, std::string> variation_params; |
| 761 | variation_params["throughput_min_requests_in_flight"] = |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 762 | base::NumberToString(test.throughput_min_requests_in_flight); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 763 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "-1"; |
tbansal | 4a4305a | 2017-06-08 05:03:19 | [diff] [blame] | 764 | NetworkQualityEstimatorParams params(variation_params); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 765 | // Set HTTP RTT to a large value so that the throughput observation window |
| 766 | // is not detected as hanging. In practice, this would be provided by |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 767 | // |network_quality_estimator| based on the recent observations. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 768 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(100)); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 769 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 770 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 771 | ¶ms, tick_clock); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 772 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 773 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 774 | auto context = context_builder->Build(); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 775 | |
| 776 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 777 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 778 | std::vector<std::unique_ptr<URLRequest>> requests_in_flight; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 779 | std::vector<TestDelegate> in_flight_test_delegates( |
| 780 | test.number_requests_in_flight); |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 781 | for (size_t i = 0; i < test.number_requests_in_flight; ++i) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 782 | // We don't care about completion, except for the first one (see below). |
| 783 | in_flight_test_delegates[i].set_on_complete(base::DoNothing()); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 784 | std::unique_ptr<URLRequest> request_network_1 = context->CreateRequest( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 785 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 786 | &in_flight_test_delegates[i], TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 787 | requests_in_flight.push_back(std::move(request_network_1)); |
| 788 | requests_in_flight.back()->Start(); |
| 789 | } |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 790 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 791 | in_flight_test_delegates[0].RunUntilComplete(); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 792 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 793 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 794 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 795 | for (size_t i = 0; i < test.number_requests_in_flight; ++i) { |
| 796 | URLRequest* request = requests_in_flight.at(i).get(); |
| 797 | throughput_analyzer.NotifyStartTransaction(*request); |
| 798 | } |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 799 | |
| 800 | // Increment the bytes received count to emulate the bytes received for |
| 801 | // |request_network_1| and |request_network_2|. |
| 802 | throughput_analyzer.IncrementBitsReceived(test.increment_bits); |
| 803 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 804 | for (size_t i = 0; i < test.number_requests_in_flight; ++i) { |
| 805 | URLRequest* request = requests_in_flight.at(i).get(); |
| 806 | throughput_analyzer.NotifyRequestCompleted(*request); |
| 807 | } |
| 808 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 809 | base::RunLoop().RunUntilIdle(); |
| 810 | |
| 811 | // Only one observation should be taken since two requests overlap. |
| 812 | if (test.expect_throughput_observation) { |
| 813 | EXPECT_EQ(1, throughput_analyzer.throughput_observations_received()); |
| 814 | } else { |
| 815 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 820 | // Tests if the throughput observation is taken correctly when the start and end |
| 821 | // of network requests overlap, and the minimum number of in flight requests |
| 822 | // when taking an observation is more than 1. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 823 | TEST_F(ThroughputAnalyzerTest, TestThroughputWithMultipleNetworkRequests) { |
Wez | 9d5dd28 | 2020-02-10 17:21:22 | [diff] [blame] | 824 | const base::test::ScopedRunLoopTimeout increased_run_timeout( |
Wez | a3ec3fe | 2020-02-17 12:52:16 | [diff] [blame] | 825 | FROM_HERE, TestTimeouts::action_max_timeout()); |
Wez | 92e1e133 | 2019-09-19 21:57:56 | [diff] [blame] | 826 | |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 827 | const base::TickClock* tick_clock = base::DefaultTickClock::GetInstance(); |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 828 | TestNetworkQualityEstimator network_quality_estimator; |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 829 | std::map<std::string, std::string> variation_params; |
| 830 | variation_params["throughput_min_requests_in_flight"] = "3"; |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 831 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "-1"; |
tbansal | 4a4305a | 2017-06-08 05:03:19 | [diff] [blame] | 832 | NetworkQualityEstimatorParams params(variation_params); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 833 | // Set HTTP RTT to a large value so that the throughput observation window |
| 834 | // is not detected as hanging. In practice, this would be provided by |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 835 | // |network_quality_estimator| based on the recent observations. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 836 | network_quality_estimator.SetStartTimeNullHttpRtt(base::Seconds(100)); |
Tarun Bansal | f67b3e7 | 2018-03-02 06:10:33 | [diff] [blame] | 837 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 838 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 839 | ¶ms, tick_clock); |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 840 | TestDelegate test_delegate; |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 841 | auto context_builder = CreateTestURLRequestContextBuilder(); |
| 842 | context_builder->set_host_resolver(CreateMockHostResolver()); |
| 843 | auto context = context_builder->Build(); |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 844 | |
| 845 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 846 | |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 847 | std::unique_ptr<URLRequest> request_1 = context->CreateRequest( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 848 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 849 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 850 | std::unique_ptr<URLRequest> request_2 = context->CreateRequest( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 851 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 852 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 853 | std::unique_ptr<URLRequest> request_3 = context->CreateRequest( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 854 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 855 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Yutaka Hirano | 36ab60c7 | 2022-03-17 08:30:37 | [diff] [blame] | 856 | std::unique_ptr<URLRequest> request_4 = context->CreateRequest( |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 857 | GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 858 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 859 | |
| 860 | request_1->Start(); |
| 861 | request_2->Start(); |
| 862 | request_3->Start(); |
| 863 | request_4->Start(); |
| 864 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 865 | // We dispatched four requests, so wait for four completions. |
| 866 | for (int i = 0; i < 4; ++i) |
| 867 | test_delegate.RunUntilComplete(); |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 868 | |
| 869 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 870 | |
| 871 | throughput_analyzer.NotifyStartTransaction(*(request_1.get())); |
| 872 | throughput_analyzer.NotifyStartTransaction(*(request_2.get())); |
| 873 | |
| 874 | const size_t increment_bits = 100 * 1000 * 8; |
| 875 | |
| 876 | // Increment the bytes received count to emulate the bytes received for |
| 877 | // |request_1| and |request_2|. |
| 878 | throughput_analyzer.IncrementBitsReceived(increment_bits); |
| 879 | |
| 880 | throughput_analyzer.NotifyRequestCompleted(*(request_1.get())); |
| 881 | base::RunLoop().RunUntilIdle(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 882 | |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 883 | // No observation should be taken since only 1 request is in flight. |
| 884 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 885 | |
| 886 | throughput_analyzer.NotifyStartTransaction(*(request_3.get())); |
| 887 | throughput_analyzer.NotifyStartTransaction(*(request_4.get())); |
| 888 | EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 889 | |
| 890 | // 3 requests are in flight which is at least as many as the minimum number of |
| 891 | // in flight requests required. An observation should be taken. |
| 892 | throughput_analyzer.IncrementBitsReceived(increment_bits); |
| 893 | |
| 894 | // Only one observation should be taken since two requests overlap. |
| 895 | throughput_analyzer.NotifyRequestCompleted(*(request_2.get())); |
| 896 | base::RunLoop().RunUntilIdle(); |
| 897 | |
| 898 | EXPECT_EQ(1, throughput_analyzer.throughput_observations_received()); |
| 899 | throughput_analyzer.NotifyRequestCompleted(*(request_3.get())); |
| 900 | throughput_analyzer.NotifyRequestCompleted(*(request_4.get())); |
| 901 | EXPECT_EQ(1, throughput_analyzer.throughput_observations_received()); |
| 902 | } |
| 903 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 904 | TEST_F(ThroughputAnalyzerTest, TestHangingWindow) { |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 905 | static constexpr size_t kCwndSizeKilobytes = 10 * 1.5; |
| 906 | static constexpr size_t kCwndSizeBits = kCwndSizeKilobytes * 1000 * 8; |
| 907 | |
| 908 | base::SimpleTestTickClock tick_clock; |
| 909 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 910 | TestNetworkQualityEstimator network_quality_estimator; |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 911 | int64_t http_rtt_msec = 1000; |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 912 | network_quality_estimator.SetStartTimeNullHttpRtt( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 913 | base::Milliseconds(http_rtt_msec)); |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 914 | std::map<std::string, std::string> variation_params; |
| 915 | variation_params["throughput_hanging_requests_cwnd_size_multiplier"] = "1"; |
| 916 | NetworkQualityEstimatorParams params(variation_params); |
| 917 | |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 918 | TestThroughputAnalyzer throughput_analyzer(&network_quality_estimator, |
| 919 | ¶ms, &tick_clock); |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 920 | |
| 921 | const struct { |
| 922 | size_t bits_received; |
| 923 | base::TimeDelta window_duration; |
| 924 | bool expected_hanging; |
| 925 | } tests[] = { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 926 | {100, base::Milliseconds(http_rtt_msec), true}, |
| 927 | {kCwndSizeBits - 1, base::Milliseconds(http_rtt_msec), true}, |
| 928 | {kCwndSizeBits + 1, base::Milliseconds(http_rtt_msec), false}, |
| 929 | {2 * (kCwndSizeBits - 1), base::Milliseconds(http_rtt_msec * 2), true}, |
| 930 | {2 * (kCwndSizeBits + 1), base::Milliseconds(http_rtt_msec * 2), false}, |
| 931 | {kCwndSizeBits / 2 - 1, base::Milliseconds(http_rtt_msec / 2), true}, |
| 932 | {kCwndSizeBits / 2 + 1, base::Milliseconds(http_rtt_msec / 2), false}, |
Tarun Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 933 | }; |
| 934 | |
| 935 | for (const auto& test : tests) { |
| 936 | base::HistogramTester histogram_tester; |
| 937 | double kbps = test.bits_received / test.window_duration.InMillisecondsF(); |
| 938 | EXPECT_EQ(test.expected_hanging, |
| 939 | throughput_analyzer.IsHangingWindow(test.bits_received, |
| 940 | test.window_duration, kbps)); |
| 941 | |
| 942 | if (test.expected_hanging) { |
| 943 | histogram_tester.ExpectUniqueSample("NQE.ThroughputObservation.Hanging", |
| 944 | kbps, 1); |
| 945 | histogram_tester.ExpectTotalCount("NQE.ThroughputObservation.NotHanging", |
| 946 | 0); |
| 947 | } else { |
| 948 | histogram_tester.ExpectTotalCount("NQE.ThroughputObservation.Hanging", 0); |
| 949 | histogram_tester.ExpectUniqueSample( |
| 950 | "NQE.ThroughputObservation.NotHanging", kbps, 1); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 955 | } // namespace |
| 956 | |
Tsuyoshi Horo | 4f516be | 2022-06-14 11:53:13 | [diff] [blame] | 957 | } // namespace net::nqe |