| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "net/base/url_util.h" | 19 #include "net/base/url_util.h" |
| 20 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 20 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 22 #include "net/url_request/url_request_test_util.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 namespace nqe { | 27 namespace nqe { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 ASSERT_FALSE(throughput_analyzer.disable_throughput_measurements()); | 86 ASSERT_FALSE(throughput_analyzer.disable_throughput_measurements()); |
| 86 std::deque<std::unique_ptr<URLRequest>> requests; | 87 std::deque<std::unique_ptr<URLRequest>> requests; |
| 87 | 88 |
| 88 // Start more requests than the maximum number of requests that can be held | 89 // Start more requests than the maximum number of requests that can be held |
| 89 // in the memory. | 90 // in the memory. |
| 90 const std::string url = test.use_local_requests | 91 const std::string url = test.use_local_requests |
| 91 ? "http://127.0.0.1/test.html" | 92 ? "http://127.0.0.1/test.html" |
| 92 : "http://example.com/test.html"; | 93 : "http://example.com/test.html"; |
| 93 for (size_t i = 0; i < 1000; ++i) { | 94 for (size_t i = 0; i < 1000; ++i) { |
| 94 std::unique_ptr<URLRequest> request( | 95 std::unique_ptr<URLRequest> request( |
| 95 context.CreateRequest(GURL(url), DEFAULT_PRIORITY, &test_delegate)); | 96 context.CreateRequest(GURL(url), DEFAULT_PRIORITY, &test_delegate, |
| 97 TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 96 ASSERT_EQ(test.use_local_requests, IsLocalhost(request->url().host())); | 98 ASSERT_EQ(test.use_local_requests, IsLocalhost(request->url().host())); |
| 97 | 99 |
| 98 throughput_analyzer.NotifyStartTransaction(*(request.get())); | 100 throughput_analyzer.NotifyStartTransaction(*(request.get())); |
| 99 requests.push_back(std::move(request)); | 101 requests.push_back(std::move(request)); |
| 100 } | 102 } |
| 101 // Too many local requests should cause the |throughput_analyzer| to disable | 103 // Too many local requests should cause the |throughput_analyzer| to disable |
| 102 // throughput measurements. | 104 // throughput measurements. |
| 103 EXPECT_EQ(test.use_local_requests, | 105 EXPECT_EQ(test.use_local_requests, |
| 104 throughput_analyzer.disable_throughput_measurements()); | 106 throughput_analyzer.disable_throughput_measurements()); |
| 105 } | 107 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 126 | 128 |
| 127 for (const auto& test : tests) { | 129 for (const auto& test : tests) { |
| 128 // Localhost requests are not allowed for estimation purposes. | 130 // Localhost requests are not allowed for estimation purposes. |
| 129 TestThroughputAnalyzer throughput_analyzer; | 131 TestThroughputAnalyzer throughput_analyzer; |
| 130 | 132 |
| 131 TestDelegate test_delegate; | 133 TestDelegate test_delegate; |
| 132 TestURLRequestContext context; | 134 TestURLRequestContext context; |
| 133 | 135 |
| 134 std::unique_ptr<URLRequest> request_local; | 136 std::unique_ptr<URLRequest> request_local; |
| 135 | 137 |
| 136 std::unique_ptr<URLRequest> request_not_local( | 138 std::unique_ptr<URLRequest> request_not_local(context.CreateRequest( |
| 137 context.CreateRequest(GURL("http://example.com/echo.html"), | 139 GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 138 DEFAULT_PRIORITY, &test_delegate)); | 140 TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 139 request_not_local->Start(); | 141 request_not_local->Start(); |
| 140 | 142 |
| 141 if (test.start_local_request) { | 143 if (test.start_local_request) { |
| 142 request_local = context.CreateRequest(GURL("http://localhost/echo.html"), | 144 request_local = context.CreateRequest(GURL("http://localhost/echo.html"), |
| 143 DEFAULT_PRIORITY, &test_delegate); | 145 DEFAULT_PRIORITY, &test_delegate, |
| 146 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 144 request_local->Start(); | 147 request_local->Start(); |
| 145 } | 148 } |
| 146 | 149 |
| 147 base::RunLoop().Run(); | 150 base::RunLoop().Run(); |
| 148 | 151 |
| 149 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); | 152 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 150 | 153 |
| 151 // If |test.start_local_request| is true, then |request_local| starts | 154 // If |test.start_local_request| is true, then |request_local| starts |
| 152 // before |request_not_local|, and ends after |request_not_local|. Thus, | 155 // before |request_not_local|, and ends after |request_not_local|. Thus, |
| 153 // network quality estimator should not get a chance to record throughput | 156 // network quality estimator should not get a chance to record throughput |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 199 |
| 197 for (const auto& test : tests) { | 200 for (const auto& test : tests) { |
| 198 // Localhost requests are not allowed for estimation purposes. | 201 // Localhost requests are not allowed for estimation purposes. |
| 199 TestThroughputAnalyzer throughput_analyzer; | 202 TestThroughputAnalyzer throughput_analyzer; |
| 200 TestDelegate test_delegate; | 203 TestDelegate test_delegate; |
| 201 TestURLRequestContext context; | 204 TestURLRequestContext context; |
| 202 | 205 |
| 203 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); | 206 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 204 | 207 |
| 205 std::unique_ptr<URLRequest> request_network_1 = context.CreateRequest( | 208 std::unique_ptr<URLRequest> request_network_1 = context.CreateRequest( |
| 206 GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate); | 209 GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 210 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 207 std::unique_ptr<URLRequest> request_network_2 = context.CreateRequest( | 211 std::unique_ptr<URLRequest> request_network_2 = context.CreateRequest( |
| 208 GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate); | 212 GURL("http://example.com/echo.html"), DEFAULT_PRIORITY, &test_delegate, |
| 213 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 209 request_network_1->Start(); | 214 request_network_1->Start(); |
| 210 request_network_2->Start(); | 215 request_network_2->Start(); |
| 211 | 216 |
| 212 base::RunLoop().Run(); | 217 base::RunLoop().Run(); |
| 213 | 218 |
| 214 EXPECT_LE(0, throughput_analyzer.throughput_observations_received()); | 219 EXPECT_LE(0, throughput_analyzer.throughput_observations_received()); |
| 215 | 220 |
| 216 throughput_analyzer.NotifyStartTransaction(*request_network_1); | 221 throughput_analyzer.NotifyStartTransaction(*request_network_1); |
| 217 throughput_analyzer.NotifyStartTransaction(*request_network_2); | 222 throughput_analyzer.NotifyStartTransaction(*request_network_2); |
| 218 | 223 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 231 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); | 236 EXPECT_EQ(0, throughput_analyzer.throughput_observations_received()); |
| 232 } | 237 } |
| 233 } | 238 } |
| 234 } | 239 } |
| 235 | 240 |
| 236 } // namespace | 241 } // namespace |
| 237 | 242 |
| 238 } // namespace nqe | 243 } // namespace nqe |
| 239 | 244 |
| 240 } // namespace net | 245 } // namespace net |
| OLD | NEW |