Home | History | Annotate | Download | only in base
      1 // Copyright 2014 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 "cc/base/rolling_time_delta_history.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace cc {
      9 namespace {
     10 
     11 TEST(RollingTimeDeltaHistoryTest, EmptyHistory) {
     12   RollingTimeDeltaHistory empty_history(0);
     13 
     14   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(0.0));
     15   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(50.0));
     16   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(100.0));
     17 
     18   empty_history.InsertSample(base::TimeDelta::FromMilliseconds(10));
     19   empty_history.InsertSample(base::TimeDelta::FromMilliseconds(15));
     20   empty_history.InsertSample(base::TimeDelta::FromMilliseconds(20));
     21 
     22   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(0.0));
     23   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(50.0));
     24   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(100.0));
     25 
     26   empty_history.Clear();
     27   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(0.0));
     28   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(50.0));
     29   EXPECT_EQ(base::TimeDelta(), empty_history.Percentile(100.0));
     30 }
     31 
     32 TEST(RollingTimeDeltaHistoryTest, SizeOneHistory) {
     33   RollingTimeDeltaHistory size_one_history(1);
     34   base::TimeDelta sample1 = base::TimeDelta::FromMilliseconds(10);
     35   base::TimeDelta sample2 = base::TimeDelta::FromMilliseconds(20);
     36 
     37   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(0.0));
     38   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(50.0));
     39   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(100.0));
     40 
     41   size_one_history.InsertSample(sample1);
     42   EXPECT_EQ(sample1, size_one_history.Percentile(0.0));
     43   EXPECT_EQ(sample1, size_one_history.Percentile(50.0));
     44   EXPECT_EQ(sample1, size_one_history.Percentile(100.0));
     45 
     46   size_one_history.InsertSample(sample2);
     47   EXPECT_EQ(sample2, size_one_history.Percentile(0.0));
     48   EXPECT_EQ(sample2, size_one_history.Percentile(50.0));
     49   EXPECT_EQ(sample2, size_one_history.Percentile(100.0));
     50 
     51   size_one_history.Clear();
     52   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(0.0));
     53   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(50.0));
     54   EXPECT_EQ(base::TimeDelta(), size_one_history.Percentile(100.0));
     55 }
     56 
     57 TEST(RollingTimeDeltaHistoryTest, LargeHistory) {
     58   RollingTimeDeltaHistory large_history(100);
     59   base::TimeDelta sample1 = base::TimeDelta::FromMilliseconds(150);
     60   base::TimeDelta sample2 = base::TimeDelta::FromMilliseconds(250);
     61   base::TimeDelta sample3 = base::TimeDelta::FromMilliseconds(200);
     62 
     63   large_history.InsertSample(sample1);
     64   large_history.InsertSample(sample2);
     65 
     66   EXPECT_EQ(sample1, large_history.Percentile(0.0));
     67   EXPECT_EQ(sample1, large_history.Percentile(25.0));
     68   EXPECT_EQ(sample2, large_history.Percentile(75.0));
     69   EXPECT_EQ(sample2, large_history.Percentile(100.0));
     70 
     71   large_history.InsertSample(sample3);
     72   EXPECT_EQ(sample1, large_history.Percentile(0.0));
     73   EXPECT_EQ(sample1, large_history.Percentile(25.0));
     74   EXPECT_EQ(sample3, large_history.Percentile(50.0));
     75   EXPECT_EQ(sample2, large_history.Percentile(100.0));
     76 
     77   // Fill the history.
     78   for (int i = 1; i <= 97; i++)
     79     large_history.InsertSample(base::TimeDelta::FromMilliseconds(i));
     80 
     81   EXPECT_EQ(base::TimeDelta::FromMilliseconds(1),
     82             large_history.Percentile(0.0));
     83   for (int i = 1; i <= 97; i++) {
     84     EXPECT_EQ(base::TimeDelta::FromMilliseconds(i),
     85               large_history.Percentile(i - 0.5));
     86   }
     87   EXPECT_EQ(sample1, large_history.Percentile(97.5));
     88   EXPECT_EQ(sample3, large_history.Percentile(98.5));
     89   EXPECT_EQ(sample2, large_history.Percentile(99.5));
     90 
     91   // Continue inserting samples, causing the oldest samples to be discarded.
     92   base::TimeDelta sample4 = base::TimeDelta::FromMilliseconds(100);
     93   base::TimeDelta sample5 = base::TimeDelta::FromMilliseconds(102);
     94   base::TimeDelta sample6 = base::TimeDelta::FromMilliseconds(104);
     95   large_history.InsertSample(sample4);
     96   large_history.InsertSample(sample5);
     97   large_history.InsertSample(sample6);
     98   EXPECT_EQ(sample4, large_history.Percentile(97.5));
     99   EXPECT_EQ(sample5, large_history.Percentile(98.5));
    100   EXPECT_EQ(sample6, large_history.Percentile(99.5));
    101 
    102   large_history.Clear();
    103   EXPECT_EQ(base::TimeDelta(), large_history.Percentile(0.0));
    104   EXPECT_EQ(base::TimeDelta(), large_history.Percentile(50.0));
    105   EXPECT_EQ(base::TimeDelta(), large_history.Percentile(100.0));
    106 }
    107 
    108 }  // namespace
    109 }  // namespace cc
    110