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

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

Issue 2107243003: NQE: Record correlation metric in UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Ilya's comments Created 4 years, 5 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>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), 70 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
71 recent_effective_connection_type_set_(false), 71 recent_effective_connection_type_set_(false),
72 recent_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), 72 recent_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
73 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN), 73 current_network_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN),
74 accuracy_recording_intervals_set_(false), 74 accuracy_recording_intervals_set_(false),
75 http_rtt_set_(false), 75 http_rtt_set_(false),
76 recent_http_rtt_set_(false), 76 recent_http_rtt_set_(false),
77 transport_rtt_set_(false), 77 transport_rtt_set_(false),
78 recent_transport_rtt_set_(false), 78 recent_transport_rtt_set_(false),
79 downlink_throughput_kbps_set_(false), 79 downlink_throughput_kbps_set_(false),
80 recent_downlink_throughput_kbps_set_(false) { 80 recent_downlink_throughput_kbps_set_(false),
81 rand_double_(0.0) {
81 // Set up embedded test server. 82 // Set up embedded test server.
82 embedded_test_server_.ServeFilesFromDirectory( 83 embedded_test_server_.ServeFilesFromDirectory(
83 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 84 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
84 EXPECT_TRUE(embedded_test_server_.Start()); 85 EXPECT_TRUE(embedded_test_server_.Start());
85 embedded_test_server_.RegisterRequestHandler(base::Bind( 86 embedded_test_server_.RegisterRequestHandler(base::Bind(
86 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this))); 87 &TestNetworkQualityEstimator::HandleRequest, base::Unretained(this)));
87 } 88 }
88 89
89 explicit TestNetworkQualityEstimator( 90 explicit TestNetworkQualityEstimator(
90 const std::map<std::string, std::string>& variation_params) 91 const std::map<std::string, std::string>& variation_params)
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 257 }
257 258
258 const std::vector<base::TimeDelta>& GetAccuracyRecordingIntervals() 259 const std::vector<base::TimeDelta>& GetAccuracyRecordingIntervals()
259 const override { 260 const override {
260 if (accuracy_recording_intervals_set_) 261 if (accuracy_recording_intervals_set_)
261 return accuracy_recording_intervals_; 262 return accuracy_recording_intervals_;
262 263
263 return NetworkQualityEstimator::GetAccuracyRecordingIntervals(); 264 return NetworkQualityEstimator::GetAccuracyRecordingIntervals();
264 } 265 }
265 266
267 void set_rand_double(double rand_double) { rand_double_ = rand_double; }
268
269 double RandDouble() const override { return rand_double_; }
270
266 using NetworkQualityEstimator::SetTickClockForTesting; 271 using NetworkQualityEstimator::SetTickClockForTesting;
267 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate; 272 using NetworkQualityEstimator::ReadCachedNetworkQualityEstimate;
268 using NetworkQualityEstimator::OnConnectionTypeChanged; 273 using NetworkQualityEstimator::OnConnectionTypeChanged;
269 274
270 private: 275 private:
271 // NetworkQualityEstimator implementation that returns the overridden 276 // NetworkQualityEstimator implementation that returns the overridden
272 // network 277 // network
273 // id (instead of invoking platform APIs). 278 // id (instead of invoking platform APIs).
274 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override { 279 NetworkQualityEstimator::NetworkID GetCurrentNetworkID() const override {
275 return NetworkQualityEstimator::NetworkID(current_network_type_, 280 return NetworkQualityEstimator::NetworkID(current_network_type_,
(...skipping 23 matching lines...) Expand all
299 304
300 bool recent_transport_rtt_set_; 305 bool recent_transport_rtt_set_;
301 base::TimeDelta recent_transport_rtt_; 306 base::TimeDelta recent_transport_rtt_;
302 307
303 bool downlink_throughput_kbps_set_; 308 bool downlink_throughput_kbps_set_;
304 int32_t downlink_throughput_kbps_; 309 int32_t downlink_throughput_kbps_;
305 310
306 bool recent_downlink_throughput_kbps_set_; 311 bool recent_downlink_throughput_kbps_set_;
307 int32_t recent_downlink_throughput_kbps_; 312 int32_t recent_downlink_throughput_kbps_;
308 313
314 double rand_double_;
315
309 // Embedded server used for testing. 316 // Embedded server used for testing.
310 EmbeddedTestServer embedded_test_server_; 317 EmbeddedTestServer embedded_test_server_;
311 318
312 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 319 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
313 }; 320 };
314 321
315 class TestEffectiveConnectionTypeObserver 322 class TestEffectiveConnectionTypeObserver
316 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver { 323 : public NetworkQualityEstimator::EffectiveConnectionTypeObserver {
317 public: 324 public:
318 std::vector<NetworkQualityEstimator::EffectiveConnectionType>& 325 std::vector<NetworkQualityEstimator::EffectiveConnectionType>&
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1942 std::string connection_type_name = 1949 std::string connection_type_name =
1943 std::string(NetworkQualityEstimator::GetNameForEffectiveConnectionType( 1950 std::string(NetworkQualityEstimator::GetNameForEffectiveConnectionType(
1944 effective_connection_type)); 1951 effective_connection_type));
1945 EXPECT_FALSE(connection_type_name.empty()); 1952 EXPECT_FALSE(connection_type_name.empty());
1946 EXPECT_EQ(effective_connection_type, 1953 EXPECT_EQ(effective_connection_type,
1947 NetworkQualityEstimator::GetEffectiveConnectionTypeForName( 1954 NetworkQualityEstimator::GetEffectiveConnectionTypeForName(
1948 connection_type_name)); 1955 connection_type_name));
1949 } 1956 }
1950 } 1957 }
1951 1958
1959 // Tests that the correlation histogram is recorded correctly based on
1960 // correlation logging probability set in the variation params.
1961 TEST(NetworkQualityEstimatorTest, CorrelationHistogram) {
1962 // Match the values set in network_quality_estimator.cc.
1963 static const int32_t kTrimBits = 5;
1964 static const int32_t kBitsPerMetric = 7;
1965
1966 const struct {
1967 bool use_transport_rtt;
1968 double rand_double;
1969 double correlation_logging_probability;
1970 base::TimeDelta transport_rtt;
1971 int32_t expected_transport_rtt_milliseconds;
1972 base::TimeDelta http_rtt;
1973 int32_t expected_http_rtt_milliseconds;
1974 int32_t downstream_throughput_kbps;
1975 int32_t expected_downstream_throughput_kbps;
1976
1977 } tests[] = {
1978 {
1979 // Verify that the metric is not recorded if the logging probability
1980 // is set to 0.0.
1981 false, 0.5, 0.0, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits,
1982 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000,
1983 3000 >> kTrimBits,
1984 },
1985 {
1986 // Verify that the metric is not recorded if the logging probability
1987 // is lower than the value returned by the random number generator.
1988 false, 0.3, 0.1, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits,
1989 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000,
1990 3000 >> kTrimBits,
1991 },
1992 {
1993 // Verify that the metric is recorded if the logging probability is
1994 // higher than the value returned by the random number generator.
1995 false, 0.3, 0.4, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits,
1996 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000,
1997 3000 >> kTrimBits,
1998 },
1999 {
2000 // Verify that the metric is recorded if the logging probability is
2001 // set to 1.0.
2002 false, 0.5, 1.0, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits,
2003 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000,
2004 3000 >> kTrimBits,
2005 },
2006 {
2007 // Verify that the metric is recorded if the logging probability is
2008 // set to 1.0.
2009 true, 0.5, 1.0, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits,
2010 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000,
2011 3000 >> kTrimBits,
2012 },
2013 {
2014 // Verify that if the metric is larger than
2015 // 2^(kBitsPerMetric + kTrimBits), it is rounded down to
2016 // (2^(kBitsPerMetric + kTrimBits) - 1) >> kTrimBits.
2017 false, 0.5, 1.0, base::TimeDelta::FromSeconds(10), 4095 >> kTrimBits,
2018 base::TimeDelta::FromSeconds(20), 4095 >> kTrimBits, 30000,
2019 4095 >> kTrimBits,
2020 },
2021 };
2022
2023 for (const auto& test : tests) {
2024 base::HistogramTester histogram_tester;
2025
2026 std::map<std::string, std::string> variation_params;
2027 variation_params["correlation_logging_probability"] =
2028 base::DoubleToString(test.correlation_logging_probability);
2029 if (test.use_transport_rtt) {
2030 variation_params["effective_connection_type_algorithm"] =
2031 "TransportRTTOrDownstreamThroughput";
2032 }
2033 TestNetworkQualityEstimator estimator(variation_params);
2034
2035 estimator.set_transport_rtt(test.transport_rtt);
2036 estimator.set_recent_transport_rtt(test.transport_rtt);
2037 estimator.set_http_rtt(test.http_rtt);
2038 estimator.set_recent_http_rtt(test.http_rtt);
2039 estimator.set_downlink_throughput_kbps(test.downstream_throughput_kbps);
2040 estimator.set_rand_double(test.rand_double);
2041
2042 TestDelegate test_delegate;
2043 TestURLRequestContext context(true);
2044 context.set_network_quality_estimator(&estimator);
2045 context.Init();
2046
2047 // Start a main-frame request that should cause network quality estimator to
2048 // record the network quality at the last main frame request.
2049 std::unique_ptr<URLRequest> request_1(context.CreateRequest(
2050 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
2051 request_1->SetLoadFlags(request_1->load_flags() | LOAD_MAIN_FRAME);
2052 request_1->Start();
2053 base::RunLoop().Run();
2054 histogram_tester.ExpectTotalCount(
2055 "NQE.Correlation.ResourceLoadTime.0Kb_128Kb", 0);
2056
2057 // Start another main-frame request which should cause network quality
2058 // estimator to record the correlation UMA.
2059 std::unique_ptr<URLRequest> request_2(context.CreateRequest(
2060 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate));
2061 request_2->Start();
2062 base::RunLoop().Run();
2063
2064 if (test.rand_double >= test.correlation_logging_probability) {
2065 histogram_tester.ExpectTotalCount(
2066 "NQE.Correlation.ResourceLoadTime.0Kb_128Kb", 0);
2067 continue;
2068 }
2069 histogram_tester.ExpectTotalCount(
2070 "NQE.Correlation.ResourceLoadTime.0Kb_128Kb", 1);
2071 std::vector<base::Bucket> buckets = histogram_tester.GetAllSamples(
2072 "NQE.Correlation.ResourceLoadTime.0Kb_128Kb");
2073 // Get the bits at index 0-10 which contain the RTT.
2074 // 128 is 2^kBitsPerMetric.
2075 if (test.use_transport_rtt) {
2076 EXPECT_EQ(test.expected_transport_rtt_milliseconds,
2077 buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric >>
2078 kBitsPerMetric);
2079 } else {
2080 EXPECT_EQ(test.expected_http_rtt_milliseconds,
2081 buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric >>
2082 kBitsPerMetric);
2083 }
2084
2085 // Get the bits at index 11-17 which contain the downstream throughput.
2086 EXPECT_EQ(test.expected_downstream_throughput_kbps,
2087 (buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric) % 128);
2088
2089 // Get the bits at index 18-24 which contain the resource fetch time.
2090 EXPECT_LE(0, (buckets.at(0).min >> kBitsPerMetric) % 128);
2091
2092 // Get the bits at index 25-31 which contain the resource load size.
2093 EXPECT_LE(0, (buckets.at(0).min) % 128);
2094 }
2095 }
2096
1952 } // namespace net 2097 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698