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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 Loading... |
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 Loading... |
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 1486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1805 std::string connection_type_name = | 1812 std::string connection_type_name = |
1806 std::string(NetworkQualityEstimator::GetNameForEffectiveConnectionType( | 1813 std::string(NetworkQualityEstimator::GetNameForEffectiveConnectionType( |
1807 effective_connection_type)); | 1814 effective_connection_type)); |
1808 EXPECT_FALSE(connection_type_name.empty()); | 1815 EXPECT_FALSE(connection_type_name.empty()); |
1809 EXPECT_EQ(effective_connection_type, | 1816 EXPECT_EQ(effective_connection_type, |
1810 NetworkQualityEstimator::GetEffectiveConnectionTypeForName( | 1817 NetworkQualityEstimator::GetEffectiveConnectionTypeForName( |
1811 connection_type_name)); | 1818 connection_type_name)); |
1812 } | 1819 } |
1813 } | 1820 } |
1814 | 1821 |
| 1822 // Tests that the correlation histogram is recorded correctly based on |
| 1823 // correlation logging probability set in the variation params. |
| 1824 TEST(NetworkQualityEstimatorTest, CorrelationHistogram) { |
| 1825 // Match the values set in network_quality_estimator.cc. |
| 1826 static const int32_t kTrimBits = 5; |
| 1827 static const int32_t kBitsPerMetric = 7; |
| 1828 |
| 1829 const struct { |
| 1830 double rand_double; |
| 1831 double correlation_logging_probability; |
| 1832 base::TimeDelta transport_rtt; |
| 1833 int32_t expected_transport_rtt_milliseconds; |
| 1834 base::TimeDelta http_rtt; |
| 1835 int32_t expected_http_rtt_milliseconds; |
| 1836 int32_t downstream_throughput_kbps; |
| 1837 int32_t expected_downstream_throughput_kbps; |
| 1838 |
| 1839 } tests[] = { |
| 1840 { |
| 1841 // Verify that the metric is not recorded if the logging probability |
| 1842 // is set to 0.0. |
| 1843 0.5, 0.0, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits, |
| 1844 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000, |
| 1845 3000 >> kTrimBits, |
| 1846 }, |
| 1847 { |
| 1848 // Verify that the metric is not recorded if the logging probability |
| 1849 // is lower than the value returned by the random number generator. |
| 1850 0.3, 0.1, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits, |
| 1851 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000, |
| 1852 3000 >> kTrimBits, |
| 1853 }, |
| 1854 { |
| 1855 // Verify that the metric is recorded if the logging probability is |
| 1856 // higher than the value returned by the random number generator. |
| 1857 0.3, 0.4, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits, |
| 1858 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000, |
| 1859 3000 >> kTrimBits, |
| 1860 }, |
| 1861 { |
| 1862 // Verify that the metric is recorded if the logging probability is |
| 1863 // set to 1.0. |
| 1864 0.5, 1.0, base::TimeDelta::FromSeconds(1), 1000 >> kTrimBits, |
| 1865 base::TimeDelta::FromSeconds(2), 2000 >> kTrimBits, 3000, |
| 1866 3000 >> kTrimBits, |
| 1867 }, |
| 1868 { |
| 1869 // Verify that if the metric is larger than |
| 1870 // 2^(kBitsPerMetric + kTrimBits), it is rounded down to |
| 1871 // (2^(kBitsPerMetric + kTrimBits) - 1) >> kTrimBits. |
| 1872 0.5, 1.0, base::TimeDelta::FromSeconds(10), 4095 >> kTrimBits, |
| 1873 base::TimeDelta::FromSeconds(20), 4095 >> kTrimBits, 30000, |
| 1874 4095 >> kTrimBits, |
| 1875 }, |
| 1876 }; |
| 1877 |
| 1878 for (const auto& test : tests) { |
| 1879 base::HistogramTester histogram_tester; |
| 1880 |
| 1881 std::map<std::string, std::string> variation_params; |
| 1882 variation_params["correlation_logging_probability"] = |
| 1883 base::DoubleToString(test.correlation_logging_probability); |
| 1884 TestNetworkQualityEstimator estimator(variation_params); |
| 1885 |
| 1886 estimator.set_transport_rtt(test.transport_rtt); |
| 1887 estimator.set_recent_transport_rtt(test.transport_rtt); |
| 1888 estimator.set_http_rtt(test.http_rtt); |
| 1889 estimator.set_recent_http_rtt(test.http_rtt); |
| 1890 estimator.set_downlink_throughput_kbps(test.downstream_throughput_kbps); |
| 1891 estimator.set_rand_double(test.rand_double); |
| 1892 |
| 1893 TestDelegate test_delegate; |
| 1894 TestURLRequestContext context(true); |
| 1895 context.set_network_quality_estimator(&estimator); |
| 1896 context.Init(); |
| 1897 |
| 1898 // Start a main-frame request that should cause network quality estimator to |
| 1899 // record the network quality at the last main frame request. |
| 1900 std::unique_ptr<URLRequest> request_1(context.CreateRequest( |
| 1901 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1902 request_1->SetLoadFlags(request_1->load_flags() | LOAD_MAIN_FRAME); |
| 1903 request_1->Start(); |
| 1904 base::RunLoop().Run(); |
| 1905 histogram_tester.ExpectTotalCount("NQE.Correlation.ResourceLoadTime", 0); |
| 1906 |
| 1907 // Start another main-frame request which should cause network quality |
| 1908 // estimator to record the correlation UMA. |
| 1909 std::unique_ptr<URLRequest> request_2(context.CreateRequest( |
| 1910 estimator.GetEchoURL(), DEFAULT_PRIORITY, &test_delegate)); |
| 1911 request_2->Start(); |
| 1912 base::RunLoop().Run(); |
| 1913 |
| 1914 if (test.rand_double >= test.correlation_logging_probability) { |
| 1915 histogram_tester.ExpectTotalCount("NQE.Correlation.ResourceLoadTime", 0); |
| 1916 continue; |
| 1917 } |
| 1918 histogram_tester.ExpectTotalCount("NQE.Correlation.ResourceLoadTime", 1); |
| 1919 std::vector<base::Bucket> buckets = |
| 1920 histogram_tester.GetAllSamples("NQE.Correlation.ResourceLoadTime"); |
| 1921 // Get the bits at index 0-10 which contain the transport RTT. |
| 1922 // 128 is 2^kBitsPerMetric. |
| 1923 EXPECT_EQ(test.expected_transport_rtt_milliseconds, |
| 1924 buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric >> |
| 1925 kBitsPerMetric); |
| 1926 // Get the bits at index 11-17 which contain the HTTP RTT. |
| 1927 EXPECT_EQ(test.expected_http_rtt_milliseconds, |
| 1928 (buckets.at(0).min >> kBitsPerMetric >> kBitsPerMetric) % 128); |
| 1929 // Get the bits at index 18-24 which contain the downstream throughput. |
| 1930 EXPECT_EQ(test.expected_downstream_throughput_kbps, |
| 1931 (buckets.at(0).min >> kBitsPerMetric) % 128); |
| 1932 // Get the bits at index 25-31 which contain the resource fetch time. |
| 1933 EXPECT_LE(0, buckets.at(0).min % 128); |
| 1934 } |
| 1935 } |
| 1936 |
1815 } // namespace net | 1937 } // namespace net |
OLD | NEW |