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