Chromium Code Reviews| 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/nqe/network_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 | 53 |
| 54 // EffectiveConnectionTypeObserver implementation: | 54 // EffectiveConnectionTypeObserver implementation: |
| 55 void OnEffectiveConnectionTypeChanged(EffectiveConnectionType type) override { | 55 void OnEffectiveConnectionTypeChanged(EffectiveConnectionType type) override { |
| 56 effective_connection_types_.push_back(type); | 56 effective_connection_types_.push_back(type); |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 std::vector<EffectiveConnectionType> effective_connection_types_; | 60 std::vector<EffectiveConnectionType> effective_connection_types_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 class TestNetworkQualityObserver | |
| 64 : public NetworkQualityEstimator::NetworkQualityObserver { | |
| 65 public: | |
| 66 TestNetworkQualityObserver() | |
| 67 : http_rtt_(nqe::internal::InvalidRTT()), | |
| 68 transport_rtt_(nqe::internal::InvalidRTT()), | |
| 69 downstream_throughput_kbps_(nqe::internal::kInvalidThroughput), | |
| 70 notifications_received_(0U) {} | |
| 71 | |
| 72 // NetworkQualityObserver implementation: | |
| 73 void OnNetworkQualityChanged(base::TimeDelta http_rtt, | |
| 74 base::TimeDelta transport_rtt, | |
| 75 int32_t downstream_throughput_kbps) override { | |
| 76 http_rtt_ = http_rtt; | |
| 77 transport_rtt_ = transport_rtt; | |
| 78 downstream_throughput_kbps_ = downstream_throughput_kbps; | |
| 79 notifications_received_++; | |
| 80 } | |
| 81 | |
| 82 size_t notifications_received() const { return notifications_received_; } | |
| 83 | |
| 84 base::TimeDelta http_rtt() const { return http_rtt_; } | |
| 85 base::TimeDelta transport_rtt() const { return transport_rtt_; } | |
| 86 int32_t downstream_throughput_kbps() const { | |
| 87 return downstream_throughput_kbps_; | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 base::TimeDelta http_rtt_; | |
| 92 base::TimeDelta transport_rtt_; | |
| 93 int32_t downstream_throughput_kbps_; | |
| 94 size_t notifications_received_; | |
|
RyanSturm
2016/10/11 01:46:26
nit: Why size_t here and not int? There's not real
tbansal1
2016/10/11 17:46:12
Done.
Not sure it matters though. From https://ch
| |
| 95 }; | |
| 96 | |
| 63 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { | 97 class TestRTTObserver : public NetworkQualityEstimator::RTTObserver { |
| 64 public: | 98 public: |
| 65 struct Observation { | 99 struct Observation { |
| 66 Observation(int32_t ms, | 100 Observation(int32_t ms, |
| 67 const base::TimeTicks& ts, | 101 const base::TimeTicks& ts, |
| 68 NetworkQualityObservationSource src) | 102 NetworkQualityObservationSource src) |
| 69 : rtt_ms(ms), timestamp(ts), source(src) {} | 103 : rtt_ms(ms), timestamp(ts), source(src) {} |
| 70 int32_t rtt_ms; | 104 int32_t rtt_ms; |
| 71 base::TimeTicks timestamp; | 105 base::TimeTicks timestamp; |
| 72 NetworkQualityObservationSource source; | 106 NetworkQualityObservationSource source; |
| (...skipping 1257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1330 "test"); | 1364 "test"); |
| 1331 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1365 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
| 1332 | 1366 |
| 1333 // A change in effective connection type does not trigger notification to the | 1367 // A change in effective connection type does not trigger notification to the |
| 1334 // observers, since it is not accompanied by any new observation or a network | 1368 // observers, since it is not accompanied by any new observation or a network |
| 1335 // change event. | 1369 // change event. |
| 1336 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | 1370 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); |
| 1337 EXPECT_EQ(2U, observer.effective_connection_types().size()); | 1371 EXPECT_EQ(2U, observer.effective_connection_types().size()); |
| 1338 } | 1372 } |
| 1339 | 1373 |
| 1374 // Tests that the network quality is computed at the specified interval, and | |
| 1375 // that the network quality observers are notified of any change. | |
| 1376 TEST(NetworkQualityEstimatorTest, TestNetworkQualityObserver) { | |
| 1377 base::HistogramTester histogram_tester; | |
| 1378 std::unique_ptr<base::SimpleTestTickClock> tick_clock( | |
| 1379 new base::SimpleTestTickClock()); | |
| 1380 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); | |
| 1381 | |
| 1382 TestNetworkQualityObserver observer; | |
| 1383 std::map<std::string, std::string> variation_params; | |
| 1384 TestNetworkQualityEstimator estimator(variation_params); | |
| 1385 estimator.AddNetworkQualityObserver(&observer); | |
| 1386 estimator.SetTickClockForTesting(std::move(tick_clock)); | |
| 1387 | |
| 1388 TestDelegate test_delegate; | |
| 1389 TestURLRequestContext context(true); | |
| 1390 context.set_network_quality_estimator(&estimator); | |
| 1391 context.Init(); | |
| 1392 | |
| 1393 EXPECT_EQ(nqe::internal::InvalidRTT(), observer.http_rtt()); | |
| 1394 EXPECT_EQ(nqe::internal::InvalidRTT(), observer.transport_rtt()); | |
| 1395 EXPECT_EQ(nqe::internal::kInvalidThroughput, | |
| 1396 observer.downstream_throughput_kbps()); | |
| 1397 EXPECT_EQ(0U, observer.notifications_received()); | |
| 1398 | |
| 1399 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(100)); | |
| 1400 base::TimeDelta transport_rtt(base::TimeDelta::FromMilliseconds(200)); | |
| 1401 int32_t downstream_throughput_kbps(300); | |
| 1402 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_2G); | |
| 1403 estimator.set_recent_http_rtt(http_rtt); | |
| 1404 estimator.set_recent_transport_rtt(transport_rtt); | |
| 1405 estimator.set_recent_downlink_throughput_kbps(downstream_throughput_kbps); | |
| 1406 tick_clock_ptr->Advance(base::TimeDelta::FromMinutes(60)); | |
| 1407 | |
| 1408 std::unique_ptr<URLRequest> request(context.CreateRequest( | |
| 1409 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | |
| 1410 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | |
| 1411 request->Start(); | |
| 1412 base::RunLoop().Run(); | |
| 1413 EXPECT_EQ(http_rtt, observer.http_rtt()); | |
| 1414 EXPECT_EQ(transport_rtt, observer.transport_rtt()); | |
| 1415 EXPECT_EQ(downstream_throughput_kbps, observer.downstream_throughput_kbps()); | |
| 1416 EXPECT_EQ(1U, observer.notifications_received()); | |
| 1417 | |
| 1418 // Next request should not trigger recomputation of effective connection type | |
| 1419 // since there has been no change in the clock. | |
| 1420 std::unique_ptr<URLRequest> request2(context.CreateRequest( | |
| 1421 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); | |
| 1422 request2->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME_DEPRECATED); | |
| 1423 request2->Start(); | |
| 1424 base::RunLoop().Run(); | |
| 1425 EXPECT_EQ(1U, observer.notifications_received()); | |
| 1426 | |
| 1427 // Change in connection type should send out notification to the observers. | |
| 1428 estimator.set_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | |
| 1429 estimator.SimulateNetworkChange(NetworkChangeNotifier::CONNECTION_WIFI, | |
| 1430 "test"); | |
| 1431 EXPECT_EQ(2U, observer.notifications_received()); | |
| 1432 | |
| 1433 // A change in effective connection type does not trigger notification to the | |
| 1434 // observers, since it is not accompanied by any new observation or a network | |
| 1435 // change event. | |
| 1436 estimator.set_recent_effective_connection_type(EFFECTIVE_CONNECTION_TYPE_3G); | |
| 1437 EXPECT_EQ(2U, observer.notifications_received()); | |
| 1438 } | |
| 1439 | |
| 1340 // Tests that the effective connection type is computed on every RTT | 1440 // Tests that the effective connection type is computed on every RTT |
| 1341 // observation if the last computed effective connection type was unknown. | 1441 // observation if the last computed effective connection type was unknown. |
| 1342 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { | 1442 TEST(NetworkQualityEstimatorTest, UnknownEffectiveConnectionType) { |
| 1343 std::unique_ptr<base::SimpleTestTickClock> tick_clock( | 1443 std::unique_ptr<base::SimpleTestTickClock> tick_clock( |
| 1344 new base::SimpleTestTickClock()); | 1444 new base::SimpleTestTickClock()); |
| 1345 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); | 1445 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); |
| 1346 | 1446 |
| 1347 TestEffectiveConnectionTypeObserver observer; | 1447 TestEffectiveConnectionTypeObserver observer; |
| 1348 std::map<std::string, std::string> variation_params; | 1448 std::map<std::string, std::string> variation_params; |
| 1349 TestNetworkQualityEstimator estimator(variation_params); | 1449 TestNetworkQualityEstimator estimator(variation_params); |
| (...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2084 if (expected_count == 1) { | 2184 if (expected_count == 1) { |
| 2085 EffectiveConnectionType last_notified_type = | 2185 EffectiveConnectionType last_notified_type = |
| 2086 observer.effective_connection_types().at( | 2186 observer.effective_connection_types().at( |
| 2087 observer.effective_connection_types().size() - 1); | 2187 observer.effective_connection_types().size() - 1); |
| 2088 EXPECT_EQ(i, last_notified_type); | 2188 EXPECT_EQ(i, last_notified_type); |
| 2089 } | 2189 } |
| 2090 } | 2190 } |
| 2091 } | 2191 } |
| 2092 | 2192 |
| 2093 } // namespace net | 2193 } // namespace net |
| OLD | NEW |