 Chromium Code Reviews
 Chromium Code Reviews Issue 1316863006:
  Populate EEP estimate in NQE  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1316863006:
  Populate EEP estimate in NQE  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" | 
| 6 | 6 | 
| 7 #include <stdint.h> | 7 #include <stdint.h> | 
| 8 | 8 | 
| 9 #include <limits> | 9 #include <limits> | 
| 10 #include <map> | 10 #include <map> | 
| 11 | 11 | 
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" | 
| 13 #include "base/files/file_path.h" | |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" | 
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" | 
| 15 #include "base/metrics/histogram_samples.h" | 16 #include "base/metrics/histogram_samples.h" | 
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" | 
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" | 
| 18 #include "base/test/histogram_tester.h" | 19 #include "base/test/histogram_tester.h" | 
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" | 
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" | 
| 21 #include "net/base/external_estimate_provider.h" | 22 #include "net/base/external_estimate_provider.h" | 
| 22 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" | 
| 23 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" | 
| 25 #include "net/http/http_status_code.h" | |
| 24 #include "net/test/embedded_test_server/embedded_test_server.h" | 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 
| 27 #include "net/test/embedded_test_server/http_request.h" | |
| 28 #include "net/test/embedded_test_server/http_response.h" | |
| 25 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" | 
| 26 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" | 
| 27 #include "url/gurl.h" | 31 #include "url/gurl.h" | 
| 28 | 32 | 
| 29 namespace { | 33 namespace { | 
| 30 | 34 | 
| 31 // Helps in setting the current network type and id. | 35 // Helps in setting the current network type and id. | 
| 32 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | 36 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { | 
| 33 public: | 37 public: | 
| 34 TestNetworkQualityEstimator( | 38 TestNetworkQualityEstimator( | 
| 35 const std::map<std::string, std::string>& variation_params) | 39 const std::map<std::string, std::string>& variation_params, | 
| 36 : NetworkQualityEstimator(scoped_ptr<net::ExternalEstimateProvider>(), | 40 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider) | 
| 41 : NetworkQualityEstimator(external_estimate_provider.Pass(), | |
| 37 variation_params, | 42 variation_params, | 
| 38 true, | 43 true, | 
| 39 true) {} | 44 true) { | 
| 45 // Set up embedded test server. | |
| 46 embedded_test_server_.ServeFilesFromDirectory( | |
| 47 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 48 DCHECK(embedded_test_server_.InitializeAndWaitUntilReady()); | |
| 49 embedded_test_server_.RegisterRequestHandler(base::Bind( | |
| 50 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); | |
| 51 } | |
| 52 | |
| 53 explicit TestNetworkQualityEstimator( | |
| 54 const std::map<std::string, std::string>& variation_params) | |
| 55 : TestNetworkQualityEstimator( | |
| 56 variation_params, | |
| 57 scoped_ptr<net::ExternalEstimateProvider>()) {} | |
| 40 | 58 | 
| 41 ~TestNetworkQualityEstimator() override {} | 59 ~TestNetworkQualityEstimator() override {} | 
| 42 | 60 | 
| 43 // Overrides the current network type and id. | 61 // Overrides the current network type and id. | 
| 44 // Notifies network quality estimator of change in connection. | 62 // Notifies network quality estimator of change in connection. | 
| 45 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 63 void SimulateNetworkChangeTo(net::NetworkChangeNotifier::ConnectionType type, | 
| 46 std::string network_id) { | 64 std::string network_id) { | 
| 47 current_network_type_ = type; | 65 current_network_type_ = type; | 
| 48 current_network_id_ = network_id; | 66 current_network_id_ = network_id; | 
| 49 OnConnectionTypeChanged(type); | 67 OnConnectionTypeChanged(type); | 
| 50 } | 68 } | 
| 51 | 69 | 
| 70 // Called by embedded server when a HTTP request is received. | |
| 71 scoped_ptr<net::test_server::HttpResponse> HandleRequest( | |
| 72 const net::test_server::HttpRequest& request) { | |
| 73 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
| 74 new net::test_server::BasicHttpResponse()); | |
| 75 http_response->set_code(net::HTTP_OK); | |
| 76 http_response->set_content("hello"); | |
| 77 http_response->set_content_type("text/plain"); | |
| 78 return http_response.Pass(); | |
| 79 } | |
| 80 | |
| 81 // Returns a GURL hosted at embedded test server. | |
| 82 const GURL GetEchoURL() const { | |
| 83 return embedded_test_server_.GetURL("/echo.html"); | |
| 84 } | |
| 85 | |
| 52 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 86 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; | 
| 53 using NetworkQualityEstimator::OnConnectionTypeChanged; | 87 using NetworkQualityEstimator::OnConnectionTypeChanged; | 
| 54 | 88 | 
| 55 private: | 89 private: | 
| 56 // NetworkQualityEstimator implementation that returns the overridden network | 90 // NetworkQualityEstimator implementation that returns the overridden network | 
| 57 // id (instead of invoking platform APIs). | 91 // id (instead of invoking platform APIs). | 
| 58 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 92 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { | 
| 59 return NetworkQualityEstimator::NetworkID(current_network_type_, | 93 return NetworkQualityEstimator::NetworkID(current_network_type_, | 
| 60 current_network_id_); | 94 current_network_id_); | 
| 61 } | 95 } | 
| 62 | 96 | 
| 63 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 97 net::NetworkChangeNotifier::ConnectionType current_network_type_; | 
| 64 std::string current_network_id_; | 98 std::string current_network_id_; | 
| 99 | |
| 100 // Embedded server used for testing. | |
| 101 net::test_server::EmbeddedTestServer embedded_test_server_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); | |
| 65 }; | 104 }; | 
| 66 | 105 | 
| 67 } // namespace | 106 } // namespace | 
| 68 | 107 | 
| 69 namespace net { | 108 namespace net { | 
| 70 | 109 | 
| 71 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 110 TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) { | 
| 72 net::test_server::EmbeddedTestServer embedded_test_server; | |
| 73 embedded_test_server.ServeFilesFromDirectory( | |
| 74 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 75 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | |
| 76 | |
| 77 base::HistogramTester histogram_tester; | 111 base::HistogramTester histogram_tester; | 
| 78 // Enable requests to local host to be used for network quality estimation. | 112 // Enable requests to local host to be used for network quality estimation. | 
| 79 std::map<std::string, std::string> variation_params; | 113 std::map<std::string, std::string> variation_params; | 
| 80 TestNetworkQualityEstimator estimator(variation_params); | 114 TestNetworkQualityEstimator estimator(variation_params); | 
| 81 | 115 | 
| 82 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 116 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 
| 83 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 117 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 
| 84 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 118 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 
| 85 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 119 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 86 base::TimeTicks(), 100)); | 120 base::TimeTicks(), 100)); | 
| 87 | 121 | 
| 88 TestDelegate test_delegate; | 122 TestDelegate test_delegate; | 
| 89 TestURLRequestContext context(false); | 123 TestNetworkDelegate network_delegate; // Must outlive URLRequest. | 
| 124 TestURLRequestContext context(true); | |
| 125 context.set_network_quality_estimator(&estimator); | |
| 126 context.set_network_delegate(&network_delegate); | |
| 127 context.Init(); | |
| 90 | 128 | 
| 91 scoped_ptr<URLRequest> request( | 129 scoped_ptr<URLRequest> request(context.CreateRequest( | 
| 92 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 130 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 
| 93 DEFAULT_PRIORITY, &test_delegate)); | |
| 94 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 131 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME); | 
| 95 request->Start(); | 132 request->Start(); | 
| 96 | |
| 97 base::RunLoop().Run(); | 133 base::RunLoop().Run(); | 
| 98 | 134 | 
| 99 // Both RTT and downstream throughput should be updated. | 135 // Both RTT and downstream throughput should be updated. | 
| 100 estimator.NotifyHeadersReceived(*request); | |
| 101 estimator.NotifyRequestCompleted(*request); | |
| 102 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), | 136 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), | 
| 103 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 137 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 
| 104 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, | 138 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, | 
| 105 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 139 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 106 base::TimeTicks(), 100)); | 140 base::TimeTicks(), 100)); | 
| 107 | 141 | 
| 108 base::TimeDelta rtt = NetworkQualityEstimator::InvalidRTT(); | 142 base::TimeDelta rtt = NetworkQualityEstimator::InvalidRTT(); | 
| 109 int32_t kbps = NetworkQualityEstimator::kInvalidThroughput; | 143 int32_t kbps = NetworkQualityEstimator::kInvalidThroughput; | 
| 110 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 144 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | 
| 111 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 145 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 
| 112 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), rtt); | 146 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), rtt); | 
| 113 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, kbps); | 147 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, kbps); | 
| 114 | 148 | 
| 115 EXPECT_NEAR( | 149 EXPECT_NEAR( | 
| 116 rtt.InMilliseconds(), | 150 rtt.InMilliseconds(), | 
| 117 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100).InMilliseconds(), | 151 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100).InMilliseconds(), | 
| 118 1); | 152 1); | 
| 119 | 153 | 
| 120 // Check UMA histograms. | 154 // Check UMA histograms. | 
| 121 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 155 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); | 
| 122 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 156 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); | 
| 123 | 157 | 
| 124 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0); | 158 histogram_tester.ExpectTotalCount("NQE.RatioEstimatedToActualRTT.Unknown", 0); | 
| 125 | 159 | 
| 126 estimator.NotifyHeadersReceived(*request); | 160 scoped_ptr<URLRequest> request2(context.CreateRequest( | 
| 127 estimator.NotifyRequestCompleted(*request); | 161 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 
| 162 request2->SetLoadFlags(request2->load_flags() | LOAD_MAIN_FRAME); | |
| 163 request2->Start(); | |
| 164 base::RunLoop().Run(); | |
| 165 | |
| 128 histogram_tester.ExpectTotalCount("NQE.RTTObservations.Unknown", 1); | 166 histogram_tester.ExpectTotalCount("NQE.RTTObservations.Unknown", 1); | 
| 129 estimator.SimulateNetworkChangeTo( | 167 estimator.SimulateNetworkChangeTo( | 
| 130 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | 168 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | 
| 131 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 169 histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); | 
| 132 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 170 histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); | 
| 133 | 171 | 
| 134 histogram_tester.ExpectTotalCount("NQE.RatioMedianRTT.WiFi", 0); | 172 histogram_tester.ExpectTotalCount("NQE.RatioMedianRTT.WiFi", 0); | 
| 135 | 173 | 
| 136 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile0.Unknown", 1); | 174 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile0.Unknown", 1); | 
| 137 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile10.Unknown", 1); | 175 histogram_tester.ExpectTotalCount("NQE.RTT.Percentile10.Unknown", 1); | 
| (...skipping 21 matching lines...) Expand all Loading... | |
| 159 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 197 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 
| 160 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 198 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 
| 161 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 199 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 162 base::TimeTicks(), 100)); | 200 base::TimeTicks(), 100)); | 
| 163 | 201 | 
| 164 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 202 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | 
| 165 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 203 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | 
| 166 } | 204 } | 
| 167 | 205 | 
| 168 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 206 TEST(NetworkQualityEstimatorTest, StoreObservations) { | 
| 169 net::test_server::EmbeddedTestServer embedded_test_server; | |
| 170 embedded_test_server.ServeFilesFromDirectory( | |
| 171 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 172 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | |
| 173 | |
| 174 std::map<std::string, std::string> variation_params; | 207 std::map<std::string, std::string> variation_params; | 
| 175 TestNetworkQualityEstimator estimator(variation_params); | 208 TestNetworkQualityEstimator estimator(variation_params); | 
| 209 | |
| 176 TestDelegate test_delegate; | 210 TestDelegate test_delegate; | 
| 177 TestURLRequestContext context(false); | 211 TestNetworkDelegate network_delegate; // Must outlive URLRequest. | 
| 212 TestURLRequestContext context(true); | |
| 213 context.set_network_quality_estimator(&estimator); | |
| 214 context.set_network_delegate(&network_delegate); | |
| 215 context.Init(); | |
| 178 | 216 | 
| 179 // Push 10 more observations than the maximum buffer size. | 217 // Push 10 more observations than the maximum buffer size. | 
| 180 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 10U; ++i) { | 218 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 10U; ++i) { | 
| 181 scoped_ptr<URLRequest> request( | 219 scoped_ptr<URLRequest> request(context.CreateRequest( | 
| 182 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 220 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 
| 183 DEFAULT_PRIORITY, &test_delegate)); | |
| 184 request->Start(); | 221 request->Start(); | 
| 185 base::RunLoop().Run(); | 222 base::RunLoop().Run(); | 
| 186 | |
| 187 estimator.NotifyHeadersReceived(*request); | |
| 188 estimator.NotifyRequestCompleted(*request); | |
| 189 } | 223 } | 
| 190 | 224 | 
| 191 EXPECT_EQ(static_cast<size_t>( | 225 EXPECT_EQ(static_cast<size_t>( | 
| 192 NetworkQualityEstimator::kMaximumObservationsBufferSize), | 226 NetworkQualityEstimator::kMaximumObservationsBufferSize), | 
| 193 estimator.downstream_throughput_kbps_observations_.Size()); | 227 estimator.downstream_throughput_kbps_observations_.Size()); | 
| 194 EXPECT_EQ(static_cast<size_t>( | 228 EXPECT_EQ(static_cast<size_t>( | 
| 195 NetworkQualityEstimator::kMaximumObservationsBufferSize), | 229 NetworkQualityEstimator::kMaximumObservationsBufferSize), | 
| 196 estimator.rtt_msec_observations_.Size()); | 230 estimator.rtt_msec_observations_.Size()); | 
| 197 | 231 | 
| 198 // Verify that the stored observations are cleared on network change. | 232 // Verify that the stored observations are cleared on network change. | 
| 199 estimator.SimulateNetworkChangeTo( | 233 estimator.SimulateNetworkChangeTo( | 
| 200 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2"); | 234 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-2"); | 
| 201 EXPECT_EQ(0U, estimator.downstream_throughput_kbps_observations_.Size()); | 235 EXPECT_EQ(0U, estimator.downstream_throughput_kbps_observations_.Size()); | 
| 202 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 236 EXPECT_EQ(0U, estimator.rtt_msec_observations_.Size()); | 
| 203 | |
| 204 scoped_ptr<URLRequest> request( | |
| 205 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | |
| 206 DEFAULT_PRIORITY, &test_delegate)); | |
| 207 } | 237 } | 
| 208 | 238 | 
| 209 // Verifies that the percentiles are correctly computed. All observations have | 239 // Verifies that the percentiles are correctly computed. All observations have | 
| 210 // the same timestamp. Kbps percentiles must be in decreasing order. RTT | 240 // the same timestamp. Kbps percentiles must be in decreasing order. RTT | 
| 211 // percentiles must be in increasing order. | 241 // percentiles must be in increasing order. | 
| 212 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { | 242 TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) { | 
| 213 std::map<std::string, std::string> variation_params; | 243 std::map<std::string, std::string> variation_params; | 
| 214 TestNetworkQualityEstimator estimator(variation_params); | 244 TestNetworkQualityEstimator estimator(variation_params); | 
| 215 base::TimeTicks now = base::TimeTicks::Now(); | 245 base::TimeTicks now = base::TimeTicks::Now(); | 
| 216 | 246 | 
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 339 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 
| 310 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 340 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 
| 311 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 341 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 312 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 342 base::TimeTicks::Now() + base::TimeDelta::FromMinutes(10), 50)); | 
| 313 } | 343 } | 
| 314 | 344 | 
| 315 // This test notifies NetworkQualityEstimator of received data. Next, | 345 // This test notifies NetworkQualityEstimator of received data. Next, | 
| 316 // throughput and RTT percentiles are checked for correctness by doing simple | 346 // throughput and RTT percentiles are checked for correctness by doing simple | 
| 317 // verifications. | 347 // verifications. | 
| 318 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 348 TEST(NetworkQualityEstimatorTest, ComputedPercentiles) { | 
| 319 net::test_server::EmbeddedTestServer embedded_test_server; | |
| 320 embedded_test_server.ServeFilesFromDirectory( | |
| 321 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 322 ASSERT_TRUE(embedded_test_server.InitializeAndWaitUntilReady()); | |
| 323 | |
| 324 std::map<std::string, std::string> variation_params; | 349 std::map<std::string, std::string> variation_params; | 
| 325 TestNetworkQualityEstimator estimator(variation_params); | 350 TestNetworkQualityEstimator estimator(variation_params); | 
| 326 | 351 | 
| 327 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 352 EXPECT_EQ(NetworkQualityEstimator::InvalidRTT(), | 
| 328 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 353 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); | 
| 329 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 354 EXPECT_EQ(NetworkQualityEstimator::kInvalidThroughput, | 
| 330 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 355 estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 331 base::TimeTicks(), 100)); | 356 base::TimeTicks(), 100)); | 
| 332 | 357 | 
| 333 TestDelegate test_delegate; | 358 TestDelegate test_delegate; | 
| 334 TestURLRequestContext context(false); | 359 TestNetworkDelegate network_delegate; // Must outlive URLRequest. | 
| 360 TestURLRequestContext context(true); | |
| 361 context.set_network_quality_estimator(&estimator); | |
| 362 context.set_network_delegate(&network_delegate); | |
| 363 context.Init(); | |
| 335 | 364 | 
| 336 // Number of observations are more than the maximum buffer size. | 365 // Number of observations are more than the maximum buffer size. | 
| 337 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 100U; ++i) { | 366 for (size_t i = 0; i < estimator.kMaximumObservationsBufferSize + 100U; ++i) { | 
| 338 scoped_ptr<URLRequest> request( | 367 scoped_ptr<URLRequest> request(context.CreateRequest( | 
| 339 context.CreateRequest(embedded_test_server.GetURL("/echo.html"), | 368 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | 
| 340 DEFAULT_PRIORITY, &test_delegate)); | |
| 341 request->Start(); | 369 request->Start(); | 
| 342 base::RunLoop().Run(); | 370 base::RunLoop().Run(); | 
| 343 | |
| 344 // Use different number of bytes to create variation. | |
| 345 estimator.NotifyHeadersReceived(*request); | |
| 346 estimator.NotifyRequestCompleted(*request); | |
| 347 } | 371 } | 
| 348 | 372 | 
| 349 // Verify the percentiles through simple tests. | 373 // Verify the percentiles through simple tests. | 
| 350 for (int i = 0; i <= 100; ++i) { | 374 for (int i = 0; i <= 100; ++i) { | 
| 351 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 375 EXPECT_GT(estimator.GetDownlinkThroughputKbpsEstimateInternal( | 
| 352 base::TimeTicks(), i), | 376 base::TimeTicks(), i), | 
| 353 0); | 377 0); | 
| 354 EXPECT_LT(estimator.GetRTTEstimateInternal(base::TimeTicks(), i), | 378 EXPECT_LT(estimator.GetRTTEstimateInternal(base::TimeTicks(), i), | 
| 355 base::TimeDelta::Max()); | 379 base::TimeDelta::Max()); | 
| 356 | 380 | 
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 EXPECT_EQ(100, rtt.InMilliseconds()); | 671 EXPECT_EQ(100, rtt.InMilliseconds()); | 
| 648 | 672 | 
| 649 int32_t downstream_throughput_kbps; | 673 int32_t downstream_throughput_kbps; | 
| 650 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( | 674 EXPECT_FALSE(estimator.GetRecentMedianDownlinkThroughputKbps( | 
| 651 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); | 675 now + base::TimeDelta::FromSeconds(10), &downstream_throughput_kbps)); | 
| 652 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( | 676 EXPECT_TRUE(estimator.GetRecentMedianDownlinkThroughputKbps( | 
| 653 now, &downstream_throughput_kbps)); | 677 now, &downstream_throughput_kbps)); | 
| 654 EXPECT_EQ(100, downstream_throughput_kbps); | 678 EXPECT_EQ(100, downstream_throughput_kbps); | 
| 655 } | 679 } | 
| 656 | 680 | 
| 681 // An external estimate provider that does not have a valid RTT or throughput | |
| 682 // estimate. | |
| 683 class InvalidExternalEstimateProvider : public ExternalEstimateProvider { | |
| 684 public: | |
| 685 InvalidExternalEstimateProvider() : get_rtt_count_(0) {} | |
| 686 ~InvalidExternalEstimateProvider() override {} | |
| 687 | |
| 688 // ExternalEstimateProvider implementation: | |
| 689 bool GetRTT(base::TimeDelta* rtt) const override { | |
| 690 DCHECK(rtt); | |
| 691 get_rtt_count_++; | |
| 692 return false; | |
| 693 } | |
| 694 | |
| 695 // ExternalEstimateProvider implementation: | |
| 696 bool GetDownstreamThroughputKbps( | |
| 697 int32_t* downstream_throughput_kbps) const override { | |
| 698 DCHECK(downstream_throughput_kbps); | |
| 699 return false; | |
| 700 } | |
| 701 | |
| 702 // ExternalEstimateProvider implementation: | |
| 703 bool GetUpstreamThroughputKbps( | |
| 704 int32_t* upstream_throughput_kbps) const override { | |
| 705 // NetworkQualityEstimator does not support upstream throughput. | |
| 706 ADD_FAILURE(); | |
| 707 return false; | |
| 708 } | |
| 709 | |
| 710 // ExternalEstimateProvider implementation: | |
| 711 bool GetTimeSinceLastUpdate( | |
| 712 base::TimeDelta* time_since_last_update) const override { | |
| 713 *time_since_last_update = base::TimeDelta::FromMilliseconds(1); | |
| 714 return true; | |
| 715 } | |
| 716 | |
| 717 // ExternalEstimateProvider implementation: | |
| 718 void SetUpdatedEstimateDelegate(UpdatedEstimateDelegate* delegate) override {} | |
| 719 | |
| 720 // ExternalEstimateProvider implementation: | |
| 721 void Update() const override {} | |
| 722 | |
| 723 size_t get_rtt_count() const { return get_rtt_count_; } | |
| 724 | |
| 725 private: | |
| 726 // Keeps track of number of times different functions were called. | |
| 727 mutable size_t get_rtt_count_; | |
| 728 | |
| 729 DISALLOW_COPY_AND_ASSIGN(InvalidExternalEstimateProvider); | |
| 730 }; | |
| 731 | |
| 732 // Tests if the RTT value from external estimate provider is discarded if the | |
| 733 // external estimate provider is invalid. | |
| 734 TEST(NetworkQualityEstimatorTest, InvalidExternalEstimateProvider) { | |
| 735 InvalidExternalEstimateProvider* invalid_external_estimate_provider = | |
| 736 new InvalidExternalEstimateProvider(); | |
| 737 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
| 738 invalid_external_estimate_provider); | |
| 739 | |
| 740 TestNetworkQualityEstimator estimator(std::map<std::string, std::string>(), | |
| 741 external_estimate_provider.Pass()); | |
| 742 | |
| 743 base::TimeDelta rtt; | |
| 744 int32_t kbps; | |
| 745 EXPECT_EQ(1U, invalid_external_estimate_provider->get_rtt_count()); | |
| 746 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | |
| 747 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
| 748 } | |
| 749 | |
| 750 class TestExternalEstimateProvider : public ExternalEstimateProvider { | |
| 751 public: | |
| 752 TestExternalEstimateProvider(base::TimeDelta rtt, | |
| 753 int32_t downstream_throughput_kbps) | |
| 754 : rtt_(rtt), | |
| 755 downstream_throughput_kbps_(downstream_throughput_kbps), | |
| 756 time_since_last_update_(base::TimeDelta::FromSeconds(1)), | |
| 757 get_time_since_last_update_count_(0), | |
| 758 get_rtt_count_(0), | |
| 759 get_downstream_throughput_kbps_count_(0), | |
| 760 update_count_(0) {} | |
| 761 ~TestExternalEstimateProvider() override {} | |
| 762 | |
| 763 // ExternalEstimateProvider implementation: | |
| 764 bool GetRTT(base::TimeDelta* rtt) const override { | |
| 765 *rtt = rtt_; | |
| 766 get_rtt_count_++; | |
| 767 return true; | |
| 768 } | |
| 769 | |
| 770 // ExternalEstimateProvider implementation: | |
| 771 bool GetDownstreamThroughputKbps( | |
| 772 int32_t* downstream_throughput_kbps) const override { | |
| 773 *downstream_throughput_kbps = downstream_throughput_kbps_; | |
| 774 get_downstream_throughput_kbps_count_++; | |
| 775 return true; | |
| 776 } | |
| 777 | |
| 778 // ExternalEstimateProvider implementation: | |
| 779 bool GetUpstreamThroughputKbps( | |
| 780 int32_t* upstream_throughput_kbps) const override { | |
| 781 // NetworkQualityEstimator does not support upstream throughput. | |
| 782 ADD_FAILURE(); | |
| 783 return false; | |
| 784 } | |
| 785 | |
| 786 // ExternalEstimateProvider implementation: | |
| 787 bool GetTimeSinceLastUpdate( | |
| 788 base::TimeDelta* time_since_last_update) const override { | |
| 789 *time_since_last_update = time_since_last_update_; | |
| 790 get_time_since_last_update_count_++; | |
| 791 return true; | |
| 792 } | |
| 793 | |
| 794 // ExternalEstimateProvider implementation: | |
| 795 void SetUpdatedEstimateDelegate(UpdatedEstimateDelegate* delegate) override {} | |
| 796 | |
| 797 // ExternalEstimateProvider implementation: | |
| 798 void Update() const override { update_count_++; } | |
| 799 | |
| 800 void set_time_since_last_update(base::TimeDelta time_since_last_update) { | |
| 801 time_since_last_update_ = time_since_last_update; | |
| 802 } | |
| 803 | |
| 804 size_t get_time_since_last_update_count() const { | |
| 805 return get_time_since_last_update_count_; | |
| 806 } | |
| 807 size_t get_rtt_count() const { return get_rtt_count_; } | |
| 808 size_t get_downstream_throughput_kbps_count() const { | |
| 809 return get_downstream_throughput_kbps_count_; | |
| 810 } | |
| 811 size_t update_count() const { return update_count_; } | |
| 812 | |
| 813 private: | |
| 814 // RTT and downstream throughput estimates. | |
| 815 const base::TimeDelta rtt_; | |
| 816 const int32_t downstream_throughput_kbps_; | |
| 817 | |
| 818 base::TimeDelta time_since_last_update_; | |
| 819 | |
| 820 // Keeps track of number of times different functions were called. | |
| 821 mutable size_t get_time_since_last_update_count_; | |
| 822 mutable size_t get_rtt_count_; | |
| 823 mutable size_t get_downstream_throughput_kbps_count_; | |
| 824 mutable size_t update_count_; | |
| 825 | |
| 826 DISALLOW_COPY_AND_ASSIGN(TestExternalEstimateProvider); | |
| 827 }; | |
| 828 | |
| 829 // Tests if the external estimate provider is called in the constructor and | |
| 830 // on network change notification. | |
| 831 TEST(NetworkQualityEstimatorTest, TestExternalEstimateProvider) { | |
| 832 TestExternalEstimateProvider* test_external_estimate_provider = | |
| 833 new TestExternalEstimateProvider(base::TimeDelta::FromMilliseconds(1), | |
| 834 100); | |
| 835 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
| 836 test_external_estimate_provider); | |
| 837 std::map<std::string, std::string> variation_params; | |
| 838 TestNetworkQualityEstimator estimator(variation_params, | |
| 839 external_estimate_provider.Pass()); | |
| 840 | |
| 841 base::TimeDelta rtt; | |
| 842 int32_t kbps; | |
| 843 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
| 844 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
| 845 | |
| 846 EXPECT_EQ( | |
| 847 1U, test_external_estimate_provider->get_time_since_last_update_count()); | |
| 848 EXPECT_EQ(1U, test_external_estimate_provider->get_rtt_count()); | |
| 849 EXPECT_EQ( | |
| 850 1U, | |
| 851 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
| 852 | |
| 853 // Change network type to WiFi. Number of queries to External estimate | |
| 854 // provider must increment. | |
| 855 estimator.SimulateNetworkChangeTo( | |
| 856 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, "test-1"); | |
| 857 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
| 858 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
| 859 EXPECT_EQ( | |
| 860 2U, test_external_estimate_provider->get_time_since_last_update_count()); | |
| 861 EXPECT_EQ(2U, test_external_estimate_provider->get_rtt_count()); | |
| 862 EXPECT_EQ( | |
| 863 2U, | |
| 864 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
| 865 | |
| 866 // Change network type to 2G. Number of queries to External estimate provider | |
| 867 // must increment. | |
| 868 estimator.SimulateNetworkChangeTo( | |
| 869 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-1"); | |
| 870 EXPECT_EQ( | |
| 871 3U, test_external_estimate_provider->get_time_since_last_update_count()); | |
| 872 EXPECT_EQ(3U, test_external_estimate_provider->get_rtt_count()); | |
| 873 EXPECT_EQ( | |
| 874 3U, | |
| 875 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
| 876 | |
| 877 // Set the external estimate as old. Network Quality estimator should request | |
| 878 // an update on connection type change. | |
| 879 EXPECT_EQ(0U, test_external_estimate_provider->update_count()); | |
| 880 test_external_estimate_provider->set_time_since_last_update( | |
| 881 base::TimeDelta::Max()); | |
| 882 | |
| 883 estimator.SimulateNetworkChangeTo( | |
| 884 NetworkChangeNotifier::ConnectionType::CONNECTION_2G, "test-2"); | |
| 885 EXPECT_EQ( | |
| 886 4U, test_external_estimate_provider->get_time_since_last_update_count()); | |
| 887 EXPECT_EQ(3U, test_external_estimate_provider->get_rtt_count()); | |
| 888 EXPECT_EQ( | |
| 889 3U, | |
| 890 test_external_estimate_provider->get_downstream_throughput_kbps_count()); | |
| 891 EXPECT_EQ(1U, test_external_estimate_provider->update_count()); | |
| 892 | |
| 893 // Estimates are unavailable because external estimate provider never | |
| 894 // notifies network quality estimator of the updated estimates. | |
| 895 EXPECT_FALSE(estimator.GetRTTEstimate(&rtt)); | |
| 896 EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
| 897 } | |
| 898 | |
| 899 // Tests if the estimate from the external estimate provider is merged with the | |
| 900 // observations collected from the HTTP requests. | |
| 901 TEST(NetworkQualityEstimatorTest, TestExternalEstimateProviderMergeEstimates) { | |
| 902 const base::TimeDelta external_estimate_provider_rtt = | |
| 903 base::TimeDelta::FromMilliseconds(1); | |
| 904 const int32_t external_estimate_provider_downstream_throughput = 100; | |
| 905 TestExternalEstimateProvider* test_external_estimate_provider = | |
| 906 new TestExternalEstimateProvider( | |
| 907 external_estimate_provider_rtt, | |
| 908 external_estimate_provider_downstream_throughput); | |
| 909 scoped_ptr<ExternalEstimateProvider> external_estimate_provider( | |
| 910 test_external_estimate_provider); | |
| 911 | |
| 912 std::map<std::string, std::string> variation_params; | |
| 913 TestNetworkQualityEstimator estimator(variation_params, | |
| 914 external_estimate_provider.Pass()); | |
| 915 | |
| 916 base::TimeDelta rtt; | |
| 917 // Estimate provided by network quality estimator should match the estimate | |
| 918 // provided by external estimate provider. | |
| 919 EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); | |
| 920 EXPECT_EQ(external_estimate_provider_rtt, rtt); | |
| 921 | |
| 922 int32_t kbps; | |
| 923 EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); | |
| 924 EXPECT_EQ(external_estimate_provider_downstream_throughput, kbps); | |
| 925 | |
| 926 EXPECT_EQ(1U, estimator.rtt_msec_observations_.Size()); | |
| 927 EXPECT_EQ(1U, estimator.downstream_throughput_kbps_observations_.Size()); | |
| 928 | |
| 929 TestDelegate test_delegate; | |
| 930 TestNetworkDelegate network_delegate; // Must outlive URLRequest. | |
| 
mmenke
2015/09/15 22:36:53
I don't think we need this?
 
tbansal1
2015/09/15 22:44:47
Removed it. Seems like it is required for LocalHtt
 | |
| 931 TestURLRequestContext context(true); | |
| 932 context.set_network_quality_estimator(&estimator); | |
| 933 context.set_network_delegate(&network_delegate); | |
| 934 context.Init(); | |
| 935 | |
| 936 scoped_ptr<URLRequest> request(context.CreateRequest( | |
| 937 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | |
| 938 request->Start(); | |
| 939 base::RunLoop().Run(); | |
| 940 | |
| 941 EXPECT_EQ(2U, estimator.rtt_msec_observations_.Size()); | |
| 942 EXPECT_EQ(2U, estimator.downstream_throughput_kbps_observations_.Size()); | |
| 943 } | |
| 944 | |
| 657 } // namespace net | 945 } // namespace net | 
| OLD | NEW |