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