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

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

Issue 2020353002: Record NQE accuracy at main frame requests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed bengr comments Created 4 years, 6 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/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>
11 #include <map> 11 #include <map>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/metrics/histogram_samples.h" 20 #include "base/metrics/histogram_samples.h"
21 #include "base/run_loop.h" 21 #include "base/run_loop.h"
22 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
23 #include "base/test/histogram_tester.h" 23 #include "base/test/histogram_tester.h"
24 #include "base/test/simple_test_tick_clock.h" 24 #include "base/test/simple_test_tick_clock.h"
25 #include "base/threading/platform_thread.h"
25 #include "base/time/time.h" 26 #include "base/time/time.h"
26 #include "build/build_config.h" 27 #include "build/build_config.h"
27 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
28 #include "net/base/network_change_notifier.h" 29 #include "net/base/network_change_notifier.h"
29 #include "net/http/http_status_code.h" 30 #include "net/http/http_status_code.h"
30 #include "net/nqe/external_estimate_provider.h" 31 #include "net/nqe/external_estimate_provider.h"
31 #include "net/nqe/network_quality_observation.h" 32 #include "net/nqe/network_quality_observation.h"
32 #include "net/nqe/network_quality_observation_source.h" 33 #include "net/nqe/network_quality_observation_source.h"
33 #include "net/nqe/observation_buffer.h" 34 #include "net/nqe/observation_buffer.h"
34 #include "net/socket/socket_performance_watcher.h" 35 #include "net/socket/socket_performance_watcher.h"
(...skipping 26 matching lines...) Expand all
61 const std::map<std::string, std::string>& variation_params, 62 const std::map<std::string, std::string>& variation_params,
62 bool allow_local_host_requests_for_tests, 63 bool allow_local_host_requests_for_tests,
63 bool allow_smaller_responses_for_tests) 64 bool allow_smaller_responses_for_tests)
64 : NetworkQualityEstimator(std::move(external_estimate_provider), 65 : NetworkQualityEstimator(std::move(external_estimate_provider),
65 variation_params, 66 variation_params,
66 allow_local_host_requests_for_tests, 67 allow_local_host_requests_for_tests,
67 allow_smaller_responses_for_tests), 68 allow_smaller_responses_for_tests),
68 effective_connection_type_set_(false), 69 effective_connection_type_set_(false),
69 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), 70 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
70 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN), 71 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN),
72 accuracy_recording_intervals_set_(false),
71 http_rtt_set_(false), 73 http_rtt_set_(false),
74 recent_http_rtt_set_(false),
75 transport_rtt_set_(false),
76 recent_transport_rtt_set_(false),
72 downlink_throughput_kbps_set_(false) { 77 downlink_throughput_kbps_set_(false) {
73 // Set up embedded test server. 78 // Set up embedded test server.
74 embedded_test_server_.ServeFilesFromDirectory( 79 embedded_test_server_.ServeFilesFromDirectory(
75 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 80 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
76 EXPECT_TRUE(embedded_test_server_.Start()); 81 EXPECT_TRUE(embedded_test_server_.Start());
77 embedded_test_server_.RegisterRequestHandler(base::Bind( 82 embedded_test_server_.RegisterRequestHandler(base::Bind(
78 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); 83 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this)));
79 } 84 }
80 85
81 explicit TestNetworkQualityEstimator( 86 explicit TestNetworkQualityEstimator(
(...skipping 22 matching lines...) Expand all
104 http_response->set_content("hello"); 109 http_response->set_content("hello");
105 http_response->set_content_type("text/plain"); 110 http_response->set_content_type("text/plain");
106 return std::move(http_response); 111 return std::move(http_response);
107 } 112 }
108 113
109 // Returns a GURL hosted at embedded test server. 114 // Returns a GURL hosted at embedded test server.
110 const GURL GetEchoURL() const { 115 const GURL GetEchoURL() const {
111 return embedded_test_server_.GetURL("/echo.html"); 116 return embedded_test_server_.GetURL("/echo.html");
112 } 117 }
113 118
119 void set_effective_connection_type(EffectiveConnectionType type) {
120 effective_connection_type_set_ = true;
121 effective_connection_type_ = type;
122 }
123
124 // Returns the effective connection type that was set using
125 // |set_effective_connection_type|. If connection type has not been set, then
126 // the base implementation is called.
127 EffectiveConnectionType GetEffectiveConnectionType() const override {
128 if (effective_connection_type_set_)
129 return effective_connection_type_;
130 return NetworkQualityEstimator::GetEffectiveConnectionType();
131 }
132
114 void set_http_rtt(const base::TimeDelta& http_rtt) { 133 void set_http_rtt(const base::TimeDelta& http_rtt) {
115 http_rtt_set_ = true; 134 http_rtt_set_ = true;
116 http_rtt_ = http_rtt; 135 http_rtt_ = http_rtt;
117 } 136 }
118 137
119 void set_effective_connection_type(EffectiveConnectionType type) { 138 // Returns the HTTP RTT that was set using |set_http_rtt|. If the HTTP RTT has
120 effective_connection_type_set_ = true; 139 // not been set, then the base implementation is called.
121 effective_connection_type_ = type;
122 }
123
124 EffectiveConnectionType GetEffectiveConnectionType() const override {
125 if (effective_connection_type_set_)
126 return effective_connection_type_;
127 return NetworkQualityEstimator::GetEffectiveConnectionType();
128 }
129
130 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override { 140 bool GetHttpRTTEstimate(base::TimeDelta* rtt) const override {
131 if (http_rtt_set_) { 141 if (http_rtt_set_) {
132 *rtt = http_rtt_; 142 *rtt = http_rtt_;
133 return true; 143 return true;
134 } 144 }
135 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt); 145 return NetworkQualityEstimator::GetHttpRTTEstimate(rtt);
136 } 146 }
137 147
148 void set_recent_http_rtt(const base::TimeDelta& recent_http_rtt) {
149 recent_http_rtt_set_ = true;
150 recent_http_rtt_ = recent_http_rtt;
151 }
152
153 // Returns the recent HTTP RTT that was set using |set_recent_http_rtt|. If
154 // the recent HTTP RTT has not been set, then the base implementation is
155 // called.
138 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time, 156 bool GetRecentHttpRTTMedian(const base::TimeTicks& start_time,
139 base::TimeDelta* rtt) const override { 157 base::TimeDelta* rtt) const override {
140 if (http_rtt_set_) { 158 if (recent_http_rtt_set_) {
141 *rtt = http_rtt_; 159 *rtt = recent_http_rtt_;
142 return true; 160 return true;
143 } 161 }
144 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt); 162 return NetworkQualityEstimator::GetRecentHttpRTTMedian(start_time, rtt);
145 } 163 }
146 164
147 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) { 165 void set_transport_rtt(const base::TimeDelta& transport_rtt) {
148 downlink_throughput_kbps_set_ = true; 166 transport_rtt_set_ = true;
149 downlink_throughput_kbps_ = downlink_throughput_kbps; 167 transport_rtt_ = transport_rtt;
150 } 168 }
151 169
152 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override { 170 // Returns the transport RTT that was set using |set_transport_rtt|. If the
153 if (downlink_throughput_kbps_set_) { 171 // transport RTT has not been set, then the base implementation is called.
154 *kbps = downlink_throughput_kbps_; 172 bool GetTransportRTTEstimate(base::TimeDelta* rtt) const override {
173 if (transport_rtt_set_) {
174 *rtt = transport_rtt_;
155 return true; 175 return true;
156 } 176 }
157 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps); 177 return NetworkQualityEstimator::GetTransportRTTEstimate(rtt);
158 } 178 }
159 179
160 bool GetRecentMedianDownlinkThroughputKbps(const base::TimeTicks& start_time, 180 void set_recent_transport_rtt(const base::TimeDelta& recent_transport_rtt) {
161 int32_t* kbps) const override { 181 recent_transport_rtt_set_ = true;
162 if (downlink_throughput_kbps_set_) { 182 recent_transport_rtt_ = recent_transport_rtt;
163 *kbps = downlink_throughput_kbps_;
164 return true;
165 } 183 }
Alexei Svitkine (slow) 2016/06/03 20:01:07 Nit: bad indent?
tbansal1 2016/06/03 20:57:20 Done, not sure why git cl format did not catch tha
166 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps(
167 start_time, kbps);
168 }
169 184
170 using NetworkQualityEstimator::SetTickClockForTesting; 185 // Returns the recent transport RTT that was set using
171 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; 186 // |set_recent_transport_rtt|. If the recent transport RTT has not been set,
172 using NetworkQualityEstimator::OnConnectionTypeChanged; 187 // then the base implementation is called.
188 bool GetRecentTransportRTTMedian(const base::TimeTicks& start_time,
189 base::TimeDelta* rtt) const override {
190 if (recent_transport_rtt_set_) {
191 *rtt = recent_transport_rtt_;
192 return true;
193 }
194 return NetworkQualityEstimator::GetRecentTransportRTTMedian(start_time,
195 rtt);
196 }
173 197
174 private: 198 void set_downlink_throughput_kbps(int32_t downlink_throughput_kbps) {
175 // NetworkQualityEstimator implementation that returns the overridden network 199 downlink_throughput_kbps_set_ = true;
176 // id (instead of invoking platform APIs). 200 downlink_throughput_kbps_ = downlink_throughput_kbps;
177 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { 201 }
178 return NetworkQualityEstimator::NetworkID(current_network_type_,
179 current_network_id_);
180 }
181 202
182 bool effective_connection_type_set_; 203 // Returns the downlink throughput that was set using
183 EffectiveConnectionType effective_connection_type_; 204 // |set_downlink_throughput_kbps|. If the downlink throughput has not been
205 // set, then the base implementation is called.
206 bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const override {
207 if (downlink_throughput_kbps_set_) {
208 *kbps = downlink_throughput_kbps_;
209 return true;
210 }
211 return NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(kbps);
212 }
184 213
185 NetworkChangeNotifier::ConnectionType current_network_type_; 214 // Returns the downlink throughput that was set using
186 std::string current_network_id_; 215 // |set_downlink_throughput_kbps|. If the downlink throughput has not been
216 // set, then the base implementation is called.
217 bool GetRecentMedianDownlinkThroughputKbps(
218 const base::TimeTicks& start_time,
219 int32_t* kbps) const override {
220 if (downlink_throughput_kbps_set_) {
221 *kbps = downlink_throughput_kbps_;
222 return true;
223 }
224 return NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps(
225 start_time, kbps);
226 }
187 227
188 bool http_rtt_set_; 228 void SetAccuracyRecordingIntervals(
189 base::TimeDelta http_rtt_; 229 const std::vector<base::TimeDelta>& accuracy_recording_intervals) {
230 accuracy_recording_intervals_set_ = true;
231 accuracy_recording_intervals_ = accuracy_recording_intervals;
232 }
190 233
191 bool downlink_throughput_kbps_set_; 234 const std::vector<base::TimeDelta>& GetAccuracyRecordingIntervals()
192 int32_t downlink_throughput_kbps_; 235 const override {
236 if (accuracy_recording_intervals_set_)
237 return accuracy_recording_intervals_;
193 238
194 // Embedded server used for testing. 239 return NetworkQualityEstimator::GetAccuracyRecordingIntervals();
195 EmbeddedTestServer embedded_test_server_; 240 }
196 241
197 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 242 using NetworkQualityEstimator::SetTickClockForTesting;
243 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
244 using NetworkQualityEstimator::OnConnectionTypeChanged;
245
246 private:
247 // NetworkQualityEstimator implementation that returns the overridden
248 // network
249 // id (instead of invoking platform APIs).
250 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override {
251 return NetworkQualityEstimator::NetworkID(current_network_type_,
252 current_network_id_);
253 }
254
255 bool effective_connection_type_set_;
256 EffectiveConnectionType effective_connection_type_;
257
258 NetworkChangeNotifier::ConnectionType current_network_type_;
259 std::string current_network_id_;
260
261 bool accuracy_recording_intervals_set_;
262 std::vector<base::TimeDelta> accuracy_recording_intervals_;
263
264 bool http_rtt_set_;
265 base::TimeDelta http_rtt_;
266
267 bool recent_http_rtt_set_;
268 base::TimeDelta recent_http_rtt_;
269
270 bool transport_rtt_set_;
271 base::TimeDelta transport_rtt_;
272
273 bool recent_transport_rtt_set_;
274 base::TimeDelta recent_transport_rtt_;
275
276 bool downlink_throughput_kbps_set_;
277 int32_t downlink_throughput_kbps_;
278
279 // Embedded server used for testing.
280 EmbeddedTestServer embedded_test_server_;
281
282 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
198 }; 283 };
199 284
200 class TestEffectiveConnectionTypeObserver 285 class TestEffectiveConnectionTypeObserver
201 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver { 286 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver {
202 public: 287 public:
203 std::vector<NetworkQualityEstimator::EffectiveConnectionType>& 288 std::vector<NetworkQualityEstimator::EffectiveConnectionType>&
204 effective_connection_types() { 289 effective_connection_types() {
205 return effective_connection_types_; 290 return effective_connection_types_;
206 } 291 }
207 292
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 const struct { 591 const struct {
507 int32_t rtt_msec; 592 int32_t rtt_msec;
508 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type; 593 NetworkQualityEstimator::EffectiveConnectionType expected_conn_type;
509 } tests[] = { 594 } tests[] = {
510 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, 595 {5000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND},
511 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, 596 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND},
512 }; 597 };
513 598
514 for (const auto& test : tests) { 599 for (const auto& test : tests) {
515 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); 600 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec));
601 estimator.set_recent_http_rtt(
602 base::TimeDelta::FromMilliseconds(test.rtt_msec));
516 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); 603 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType());
517 } 604 }
518 } 605 }
519 606
520 // Tests that |GetEffectiveConnectionType| returns 607 // Tests that |GetEffectiveConnectionType| returns
521 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline. 608 // EFFECTIVE_CONNECTION_TYPE_OFFLINE when the device is currently offline.
522 TEST(NetworkQualityEstimatorTest, Offline) { 609 TEST(NetworkQualityEstimatorTest, Offline) {
523 std::map<std::string, std::string> variation_params; 610 std::map<std::string, std::string> variation_params;
524 TestNetworkQualityEstimator estimator(variation_params); 611 TestNetworkQualityEstimator estimator(variation_params);
525 612
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, 662 {500, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G},
576 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, 663 {400, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G},
577 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G}, 664 {300, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_4G},
578 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, 665 {200, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND},
579 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, 666 {100, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND},
580 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND}, 667 {20, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_BROADBAND},
581 }; 668 };
582 669
583 for (const auto& test : tests) { 670 for (const auto& test : tests) {
584 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); 671 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec));
672 estimator.set_recent_http_rtt(
673 base::TimeDelta::FromMilliseconds(test.rtt_msec));
585 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); 674 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType());
586 } 675 }
587 } 676 }
588 677
589 // Tests that |GetEffectiveConnectionType| returns correct connection type when 678 // Tests that |GetEffectiveConnectionType| returns correct connection type when
590 // both RTT and throughput thresholds are specified in the variation params. 679 // both RTT and throughput thresholds are specified in the variation params.
591 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) { 680 TEST(NetworkQualityEstimatorTest, ObtainThresholdsRTTandThroughput) {
592 std::map<std::string, std::string> variation_params; 681 std::map<std::string, std::string> variation_params;
593 682
594 variation_params["Offline.ThresholdMedianHttpRTTMsec"] = "4000"; 683 variation_params["Offline.ThresholdMedianHttpRTTMsec"] = "4000";
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 // Set both RTT and throughput. RTT is the bottleneck. 723 // Set both RTT and throughput. RTT is the bottleneck.
635 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, 724 {3000, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G},
636 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, 725 {700, 25000, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G},
637 // Set throughput to an invalid value. 726 // Set throughput to an invalid value.
638 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G}, 727 {3000, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G},
639 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G}, 728 {700, 0, NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_3G},
640 }; 729 };
641 730
642 for (const auto& test : tests) { 731 for (const auto& test : tests) {
643 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec)); 732 estimator.set_http_rtt(base::TimeDelta::FromMilliseconds(test.rtt_msec));
733 estimator.set_recent_http_rtt(
734 base::TimeDelta::FromMilliseconds(test.rtt_msec));
644 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps); 735 estimator.set_downlink_throughput_kbps(test.downlink_throughput_kbps);
645 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType()); 736 EXPECT_EQ(test.expected_conn_type, estimator.GetEffectiveConnectionType());
646 } 737 }
647 } 738 }
648 739
649 // Tests if |weight_multiplier_per_second_| is set to correct value for various 740 // Tests if |weight_multiplier_per_second_| is set to correct value for various
650 // values of half life parameter. 741 // values of half life parameter.
651 TEST(NetworkQualityEstimatorTest, HalfLifeParam) { 742 TEST(NetworkQualityEstimatorTest, HalfLifeParam) {
652 std::map<std::string, std::string> variation_params; 743 std::map<std::string, std::string> variation_params;
653 744
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after
1453 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1); 1544 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile50.Unknown", 1);
1454 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1); 1545 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile90.Unknown", 1);
1455 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown", 1546 histogram_tester.ExpectTotalCount("NQE.TransportRTT.Percentile100.Unknown",
1456 1); 1547 1);
1457 1548
1458 // Verify that metrics are logged correctly on main-frame requests. 1549 // Verify that metrics are logged correctly on main-frame requests.
1459 histogram_tester.ExpectTotalCount( 1550 histogram_tester.ExpectTotalCount(
1460 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests); 1551 "NQE.MainFrame.TransportRTT.Percentile50.Unknown", num_requests);
1461 } 1552 }
1462 1553
1554 // Tests if the NQE accuracy metrics are recorded properly.
1555 TEST(NetworkQualityEstimatorTest, RecordAccuracy) {
1556 const int expected_rtt_msec = 100;
1557
1558 const base::TimeDelta accuracy_recording_delays[] = {
1559 base::TimeDelta::FromSeconds(0), base::TimeDelta::FromSeconds(1),
1560 };
1561
1562 const struct {
1563 base::TimeDelta rtt;
1564 base::TimeDelta recent_rtt;
1565 } tests[] = {
1566 {base::TimeDelta::FromMilliseconds(expected_rtt_msec),
1567 base::TimeDelta::FromMilliseconds(expected_rtt_msec)},
1568 {base::TimeDelta::FromMilliseconds(expected_rtt_msec + 1),
1569 base::TimeDelta::FromMilliseconds(expected_rtt_msec)},
1570 {base::TimeDelta::FromMilliseconds(expected_rtt_msec - 1),
1571 base::TimeDelta::FromMilliseconds(expected_rtt_msec)},
1572 };
1573
1574 for (const auto& accuracy_recording_delay : accuracy_recording_delays) {
1575 for (const auto& test : tests) {
1576 std::unique_ptr<base::SimpleTestTickClock> tick_clock(
1577 new base::SimpleTestTickClock());
1578 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get();
1579 tick_clock_ptr->Advance(base::TimeDelta::FromSeconds(1));
1580
1581 std::map<std::string, std::string> variation_params;
1582 TestNetworkQualityEstimator estimator(variation_params);
1583 estimator.SetTickClockForTesting(std::move(tick_clock));
1584
1585 std::vector<base::TimeDelta> accuracy_recording_intervals;
1586 accuracy_recording_intervals.push_back(accuracy_recording_delay);
1587 estimator.SetAccuracyRecordingIntervals(accuracy_recording_intervals);
1588
1589 // RTT is higher than threshold. Network is slow.
1590 // Network was predicted to be slow and actually was slow.
1591 estimator.set_http_rtt(test.rtt);
1592 estimator.set_recent_http_rtt(test.recent_rtt);
1593 estimator.set_transport_rtt(test.rtt);
1594 estimator.set_recent_transport_rtt(test.recent_rtt);
1595
1596 base::HistogramTester histogram_tester;
1597
1598 TestDelegate test_delegate;
1599 TestURLRequestContext context(true);
1600 context.set_network_quality_estimator(&estimator);
1601 context.Init();
1602
1603 // Start a main-frame request which should cause network quality estimator
1604 // to record accuracy UMA.
1605 std::unique_ptr<URLRequest> request(context.CreateRequest(
1606 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
1607 request->SetLoadFlags(request->load_flags() | LOAD_MAIN_FRAME);
1608 request->Start();
1609 base::RunLoop().Run();
1610
1611 if (accuracy_recording_delay != base::TimeDelta()) {
1612 tick_clock_ptr->Advance(accuracy_recording_delay);
1613
1614 // Sleep to ensure that the delayed task is posted.
1615 base::PlatformThread::Sleep(accuracy_recording_delay);
1616 base::RunLoop().RunUntilIdle();
1617 }
1618
1619 const int diff = std::abs(test.rtt.InMilliseconds() -
1620 test.recent_rtt.InMilliseconds());
1621 const std::string diff_value =
1622 base::IntToString(accuracy_recording_delay.InSeconds());
1623
1624 if (test.rtt >= test.recent_rtt) {
1625 histogram_tester.ExpectUniqueSample(
1626 "NQE.Accuracy.HttpRTT.EstimatedActualDiff." + diff_value +
1627 ".60_140",
1628 diff, 1);
1629 histogram_tester.ExpectTotalCount(
1630 "NQE.Accuracy.HttpRTT.ActualEstimatedDiff." + diff_value +
1631 ".60_140",
1632 0);
1633 histogram_tester.ExpectUniqueSample(
1634 "NQE.Accuracy.TransportRTT.EstimatedActualDiff." + diff_value +
1635 ".60_140",
1636 diff, 1);
1637 histogram_tester.ExpectTotalCount(
1638 "NQE.Accuracy.TransportRTT.ActualEstimatedDiff." + diff_value +
1639 ".60_140",
1640 0);
1641 } else {
1642 histogram_tester.ExpectTotalCount(
1643 "NQE.Accuracy.HttpRTT.EstimatedActualDiff." + diff_value +
1644 ".60_140",
1645 0);
1646 histogram_tester.ExpectUniqueSample(
1647 "NQE.Accuracy.HttpRTT.ActualEstimatedDiff." + diff_value +
1648 ".60_140",
1649 diff, 1);
1650 histogram_tester.ExpectTotalCount(
1651 "NQE.Accuracy.TransportRTT.EstimatedActualDiff." + diff_value +
1652 ".60_140",
1653 0);
1654 histogram_tester.ExpectUniqueSample(
1655 "NQE.Accuracy.TransportRTT.ActualEstimatedDiff." + diff_value +
1656 ".60_140",
1657 diff, 1);
1658 }
1659 }
1660 }
1661 }
1662
1463 } // namespace net 1663 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698