Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: net/base/network_quality_estimator_unittest.cc

Issue 1376473003: Notify NQE of TCP RTT values (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased, addressed sleevi comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <limits> 9 #include <limits>
9 #include <map> 10 #include <map>
11 #include <string>
10 #include <utility> 12 #include <utility>
11 #include <vector> 13 #include <vector>
12 14
13 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
15 #include "base/macros.h" 17 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram_samples.h" 19 #include "base/metrics/histogram_samples.h"
18 #include "base/run_loop.h" 20 #include "base/run_loop.h"
19 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
20 #include "base/test/histogram_tester.h" 22 #include "base/test/histogram_tester.h"
21 #include "base/time/time.h" 23 #include "base/time/time.h"
22 #include "build/build_config.h" 24 #include "build/build_config.h"
23 #include "net/base/external_estimate_provider.h" 25 #include "net/base/external_estimate_provider.h"
24 #include "net/base/load_flags.h" 26 #include "net/base/load_flags.h"
25 #include "net/base/network_change_notifier.h" 27 #include "net/base/network_change_notifier.h"
28 #include "net/http/http_network_session.h"
26 #include "net/http/http_status_code.h" 29 #include "net/http/http_status_code.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h" 30 #include "net/test/embedded_test_server/embedded_test_server.h"
28 #include "net/test/embedded_test_server/http_request.h" 31 #include "net/test/embedded_test_server/http_request.h"
29 #include "net/test/embedded_test_server/http_response.h" 32 #include "net/test/embedded_test_server/http_response.h"
33 #include "net/url_request/url_request.h"
30 #include "net/url_request/url_request_test_util.h" 34 #include "net/url_request/url_request_test_util.h"
31 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
32 #include "url/gurl.h" 36 #include "url/gurl.h"
33 37
38 #if defined(TCP_INFO)
39 #include <netinet/tcp.h>
40 #endif
41
34 namespace { 42 namespace {
35 43
36 // Helps in setting the current network type and id. 44 // Helps in setting the current network type and id.
37 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { 45 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator {
38 public: 46 public:
39 TestNetworkQualityEstimator( 47 TestNetworkQualityEstimator(
40 const std::map<std::string, std::string>& variation_params, 48 const std::map<std::string, std::string>& variation_params,
41 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider) 49 scoped_ptr<net::ExternalEstimateProvider> external_estimate_provider)
42 : NetworkQualityEstimator(std::move(external_estimate_provider), 50 : NetworkQualityEstimator(std::move(external_estimate_provider),
43 variation_params, 51 variation_params,
44 true, 52 true,
45 true) { 53 true),
54 watcher_reset_notification_received_count_(0) {
46 // Set up embedded test server. 55 // Set up embedded test server.
47 embedded_test_server_.ServeFilesFromDirectory( 56 embedded_test_server_.ServeFilesFromDirectory(
48 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 57 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
49 EXPECT_TRUE(embedded_test_server_.Start()); 58 EXPECT_TRUE(embedded_test_server_.Start());
50 embedded_test_server_.RegisterRequestHandler(base::Bind( 59 embedded_test_server_.RegisterRequestHandler(base::Bind(
51 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); 60 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this)));
52 } 61 }
53 62
54 explicit TestNetworkQualityEstimator( 63 explicit TestNetworkQualityEstimator(
55 const std::map<std::string, std::string>& variation_params) 64 const std::map<std::string, std::string>& variation_params)
(...skipping 21 matching lines...) Expand all
77 http_response->set_content("hello"); 86 http_response->set_content("hello");
78 http_response->set_content_type("text/plain"); 87 http_response->set_content_type("text/plain");
79 return std::move(http_response); 88 return std::move(http_response);
80 } 89 }
81 90
82 // Returns a GURL hosted at embedded test server. 91 // Returns a GURL hosted at embedded test server.
83 const GURL GetEchoURL() const { 92 const GURL GetEchoURL() const {
84 return embedded_test_server_.GetURL("/echo.html"); 93 return embedded_test_server_.GetURL("/echo.html");
85 } 94 }
86 95
96 size_t watcher_reset_notification_received_count() const {
97 return watcher_reset_notification_received_count_;
98 }
99
87 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; 100 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
88 using NetworkQualityEstimator::OnConnectionTypeChanged; 101 using NetworkQualityEstimator::OnConnectionTypeChanged;
89 102
90 private: 103 private:
91 // NetworkQualityEstimator implementation that returns the overridden network 104 // NetworkQualityEstimator implementation that returns the overridden network
92 // id (instead of invoking platform APIs). 105 // id (instead of invoking platform APIs).
93 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { 106 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override {
94 return NetworkQualityEstimator::NetworkID(current_network_type_, 107 return NetworkQualityEstimator::NetworkID(current_network_type_,
95 current_network_id_); 108 current_network_id_);
96 } 109 }
97 110
98 net::NetworkChangeNotifier::ConnectionType current_network_type_; 111 net::NetworkChangeNotifier::ConnectionType current_network_type_;
99 std::string current_network_id_; 112 std::string current_network_id_;
100 113
101 // Embedded server used for testing. 114 // Embedded server used for testing.
102 net::EmbeddedTestServer embedded_test_server_; 115 net::EmbeddedTestServer embedded_test_server_;
103 116
117 size_t watcher_reset_notification_received_count_;
118
104 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 119 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
105 }; 120 };
106 121
107 class TestRTTObserver : public net::NetworkQualityEstimator::RTTObserver { 122 class TestRTTObserver : public net::NetworkQualityEstimator::RTTObserver {
108 public: 123 public:
109 struct Observation { 124 struct Observation {
110 Observation(int32_t ms, 125 Observation(int32_t ms,
111 const base::TimeTicks& ts, 126 const base::TimeTicks& ts,
112 net::NetworkQualityEstimator::ObservationSource src) 127 net::NetworkQualityEstimator::ObservationSource src)
113 : rtt_ms(ms), timestamp(ts), source(src) {} 128 : rtt_ms(ms), timestamp(ts), source(src) {}
(...skipping 940 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 1069
1055 // Both RTT and downstream throughput should be updated. 1070 // Both RTT and downstream throughput should be updated.
1056 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(), 1071 EXPECT_NE(NetworkQualityEstimator::InvalidRTT(),
1057 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100)); 1072 estimator.GetRTTEstimateInternal(base::TimeTicks(), 100));
1058 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput, 1073 EXPECT_NE(NetworkQualityEstimator::kInvalidThroughput,
1059 estimator.GetDownlinkThroughputKbpsEstimateInternal( 1074 estimator.GetDownlinkThroughputKbpsEstimateInternal(
1060 base::TimeTicks(), 100)); 1075 base::TimeTicks(), 100));
1061 1076
1062 EXPECT_EQ(2U, rtt_observer.observations().size()); 1077 EXPECT_EQ(2U, rtt_observer.observations().size());
1063 EXPECT_EQ(2U, throughput_observer.observations().size()); 1078 EXPECT_EQ(2U, throughput_observer.observations().size());
1064 for (auto observation : rtt_observer.observations()) { 1079 for (const auto& observation : rtt_observer.observations()) {
1065 EXPECT_LE(0, observation.rtt_ms); 1080 EXPECT_LE(0, observation.rtt_ms);
1066 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); 1081 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
1067 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); 1082 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source);
1068 } 1083 }
1069 for (auto observation : throughput_observer.observations()) { 1084 for (const auto& observation : throughput_observer.observations()) {
1070 EXPECT_LE(0, observation.throughput_kbps); 1085 EXPECT_LE(0, observation.throughput_kbps);
1071 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds()); 1086 EXPECT_LE(0, (observation.timestamp - then).InMilliseconds());
1072 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source); 1087 EXPECT_EQ(NetworkQualityEstimator::URL_REQUEST, observation.source);
1073 } 1088 }
1074 1089
1075 // Verify that observations from TCP and QUIC are passed on to the observers. 1090 // Verify that observations from TCP and QUIC are passed on to the observers.
1076 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1)); 1091 base::TimeDelta tcp_rtt(base::TimeDelta::FromMilliseconds(1));
1077 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2)); 1092 base::TimeDelta quic_rtt(base::TimeDelta::FromMilliseconds(2));
1078 1093
1079 scoped_ptr<SocketPerformanceWatcher> tcp_watcher = 1094 scoped_ptr<SocketPerformanceWatcher> tcp_watcher =
1080 estimator.CreateSocketPerformanceWatcher( 1095 estimator.GetSocketPerformanceWatcherFactory()
1081 SocketPerformanceWatcherFactory::PROTOCOL_TCP); 1096 ->CreateSocketPerformanceWatcher(
1097 SocketPerformanceWatcherFactory::PROTOCOL_TCP);
1082 scoped_ptr<SocketPerformanceWatcher> quic_watcher = 1098 scoped_ptr<SocketPerformanceWatcher> quic_watcher =
1083 estimator.CreateSocketPerformanceWatcher( 1099 estimator.GetSocketPerformanceWatcherFactory()
1084 SocketPerformanceWatcherFactory::PROTOCOL_QUIC); 1100 ->CreateSocketPerformanceWatcher(
1101 SocketPerformanceWatcherFactory::PROTOCOL_QUIC);
1085 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt); 1102 tcp_watcher->OnUpdatedRTTAvailable(tcp_rtt);
1086 quic_watcher->OnUpdatedRTTAvailable(quic_rtt); 1103 quic_watcher->OnUpdatedRTTAvailable(quic_rtt);
1087 1104
1105 base::RunLoop().RunUntilIdle();
1106
1088 EXPECT_EQ(4U, rtt_observer.observations().size()); 1107 EXPECT_EQ(4U, rtt_observer.observations().size());
1089 EXPECT_EQ(2U, throughput_observer.observations().size()); 1108 EXPECT_EQ(2U, throughput_observer.observations().size());
1090 1109
1091 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms); 1110 EXPECT_EQ(tcp_rtt.InMilliseconds(), rtt_observer.observations().at(2).rtt_ms);
1092 EXPECT_EQ(quic_rtt.InMilliseconds(), 1111 EXPECT_EQ(quic_rtt.InMilliseconds(),
1093 rtt_observer.observations().at(3).rtt_ms); 1112 rtt_observer.observations().at(3).rtt_ms);
1094 } 1113 }
1095 1114
1115 // TestTCPSocketRTT requires kernel support for tcp_info struct, and so it is
1116 // enabled only on the POSIX platforms.
1117 #if defined(TCP_INFO)
1118 #define MAYBE_TestTCPSocketRTT TestTCPSocketRTT
1119 #else
1120 #define MAYBE_TestTCPSocketRTT DISABLED_TestTCPSocketRTT
1121 #endif
1122 // Tests that the TCP socket notifies the Network Quality Estimator of TCP RTTs,
1123 // which in turn notifies registered RTT observers.
1124 TEST(NetworkQualityEstimatorTest, MAYBE_TestTCPSocketRTT) {
1125 TestRTTObserver rtt_observer;
1126 std::map<std::string, std::string> variation_params;
1127 TestNetworkQualityEstimator estimator(variation_params);
1128 estimator.AddRTTObserver(&rtt_observer);
1129
1130 TestDelegate test_delegate;
1131 TestURLRequestContext context(true);
1132 context.set_network_quality_estimator(&estimator);
1133
1134 scoped_ptr<HttpNetworkSession::Params> params(new HttpNetworkSession::Params);
1135 // |estimator| should be notified of TCP RTT observations.
1136 params->socket_performance_watcher_factory =
1137 estimator.GetSocketPerformanceWatcherFactory();
1138 context.set_http_network_session_params(std::move(params));
1139 context.Init();
1140
1141 EXPECT_EQ(0U, rtt_observer.observations().size());
1142 EXPECT_EQ(0U, estimator.watcher_reset_notification_received_count());
1143
1144 const size_t kNumRequests = 2;
1145
1146 // Send two requests. Verify that the completion of each request generates at
1147 // least one TCP RTT observation.
1148 for (size_t i = 0; i < kNumRequests; ++i) {
1149 size_t before_count_tcp_rtt_observations = 0;
1150 for (const auto& observation : rtt_observer.observations()) {
1151 if (observation.source == NetworkQualityEstimator::TCP)
1152 ++before_count_tcp_rtt_observations;
1153 }
1154
1155 scoped_ptr<URLRequest> request(context.CreateRequest(
1156 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
1157 request->Start();
1158 base::RunLoop().Run();
1159
1160 size_t after_count_tcp_rtt_observations = 0;
1161 for (const auto& observation : rtt_observer.observations()) {
1162 if (observation.source == NetworkQualityEstimator::TCP)
1163 ++after_count_tcp_rtt_observations;
1164 }
1165 // Exactly one notification should be received per socket performance
1166 // watcher because the delay between notifications is currently set to a
1167 // high value.
1168 EXPECT_EQ(1U, after_count_tcp_rtt_observations -
1169 before_count_tcp_rtt_observations)
1170 << i;
1171 }
1172 // At least one reset should be received for each request.
1173 EXPECT_LE(kNumRequests,
1174 estimator.watcher_reset_notification_received_count());
1175 }
1176
1096 } // namespace net 1177 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698