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

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

Issue 1942893002: Split NQE to multiple files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/nqe/network_quality_observation.h"
6
7 #include <stddef.h>
8
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/time/time.h"
14 #include "net/nqe/network_quality_observation_source.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace net {
18
19 namespace nqe {
20
21 namespace {
22
23 // Verifies that the percentiles are correctly computed. All observations have
24 // the same timestamp.
25 TEST(NetworkQualityObservationTest, PercentileSameTimestamps) {
26 internal::ObservationBuffer<int32_t> int_buffer(0.5);
27 internal::ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
28 ASSERT_EQ(0u, int_buffer.Size());
29 ASSERT_LT(0u, int_buffer.Capacity());
30 ASSERT_EQ(0u, time_delta_buffer.Size());
31 ASSERT_LT(0u, time_delta_buffer.Capacity());
32
33 const base::TimeTicks now = base::TimeTicks::Now();
34
35 int32_t result;
36 base::TimeDelta time_delta_result;
37
38 // Percentiles should be unavailable when no observations are available.
39 EXPECT_FALSE(
40 int_buffer.GetPercentile(base::TimeTicks(), &result, 50,
41 std::vector<NetworkQualityObservationSource>()));
42 EXPECT_FALSE(time_delta_buffer.GetPercentile(
43 base::TimeTicks(), &time_delta_result, 50,
44 std::vector<NetworkQualityObservationSource>()));
45
46 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even
47 // samples. This helps in verifying that the order of samples does not matter.
48 for (int i = 1; i <= 99; i += 2) {
49 int_buffer.AddObservation(internal::Observation<int32_t>(
50 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
51 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
52 base::TimeDelta::FromMilliseconds(i), now,
53 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
54 EXPECT_TRUE(int_buffer.GetPercentile(
55 base::TimeTicks(), &result, 50,
56 std::vector<NetworkQualityObservationSource>()));
57 ASSERT_EQ(static_cast<size_t>(i / 2 + 1), int_buffer.Size());
58 EXPECT_TRUE(time_delta_buffer.GetPercentile(
59 base::TimeTicks(), &time_delta_result, 50,
60 std::vector<NetworkQualityObservationSource>()));
61 ASSERT_EQ(static_cast<size_t>(i / 2 + 1), time_delta_buffer.Size());
62 }
63
64 for (int i = 2; i <= 100; i += 2) {
65 int_buffer.AddObservation(internal::Observation<int32_t>(
66 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
67 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
68 base::TimeDelta::FromMilliseconds(i), now,
69 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
70 EXPECT_TRUE(int_buffer.GetPercentile(
71 base::TimeTicks(), &result, 50,
72 std::vector<NetworkQualityObservationSource>()));
73 ASSERT_EQ(static_cast<size_t>(i / 2 + 50), int_buffer.Size());
74 EXPECT_TRUE(time_delta_buffer.GetPercentile(
75 base::TimeTicks(), &time_delta_result, 50,
76 std::vector<NetworkQualityObservationSource>()));
77 ASSERT_EQ(static_cast<size_t>(i / 2 + 50), time_delta_buffer.Size());
78 }
79
80 ASSERT_EQ(100u, int_buffer.Size());
81 ASSERT_EQ(100u, time_delta_buffer.Size());
82
83 for (int i = 0; i <= 100; ++i) {
84 // Checks if the difference between the two integers is less than 1. This is
85 // required because computed percentiles may be slightly different from
86 // what is expected due to floating point computation errors and integer
87 // rounding off errors.
88 EXPECT_TRUE(int_buffer.GetPercentile(
89 base::TimeTicks(), &result, i,
90 std::vector<NetworkQualityObservationSource>()));
91 EXPECT_TRUE(time_delta_buffer.GetPercentile(
92 base::TimeTicks(), &time_delta_result, i,
93 std::vector<NetworkQualityObservationSource>()));
94 EXPECT_NEAR(result, i, 1);
95 EXPECT_NEAR(time_delta_result.InMilliseconds(), i, 1);
96 }
97
98 EXPECT_FALSE(int_buffer.GetPercentile(
99 now + base::TimeDelta::FromSeconds(1), &result, 50,
100 std::vector<NetworkQualityObservationSource>()));
101 EXPECT_FALSE(time_delta_buffer.GetPercentile(
102 now + base::TimeDelta::FromSeconds(1), &time_delta_result, 50,
103 std::vector<NetworkQualityObservationSource>()));
104
105 // Percentiles should be unavailable when no observations are available.
106 int_buffer.Clear();
107 time_delta_buffer.Clear();
108 EXPECT_FALSE(
109 int_buffer.GetPercentile(base::TimeTicks(), &result, 50,
110 std::vector<NetworkQualityObservationSource>()));
111 EXPECT_FALSE(time_delta_buffer.GetPercentile(
112 base::TimeTicks(), &time_delta_result, 50,
113 std::vector<NetworkQualityObservationSource>()));
114 }
115
116 // Verifies that the percentiles are correctly computed. Observations have
117 // different timestamps with half the observations being very old and the rest
118 // of them being very recent. Percentiles should factor in recent observations
119 // much more heavily than older samples.
120 TEST(NetworkQualityObservationTest, PercentileDifferentTimestamps) {
121 internal::ObservationBuffer<int32_t> int_buffer(0.5);
122 internal::ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
123 const base::TimeTicks now = base::TimeTicks::Now();
124 const base::TimeTicks very_old = now - base::TimeDelta::FromDays(365);
125
126 int32_t result;
127 base::TimeDelta time_delta_result;
128
129 // Network quality should be unavailable when no observations are available.
130 EXPECT_FALSE(
131 int_buffer.GetPercentile(base::TimeTicks(), &result, 50,
132 std::vector<NetworkQualityObservationSource>()));
133 EXPECT_FALSE(time_delta_buffer.GetPercentile(
134 base::TimeTicks(), &time_delta_result, 50,
135 std::vector<NetworkQualityObservationSource>()));
136
137 // First 50 samples have very old timestamp.
138 for (int i = 1; i <= 50; ++i) {
139 int_buffer.AddObservation(internal::Observation<int32_t>(
140 i, very_old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
141 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
142 base::TimeDelta::FromMilliseconds(i), very_old,
143 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
144 }
145
146 // Next 50 (i.e., from 51 to 100) have recent timestamp.
147 for (int i = 51; i <= 100; ++i) {
148 int_buffer.AddObservation(internal::Observation<int32_t>(
149 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
150 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
151 base::TimeDelta::FromMilliseconds(i), now,
152 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
153 }
154
155 // Older samples have very little weight. So, all percentiles are >= 51
156 // (lowest value among recent observations).
157 for (int i = 1; i < 100; ++i) {
158 // Checks if the difference between the two integers is less than 1. This is
159 // required because computed percentiles may be slightly different from
160 // what is expected due to floating point computation errors and integer
161 // rounding off errors.
162 EXPECT_TRUE(int_buffer.GetPercentile(
163 base::TimeTicks(), &result, i,
164 std::vector<NetworkQualityObservationSource>()));
165 EXPECT_NEAR(result, 51 + 0.49 * i, 1);
166
167 EXPECT_TRUE(time_delta_buffer.GetPercentile(
168 base::TimeTicks(), &time_delta_result, i,
169 std::vector<NetworkQualityObservationSource>()));
170 EXPECT_NEAR(time_delta_result.InMilliseconds(), 51 + 0.49 * i, 1);
171 }
172
173 EXPECT_FALSE(int_buffer.GetPercentile(
174 now + base::TimeDelta::FromSeconds(1), &result, 50,
175 std::vector<NetworkQualityObservationSource>()));
176 EXPECT_FALSE(time_delta_buffer.GetPercentile(
177 now + base::TimeDelta::FromSeconds(1), &time_delta_result, 50,
178 std::vector<NetworkQualityObservationSource>()));
179 }
180
181 // Verifies that the percentiles are correctly computed when some of the
182 // observation sources are disallowed. All observations have the same timestamp.
183 TEST(NetworkQualityObservationTest, DisallowedObservationSources) {
184 internal::ObservationBuffer<int32_t> int_buffer(0.5);
185 internal::ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
186 const base::TimeTicks now = base::TimeTicks::Now();
187
188 int32_t result;
189 base::TimeDelta time_delta_result;
190
191 // Network quality should be unavailable when no observations are available.
192 EXPECT_FALSE(
193 int_buffer.GetPercentile(base::TimeTicks(), &result, 50,
194 std::vector<NetworkQualityObservationSource>()));
195 EXPECT_FALSE(time_delta_buffer.GetPercentile(
196 base::TimeTicks(), &time_delta_result, 50,
197 std::vector<NetworkQualityObservationSource>()));
198
199 // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even
200 // samples. This helps in verifying that the order of samples does not matter.
201 for (int i = 1; i <= 99; i += 2) {
202 int_buffer.AddObservation(internal::Observation<int32_t>(
203 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
204 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
205 base::TimeDelta::FromMilliseconds(i), now,
206 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
207 }
208
209 // Add samples for TCP and QUIC observations which should not be taken into
210 // account when computing the percentile.
211 for (int i = 1; i <= 99; i += 2) {
212 int_buffer.AddObservation(internal::Observation<int32_t>(
213 10000, now, NETWORK_QUALITY_OBSERVATION_SOURCE_TCP));
214 int_buffer.AddObservation(internal::Observation<int32_t>(
215 10000, now, NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC));
216 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
217 base::TimeDelta::FromMilliseconds(10000), now,
218 NETWORK_QUALITY_OBSERVATION_SOURCE_TCP));
219 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
220 base::TimeDelta::FromMilliseconds(10000), now,
221 NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC));
222 }
223
224 for (int i = 2; i <= 100; i += 2) {
225 int_buffer.AddObservation(internal::Observation<int32_t>(
226 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
227 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
228 base::TimeDelta::FromMilliseconds(i), now,
229 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
230 }
231
232 std::vector<NetworkQualityObservationSource> disallowed_observation_sources;
233 disallowed_observation_sources.push_back(
234 NETWORK_QUALITY_OBSERVATION_SOURCE_TCP);
235 disallowed_observation_sources.push_back(
236 NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC);
237
238 for (int i = 0; i <= 100; ++i) {
239 // Checks if the difference between the two integers is less than 1. This is
240 // required because computed percentiles may be slightly different from
241 // what is expected due to floating point computation errors and integer
242 // rounding off errors.
243 EXPECT_TRUE(int_buffer.GetPercentile(base::TimeTicks(), &result, i,
244 disallowed_observation_sources));
245 EXPECT_NEAR(result, i, 1);
246 EXPECT_TRUE(
247 time_delta_buffer.GetPercentile(base::TimeTicks(), &time_delta_result,
248 i, disallowed_observation_sources));
249 EXPECT_NEAR(time_delta_result.InMilliseconds(), i, 1);
250 }
251
252 // Now check the percentile value for TCP and QUIC observations.
253 disallowed_observation_sources.clear();
254 disallowed_observation_sources.push_back(
255 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST);
256 for (int i = 0; i <= 100; ++i) {
257 // Checks if the difference between the two integers is less than 1. This is
258 // required because computed percentiles may be slightly different from
259 // what is expected due to floating point computation errors and integer
260 // rounding off errors.
261 EXPECT_TRUE(int_buffer.GetPercentile(base::TimeTicks(), &result, i,
262 disallowed_observation_sources));
263 EXPECT_NEAR(result, 10000, 1);
264 EXPECT_TRUE(
265 time_delta_buffer.GetPercentile(base::TimeTicks(), &time_delta_result,
266 i, disallowed_observation_sources));
267 EXPECT_NEAR(time_delta_result.InMilliseconds(), 10000, 1);
268 }
269 }
270
271 TEST(NetworkQualityObservationTest, TestGetMedianRTTSince) {
272 internal::ObservationBuffer<int32_t> int_buffer(0.5);
273 internal::ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
274 base::TimeTicks now = base::TimeTicks::Now();
275 base::TimeTicks old = now - base::TimeDelta::FromMilliseconds(1);
276 ASSERT_NE(old, now);
277
278 // First sample has very old timestamp.
279 int_buffer.AddObservation(internal::Observation<int32_t>(
280 1, old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
281 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
282 base::TimeDelta::FromMilliseconds(1), old,
283 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
284
285 int_buffer.AddObservation(internal::Observation<int32_t>(
286 100, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
287 time_delta_buffer.AddObservation(internal::Observation<base::TimeDelta>(
288 base::TimeDelta::FromMilliseconds(100), now,
289 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
290
291 const struct {
292 base::TimeTicks start_timestamp;
293 bool expect_network_quality_available;
294 base::TimeDelta expected_url_request_rtt;
295 int32_t expected_downstream_throughput;
296 } tests[] = {
297 {now + base::TimeDelta::FromSeconds(10), false,
298 base::TimeDelta::FromMilliseconds(0), 0},
299 {now, true, base::TimeDelta::FromMilliseconds(100), 100},
300 {now - base::TimeDelta::FromMicroseconds(500), true,
301 base::TimeDelta::FromMilliseconds(100), 100},
302
303 };
304
305 for (const auto& test : tests) {
306 base::TimeDelta url_request_rtt;
307 int32_t downstream_throughput_kbps;
308 std::vector<NetworkQualityObservationSource> disallowed_observation_sources;
309
310 EXPECT_EQ(
311 test.expect_network_quality_available,
312 time_delta_buffer.GetPercentile(test.start_timestamp, &url_request_rtt,
313 50, disallowed_observation_sources));
314 EXPECT_EQ(test.expect_network_quality_available,
315 int_buffer.GetPercentile(test.start_timestamp,
316 &downstream_throughput_kbps, 50,
317 disallowed_observation_sources));
318
319 if (test.expect_network_quality_available) {
320 EXPECT_EQ(test.expected_url_request_rtt, url_request_rtt);
321 EXPECT_EQ(test.expected_downstream_throughput,
322 downstream_throughput_kbps);
323 }
324 }
325 }
326
327 } // namespace
328
329 } // namespace nqe
330
331 } // namespace net
OLDNEW
« net/nqe/network_quality_observation_source.h ('K') | « net/nqe/network_quality_observation_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698