src/net/nqe/observation_buffer_unittest.cc
// Verifies that the percentiles are correctly computed. All observations have
// the same timestamp.
Verifies that the percentiles are correctly computed. All observations have
the same timestamp.
Verifies that the percentiles are correctly computed . All observations have the same timestamp .
TEST(NetworkQualityObservationBufferTest, PercentileSameTimestamps) {
  std::map<std::string, std::string> variation_params;
  NetworkQualityEstimatorParams params(variation_params);
  base::SimpleTestTickClock tick_clock;
  tick_clock.Advance(base::TimeDelta::FromMinutes(1));
  ObservationBuffer buffer(&params, &tick_clock, 0.5, 1.0);
  ASSERT_EQ(0u, buffer.Size());
  ASSERT_LT(0u, buffer.Capacity());
  const base::TimeTicks now = tick_clock.NowTicks();
  size_t observations_count = 0;
  EXPECT_FALSE(
      buffer
          .GetPercentile(base::TimeTicks(), INT32_MIN, 50,
                         &observations_count)
          .has_value());
  EXPECT_EQ(0u, observations_count);
  for (int i = 1; i <= 99; i += 2) {
    buffer.AddObservation(Observation(i, now, INT32_MIN,
                                      NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP));
    EXPECT_TRUE(buffer.GetPercentile(base::TimeTicks(), INT32_MIN, 50, nullptr)
                    .has_value());
    ASSERT_EQ(static_cast<size_t>(i / 2 + 1), buffer.Size());
  }
  for (int i = 2; i <= 100; i += 2) {
    buffer.AddObservation(Observation(i, now, INT32_MIN,
                                      NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP));
    EXPECT_TRUE(buffer.GetPercentile(base::TimeTicks(), INT32_MIN, 50, nullptr)
                    .has_value());
    ASSERT_EQ(static_cast<size_t>(i / 2 + 50), buffer.Size());
  }
  ASSERT_EQ(100u, buffer.Size());
  for (int i = 0; i <= 100; ++i) {
    base::Optional<int32_t> result = buffer.GetPercentile(
        base::TimeTicks(), INT32_MIN, i, &observations_count);
    EXPECT_EQ(100u, observations_count);
    EXPECT_TRUE(result.has_value());
    EXPECT_NEAR(result.value(), i, 1.0);
  }
  EXPECT_FALSE(
      buffer
          .GetPercentile(now + base::TimeDelta::FromSeconds(1), INT32_MIN, 50,
                         &observations_count)
          .has_value());
  EXPECT_EQ(0u, observations_count);
  buffer.Clear();
  EXPECT_FALSE(
      buffer
          .GetPercentile(base::TimeTicks(), INT32_MIN, 50,
                         &observations_count)
          .has_value());
  EXPECT_EQ(0u, observations_count);
}
TEST ( NetworkQualityObservationBufferTest , PercentileSameTimestamps ) { std :: map < std :: string , std :: string > variation_params ; NetworkQualityEstimatorParams params ( variation_params ) ; base :: SimpleTestTickClock tick_clock ; tick_clock . Advance ( base :: TimeDelta :: FromMinutes ( 1 ) ) ; ObservationBuffer buffer ( & params , & tick_clock , 0.5 , 1.0 ) ; ASSERT_EQ ( 0u , buffer . Size ( ) ) ; ASSERT_LT ( 0u , buffer . Capacity ( ) ) ; const base :: TimeTicks now = tick_clock . NowTicks ( ) ; size_t observations_count = 0 ; EXPECT_FALSE ( buffer . GetPercentile ( base :: TimeTicks ( ) , INT32_MIN , 50 , & observations_count ) . has_value ( ) ) ; EXPECT_EQ ( 0u , observations_count ) ; for ( int i = 1 ; i <= 99 ; i += 2 ) { buffer . AddObservation ( Observation ( i , now , INT32_MIN , NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP ) ) ; EXPECT_TRUE ( buffer . GetPercentile ( base :: TimeTicks ( ) , INT32_MIN , 50 , nullptr ) . has_value ( ) ) ; ASSERT_EQ ( static_cast < size_t > ( i / 2 + 1 ) , buffer . Size ( ) ) ; } for ( int i = 2 ; i <= 100 ; i += 2 ) { buffer . AddObservation ( Observation ( i , now , INT32_MIN , NETWORK_QUALITY_OBSERVATION_SOURCE_HTTP ) ) ; EXPECT_TRUE ( buffer . GetPercentile ( base :: TimeTicks ( ) , INT32_MIN , 50 , nullptr ) . has_value ( ) ) ; ASSERT_EQ ( static_cast < size_t > ( i / 2 + 50 ) , buffer . Size ( ) ) ; } ASSERT_EQ ( 100u , buffer . Size ( ) ) ; for ( int i = 0 ; i <= 100 ; ++ i ) { base :: Optional < int32_t > result = buffer . GetPercentile ( base :: TimeTicks ( ) , INT32_MIN , i , & observations_count ) ; EXPECT_EQ ( 100u , observations_count ) ; EXPECT_TRUE ( result . has_value ( ) ) ; EXPECT_NEAR ( result . value ( ) , i , 1.0 ) ; } EXPECT_FALSE ( buffer . GetPercentile ( now + base :: TimeDelta :: FromSeconds ( 1 ) , INT32_MIN , 50 , & observations_count ) . has_value ( ) ) ; EXPECT_EQ ( 0u , observations_count ) ; buffer . Clear ( ) ; EXPECT_FALSE ( buffer . GetPercentile ( base :: TimeTicks ( ) , INT32_MIN , 50 , & observations_count ) . has_value ( ) ) ; EXPECT_EQ ( 0u , observations_count ) ; }
Verifies that the percentiles are correctly computed.
Verifies that the percentiles are correctly computed .
// Percentiles should be unavailable when no observations are available. // Insert samples from {1,2,3,..., 100}. First insert odd samples, then even // samples. This helps in verifying that the order of samples does not matter. // Checks if the difference between actual result and the computed result is // less than 1. This is required because computed percentiles may be // slightly different from what is expected due to floating point // computation errors and integer rounding off errors. // Percentiles should be unavailable when no observations are available.