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

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

Issue 2411833003: NQE: Add more statistics to ObservationBuffer class (Closed)
Patch Set: Addressed kundaji's comments Created 4 years, 2 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
« net/nqe/observation_buffer.h ('K') | « net/nqe/observation_buffer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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/observation_buffer.h" 5 #include "net/nqe/observation_buffer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 } 178 }
179 179
180 // Verifies that the percentiles are correctly computed. Observations have 180 // Verifies that the percentiles are correctly computed. Observations have
181 // different timestamps with half the observations being very old and the rest 181 // different timestamps with half the observations being very old and the rest
182 // of them being very recent. Percentiles should factor in recent observations 182 // of them being very recent. Percentiles should factor in recent observations
183 // much more heavily than older samples. 183 // much more heavily than older samples.
184 TEST(NetworkQualityObservationBufferTest, PercentileDifferentTimestamps) { 184 TEST(NetworkQualityObservationBufferTest, PercentileDifferentTimestamps) {
185 ObservationBuffer<int32_t> int_buffer(0.5); 185 ObservationBuffer<int32_t> int_buffer(0.5);
186 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); 186 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
187 const base::TimeTicks now = base::TimeTicks::Now(); 187 const base::TimeTicks now = base::TimeTicks::Now();
188 const base::TimeTicks very_old = now - base::TimeDelta::FromDays(365); 188 const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7);
189 189
190 int32_t result; 190 int32_t result;
191 base::TimeDelta time_delta_result; 191 base::TimeDelta time_delta_result;
192 192
193 // Network quality should be unavailable when no observations are available. 193 // Network quality should be unavailable when no observations are available.
194 EXPECT_FALSE( 194 EXPECT_FALSE(
195 int_buffer.GetPercentile(base::TimeTicks(), &result, 50, 195 int_buffer.GetPercentile(base::TimeTicks(), &result, 50,
196 std::vector<NetworkQualityObservationSource>())); 196 std::vector<NetworkQualityObservationSource>()));
197 EXPECT_FALSE(time_delta_buffer.GetPercentile( 197 EXPECT_FALSE(time_delta_buffer.GetPercentile(
198 base::TimeTicks(), &time_delta_result, 50, 198 base::TimeTicks(), &time_delta_result, 50,
(...skipping 18 matching lines...) Expand all
217 } 217 }
218 218
219 // Older samples have very little weight. So, all percentiles are >= 51 219 // Older samples have very little weight. So, all percentiles are >= 51
220 // (lowest value among recent observations). 220 // (lowest value among recent observations).
221 for (int i = 1; i < 100; ++i) { 221 for (int i = 1; i < 100; ++i) {
222 // Checks if the difference between the two integers is less than 1. This is 222 // Checks if the difference between the two integers is less than 1. This is
223 // required because computed percentiles may be slightly different from 223 // required because computed percentiles may be slightly different from
224 // what is expected due to floating point computation errors and integer 224 // what is expected due to floating point computation errors and integer
225 // rounding off errors. 225 // rounding off errors.
226 EXPECT_TRUE(int_buffer.GetPercentile( 226 EXPECT_TRUE(int_buffer.GetPercentile(
227 base::TimeTicks(), &result, i, 227 very_old, &result, i, std::vector<NetworkQualityObservationSource>()));
228 std::vector<NetworkQualityObservationSource>()));
229 EXPECT_NEAR(result, 51 + 0.49 * i, 1); 228 EXPECT_NEAR(result, 51 + 0.49 * i, 1);
230 229
231 EXPECT_TRUE(time_delta_buffer.GetPercentile( 230 EXPECT_TRUE(time_delta_buffer.GetPercentile(
232 base::TimeTicks(), &time_delta_result, i, 231 very_old, &time_delta_result, i,
233 std::vector<NetworkQualityObservationSource>())); 232 std::vector<NetworkQualityObservationSource>()));
234 EXPECT_NEAR(time_delta_result.InMilliseconds(), 51 + 0.49 * i, 1); 233 EXPECT_NEAR(time_delta_result.InMilliseconds(), 51 + 0.49 * i, 1);
235 } 234 }
236 235
237 EXPECT_FALSE(int_buffer.GetPercentile( 236 EXPECT_FALSE(int_buffer.GetPercentile(
238 now + base::TimeDelta::FromSeconds(1), &result, 50, 237 now + base::TimeDelta::FromSeconds(1), &result, 50,
239 std::vector<NetworkQualityObservationSource>())); 238 std::vector<NetworkQualityObservationSource>()));
240 EXPECT_FALSE(time_delta_buffer.GetPercentile( 239 EXPECT_FALSE(time_delta_buffer.GetPercentile(
241 now + base::TimeDelta::FromSeconds(1), &time_delta_result, 50, 240 now + base::TimeDelta::FromSeconds(1), &time_delta_result, 50,
242 std::vector<NetworkQualityObservationSource>())); 241 std::vector<NetworkQualityObservationSource>()));
243 } 242 }
244 243
244 #if !defined(OS_WIN)
245 // Disabled on OS_WIN since the GetUnweightedAverage() and
246 // GetUnweightedAverage() functions are not yet called outside tests, and so the
247 // compiler on Windows does not generate the object code for these functions.
248 // TODO(tbansal): Enable these tests on Windows once these functions are called
bengr 2016/10/14 21:15:59 File a bug for this and reference it here.
tbansal1 2016/10/14 22:25:30 Done.
249 // outside the tests.
250 TEST(NetworkQualityObservationBufferTest,
251 UnweightedAverageDifferentTimestamps) {
252 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
253 ObservationBuffer<int32_t> int_buffer(0.5);
254 const base::TimeTicks now = base::TimeTicks::Now();
255 const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7);
256
257 base::TimeDelta time_delta_result;
258 int32_t int_result;
259
260 // Network quality should be unavailable when no observations are available.
261 EXPECT_FALSE(time_delta_buffer.GetUnweightedAverage(
262 base::TimeTicks(), std::vector<NetworkQualityObservationSource>(),
263 &time_delta_result));
264 EXPECT_FALSE(int_buffer.GetUnweightedAverage(
265 base::TimeTicks(), std::vector<NetworkQualityObservationSource>(),
266 &int_result));
267
268 // First 50 samples have very old timestamp.
bengr 2016/10/14 21:15:59 Turn these all into complete sentences. E.g.: //
tbansal1 2016/10/14 22:25:30 Done.
269 for (int i = 1; i <= 50; ++i) {
270 time_delta_buffer.AddObservation(Observation<base::TimeDelta>(
271 base::TimeDelta::FromMilliseconds(i), very_old,
272 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
273 int_buffer.AddObservation(Observation<int32_t>(
274 i, very_old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
275 }
276
277 // Next 50 (i.e., from 51 to 100) have recent timestamp.
278 for (int i = 51; i <= 100; ++i) {
279 time_delta_buffer.AddObservation(Observation<base::TimeDelta>(
280 base::TimeDelta::FromMilliseconds(i), now,
281 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
282 int_buffer.AddObservation(Observation<int32_t>(
283 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
284 }
285
286 // All samples have equal weight. So, the unweighted average is average of
287 // all samples.
288 EXPECT_TRUE(time_delta_buffer.GetUnweightedAverage(
289 very_old, std::vector<NetworkQualityObservationSource>(),
290 &time_delta_result));
291 EXPECT_NEAR(time_delta_result.InMilliseconds(), (1 + 100) / 2, 1);
292
293 EXPECT_TRUE(int_buffer.GetUnweightedAverage(
294 very_old, std::vector<NetworkQualityObservationSource>(), &int_result));
295 EXPECT_NEAR(int_result, (1 + 100) / 2, 1);
296
297 EXPECT_FALSE(time_delta_buffer.GetUnweightedAverage(
298 now + base::TimeDelta::FromSeconds(1),
299 std::vector<NetworkQualityObservationSource>(), &time_delta_result));
300 EXPECT_FALSE(int_buffer.GetUnweightedAverage(
301 now + base::TimeDelta::FromSeconds(1),
302 std::vector<NetworkQualityObservationSource>(), &int_result));
303 }
304
305 TEST(NetworkQualityObservationBufferTest, WeightedAverageDifferentTimestamps) {
306 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
307 ObservationBuffer<int32_t> int_buffer(0.5);
308 const base::TimeTicks now = base::TimeTicks::Now();
309 const base::TimeTicks very_old = now - base::TimeDelta::FromDays(7);
310
311 base::TimeDelta time_delta_result;
312 int32_t int_result;
313
314 // Network quality should be unavailable when no observations are available.
315 EXPECT_FALSE(time_delta_buffer.GetWeightedAverage(
316 base::TimeTicks(), std::vector<NetworkQualityObservationSource>(),
317 &time_delta_result));
318 EXPECT_FALSE(int_buffer.GetWeightedAverage(
319 base::TimeTicks(), std::vector<NetworkQualityObservationSource>(),
320 &int_result));
321
322 // First 50 samples have very old timestamp.
323 for (int i = 1; i <= 50; ++i) {
324 time_delta_buffer.AddObservation(Observation<base::TimeDelta>(
325 base::TimeDelta::FromMilliseconds(i), very_old,
326 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
327 int_buffer.AddObservation(Observation<int32_t>(
328 i, very_old, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
329 }
330
331 // Next 50 (i.e., from 51 to 100) have recent timestamp.
332 for (int i = 51; i <= 100; ++i) {
333 time_delta_buffer.AddObservation(Observation<base::TimeDelta>(
334 base::TimeDelta::FromMilliseconds(i), now,
335 NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
336 int_buffer.AddObservation(Observation<int32_t>(
337 i, now, NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST));
338 }
339
340 // Older samples have very little weight.
341 EXPECT_TRUE(time_delta_buffer.GetWeightedAverage(
342 very_old, std::vector<NetworkQualityObservationSource>(),
343 &time_delta_result));
344 // Weighted Average must be the average of all recent sampples.
345 EXPECT_NEAR(time_delta_result.InMilliseconds(), (51 + 100) / 2, 1);
346
347 EXPECT_TRUE(int_buffer.GetWeightedAverage(
348 very_old, std::vector<NetworkQualityObservationSource>(), &int_result));
349 // Weighted Average must be the average of all recent sampples.
350 EXPECT_NEAR(int_result, (51 + 100) / 2, 1);
351
352 EXPECT_FALSE(time_delta_buffer.GetWeightedAverage(
353 now + base::TimeDelta::FromSeconds(1),
354 std::vector<NetworkQualityObservationSource>(), &time_delta_result));
355 EXPECT_FALSE(int_buffer.GetWeightedAverage(
356 now + base::TimeDelta::FromSeconds(1),
357 std::vector<NetworkQualityObservationSource>(), &int_result));
358 }
359 #endif // !defined(OS_WIN)
360
245 // Verifies that the percentiles are correctly computed when some of the 361 // Verifies that the percentiles are correctly computed when some of the
246 // observation sources are disallowed. All observations have the same timestamp. 362 // observation sources are disallowed. All observations have the same timestamp.
247 TEST(NetworkQualityObservationBufferTest, DisallowedObservationSources) { 363 TEST(NetworkQualityObservationBufferTest, DisallowedObservationSources) {
248 ObservationBuffer<int32_t> int_buffer(0.5); 364 ObservationBuffer<int32_t> int_buffer(0.5);
249 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); 365 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5);
250 const base::TimeTicks now = base::TimeTicks::Now(); 366 const base::TimeTicks now = base::TimeTicks::Now();
251 367
252 int32_t result; 368 int32_t result;
253 base::TimeDelta time_delta_result; 369 base::TimeDelta time_delta_result;
254 370
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 } 504 }
389 } 505 }
390 506
391 } // namespace 507 } // namespace
392 508
393 } // namespace internal 509 } // namespace internal
394 510
395 } // namespace nqe 511 } // namespace nqe
396 512
397 } // namespace net 513 } // namespace net
OLDNEW
« net/nqe/observation_buffer.h ('K') | « net/nqe/observation_buffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698