Debian Codegraph @

test/core/gprpp/time_averaged_stats_test.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/gprpp/time_averaged_stats.h"
20
21 #include <math.h>
22
23 #include <gtest/gtest.h>
24
25 namespace grpc_core {
26 namespace {
27
28 TEST(TimeAveragedStatsTest, NoRegressNoPersistTest1) {
29 TimeAveragedStats tas(1000, 0, 0.0);
30 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
31 EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight());
32
33 /* Should have no effect */
34 tas.UpdateAverage();
35 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
36 EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight());
37
38 /* Should replace old average */
39 tas.AddSample(2000);
40 tas.UpdateAverage();
41 EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg());
42 EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight());
43 }
44
45 TEST(TimeAveragedStatsTest, NoRegressNoPersistTest2) {
46 TimeAveragedStats tas(1000, 0, 0.0);
47 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
48 /* Should replace init value */
49 tas.AddSample(2000);
50 tas.UpdateAverage();
51 EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg());
52 EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight());
53
54 tas.AddSample(3000);
55 tas.UpdateAverage();
56 EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg());
57 EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight());
58 }
59
60 TEST(TimeAveragedStatsTest, NoRegressNoPersistTest3) {
61 TimeAveragedStats tas(1000, 0, 0.0);
62 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
63 /* Should replace init value */
64 tas.AddSample(2500);
65 tas.UpdateAverage();
66 EXPECT_DOUBLE_EQ(2500, tas.aggregate_weighted_avg());
67 EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight());
68
69 tas.AddSample(3500);
70 tas.AddSample(4500);
71 tas.UpdateAverage();
72 EXPECT_DOUBLE_EQ(4000, tas.aggregate_weighted_avg());
73 EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight());
74 }
75
76 TEST(TimeAveragedStatsTest, SomeRegressNoPersistTest) {
77 TimeAveragedStats tas(1000, 0.5, 0.0);
78 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
79 EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight());
80 tas.AddSample(2000);
81 tas.AddSample(2000);
82 tas.UpdateAverage();
83 /* (2 * 2000 + 0.5 * 1000) / 2.5 */
84 EXPECT_DOUBLE_EQ(1800, tas.aggregate_weighted_avg());
85 EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight());
86 }
87
88 TEST(TimeAveragedStatsTest, SomeDecayTest) {
89 TimeAveragedStats tas(1000, 1, 0.0);
90 EXPECT_EQ(1000, tas.aggregate_weighted_avg());
91 /* Should avg with init value */
92 tas.AddSample(2000);
93 tas.UpdateAverage();
94 EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg());
95 EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight());
96
97 tas.AddSample(2000);
98 tas.UpdateAverage();
99 EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg());
100 EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight());
101
102 tas.AddSample(2000);
103 tas.UpdateAverage();
104 EXPECT_DOUBLE_EQ(1500, tas.aggregate_weighted_avg());
105 EXPECT_DOUBLE_EQ(2, tas.aggregate_total_weight());
106 }
107
108 TEST(TimeAveragedStatsTest, NoRegressFullPersistTest) {
109 TimeAveragedStats tas(1000, 0, 1.0);
110 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
111 EXPECT_DOUBLE_EQ(0, tas.aggregate_total_weight());
112
113 /* Should replace init value */
114 tas.AddSample(2000);
115 tas.UpdateAverage();
116 EXPECT_EQ(2000, tas.aggregate_weighted_avg());
117 EXPECT_EQ(1, tas.aggregate_total_weight());
118
119 /* Will result in average of the 3 samples. */
120 tas.AddSample(2300);
121 tas.AddSample(2300);
122 tas.UpdateAverage();
123 EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg());
124 EXPECT_DOUBLE_EQ(3, tas.aggregate_total_weight());
125 }
126
127 TEST(TimeAveragedStatsTest, NoRegressSomePersistTest) {
128 TimeAveragedStats tas(1000, 0, 0.5);
129 /* Should replace init value */
130 tas.AddSample(2000);
131 tas.UpdateAverage();
132 EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg());
133 EXPECT_DOUBLE_EQ(1, tas.aggregate_total_weight());
134
135 tas.AddSample(2500);
136 tas.AddSample(4000);
137 tas.UpdateAverage();
138 EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg());
139 EXPECT_DOUBLE_EQ(2.5, tas.aggregate_total_weight());
140 }
141
142 TEST(TimeAveragedStatsTest, SomeRegressSomePersistTest) {
143 TimeAveragedStats tas(1000, 0.4, 0.6);
144 /* Sample weight = 0 */
145 EXPECT_EQ(1000, tas.aggregate_weighted_avg());
146 EXPECT_EQ(0, tas.aggregate_total_weight());
147
148 tas.UpdateAverage();
149 /* (0.6 * 0 * 1000 + 0.4 * 1000 / 0.4) */
150 EXPECT_DOUBLE_EQ(1000, tas.aggregate_weighted_avg());
151 EXPECT_DOUBLE_EQ(0.4, tas.aggregate_total_weight());
152
153 tas.AddSample(2640);
154 tas.UpdateAverage();
155 /* (1 * 2640 + 0.6 * 0.4 * 1000 + 0.4 * 1000 / (1 + 0.6 * 0.4 + 0.4) */
156 EXPECT_DOUBLE_EQ(2000, tas.aggregate_weighted_avg());
157 EXPECT_DOUBLE_EQ(1.64, tas.aggregate_total_weight());
158
159 tas.AddSample(2876.8);
160 tas.UpdateAverage();
161 /* (1 * 2876.8 + 0.6 * 1.64 * 2000 + 0.4 * 1000 / (1 + 0.6 * 1.64 + 0.4) */
162 EXPECT_DOUBLE_EQ(2200, tas.aggregate_weighted_avg());
163 EXPECT_DOUBLE_EQ(2.384, tas.aggregate_total_weight());
164
165 tas.AddSample(4944.32);
166 tas.UpdateAverage();
167 /* (1 * 4944.32 + 0.6 * 2.384 * 2200 + 0.4 * 1000) /
168 (1 + 0.6 * 2.384 + 0.4) */
169 EXPECT_DOUBLE_EQ(3000, tas.aggregate_weighted_avg());
170 EXPECT_DOUBLE_EQ(2.8304, tas.aggregate_total_weight());
171 }
172
173 } // namespace
174 } // namespace grpc_core
175
176 int main(int argc, char** argv) {
177 ::testing::InitGoogleTest(&argc, argv);
178 return RUN_ALL_TESTS();
179 }