Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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): crbug.com/656170 Enable these tests on Windows once these | |
| 249 // functions are called 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 // The first 50 samples have very old timestamp. | |
|
bengr
2016/10/18 21:32:35
timestamp -> timestamps.
tbansal1
2016/10/18 21:50:40
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 // The next 50 (i.e., from 51 to 100) samples have recent timestamp. | |
|
bengr
2016/10/18 21:32:35
timestamp -> timestamps
tbansal1
2016/10/18 21:50:40
Done.
| |
| 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 the 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 // The first 50 samples have very old timestamp. | |
|
bengr
2016/10/18 21:32:35
timestamp -> timestamps
tbansal1
2016/10/18 21:50:40
Done.
| |
| 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 // The next 50 (i.e., from 51 to 100) samples have recent timestamp. | |
|
bengr
2016/10/18 21:32:35
timestamp -> timestamps
tbansal1
2016/10/18 21:50:40
Done.
| |
| 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 // The older samples have very little weight, and so the weighted average must | |
| 341 // be approximately equal to the average of all recent samples. | |
| 342 EXPECT_TRUE(time_delta_buffer.GetWeightedAverage( | |
| 343 very_old, std::vector<NetworkQualityObservationSource>(), | |
| 344 &time_delta_result)); | |
| 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 EXPECT_NEAR(int_result, (51 + 100) / 2, 1); | |
| 350 | |
| 351 EXPECT_FALSE(time_delta_buffer.GetWeightedAverage( | |
| 352 now + base::TimeDelta::FromSeconds(1), | |
| 353 std::vector<NetworkQualityObservationSource>(), &time_delta_result)); | |
| 354 EXPECT_FALSE(int_buffer.GetWeightedAverage( | |
| 355 now + base::TimeDelta::FromSeconds(1), | |
| 356 std::vector<NetworkQualityObservationSource>(), &int_result)); | |
| 357 } | |
| 358 #endif // !defined(OS_WIN) | |
| 359 | |
| 245 // Verifies that the percentiles are correctly computed when some of the | 360 // Verifies that the percentiles are correctly computed when some of the |
| 246 // observation sources are disallowed. All observations have the same timestamp. | 361 // observation sources are disallowed. All observations have the same timestamp. |
| 247 TEST(NetworkQualityObservationBufferTest, DisallowedObservationSources) { | 362 TEST(NetworkQualityObservationBufferTest, DisallowedObservationSources) { |
| 248 ObservationBuffer<int32_t> int_buffer(0.5); | 363 ObservationBuffer<int32_t> int_buffer(0.5); |
| 249 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); | 364 ObservationBuffer<base::TimeDelta> time_delta_buffer(0.5); |
| 250 const base::TimeTicks now = base::TimeTicks::Now(); | 365 const base::TimeTicks now = base::TimeTicks::Now(); |
| 251 | 366 |
| 252 int32_t result; | 367 int32_t result; |
| 253 base::TimeDelta time_delta_result; | 368 base::TimeDelta time_delta_result; |
| 254 | 369 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 } | 503 } |
| 389 } | 504 } |
| 390 | 505 |
| 391 } // namespace | 506 } // namespace |
| 392 | 507 |
| 393 } // namespace internal | 508 } // namespace internal |
| 394 | 509 |
| 395 } // namespace nqe | 510 } // namespace nqe |
| 396 | 511 |
| 397 } // namespace net | 512 } // namespace net |
| OLD | NEW |