xref: /aosp_15_r20/external/grpc-grpc/test/core/util/histogram_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 "test/core/util/histogram.h"
20 
21 #include <memory>
22 
23 #include "gtest/gtest.h"
24 
25 #include <grpc/support/log.h>
26 
27 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x);
28 
TEST(HistogramTest,NoOp)29 TEST(HistogramTest, NoOp) {
30   grpc_histogram_destroy(grpc_histogram_create(0.01, 60e9));
31 }
32 
expect_percentile(grpc_histogram * h,double percentile,double min_expect,double max_expect)33 static void expect_percentile(grpc_histogram* h, double percentile,
34                               double min_expect, double max_expect) {
35   double got = grpc_histogram_percentile(h, percentile);
36   gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got,
37           max_expect);
38   ASSERT_LE(min_expect, got);
39   ASSERT_LE(got, max_expect);
40 }
41 
TEST(HistogramTest,Simple)42 TEST(HistogramTest, Simple) {
43   grpc_histogram* h;
44 
45   LOG_TEST("test_simple");
46 
47   h = grpc_histogram_create(0.01, 60e9);
48   grpc_histogram_add(h, 10000);
49   grpc_histogram_add(h, 10000);
50   grpc_histogram_add(h, 11000);
51   grpc_histogram_add(h, 11000);
52 
53   expect_percentile(h, 50, 10001, 10999);
54   ASSERT_EQ(grpc_histogram_mean(h), 10500);
55 
56   grpc_histogram_destroy(h);
57 }
58 
TEST(HistogramTest,Percentile)59 TEST(HistogramTest, Percentile) {
60   grpc_histogram* h;
61   double last;
62   double i;
63   double cur;
64 
65   LOG_TEST("test_percentile");
66 
67   h = grpc_histogram_create(0.05, 1e9);
68   grpc_histogram_add(h, 2.5);
69   grpc_histogram_add(h, 2.5);
70   grpc_histogram_add(h, 8);
71   grpc_histogram_add(h, 4);
72 
73   ASSERT_EQ(grpc_histogram_count(h), 4);
74   ASSERT_EQ(grpc_histogram_minimum(h), 2.5);
75   ASSERT_EQ(grpc_histogram_maximum(h), 8);
76   ASSERT_EQ(grpc_histogram_sum(h), 17);
77   ASSERT_EQ(grpc_histogram_sum_of_squares(h), 92.5);
78   ASSERT_EQ(grpc_histogram_mean(h), 4.25);
79   ASSERT_EQ(grpc_histogram_variance(h), 5.0625);
80   ASSERT_EQ(grpc_histogram_stddev(h), 2.25);
81 
82   expect_percentile(h, -10, 2.5, 2.5);
83   expect_percentile(h, 0, 2.5, 2.5);
84   expect_percentile(h, 12.5, 2.5, 2.5);
85   expect_percentile(h, 25, 2.5, 2.5);
86   expect_percentile(h, 37.5, 2.5, 2.8);
87   expect_percentile(h, 50, 3.0, 3.5);
88   expect_percentile(h, 62.5, 3.5, 4.5);
89   expect_percentile(h, 75, 5, 7.9);
90   expect_percentile(h, 100, 8, 8);
91   expect_percentile(h, 110, 8, 8);
92 
93   // test monotonicity
94   last = 0.0;
95   for (i = 0; i < 100.0; i += 0.01) {
96     cur = grpc_histogram_percentile(h, i);
97     ASSERT_GE(cur, last);
98     last = cur;
99   }
100 
101   grpc_histogram_destroy(h);
102 }
103 
TEST(HistogramTest,Merge)104 TEST(HistogramTest, Merge) {
105   grpc_histogram *h1, *h2;
106   double last;
107   double i;
108   double cur;
109 
110   LOG_TEST("test_merge");
111 
112   h1 = grpc_histogram_create(0.05, 1e9);
113   grpc_histogram_add(h1, 2.5);
114   grpc_histogram_add(h1, 2.5);
115   grpc_histogram_add(h1, 8);
116   grpc_histogram_add(h1, 4);
117 
118   h2 = grpc_histogram_create(0.01, 1e9);
119   ASSERT_EQ(grpc_histogram_merge(h1, h2), 0);
120   grpc_histogram_destroy(h2);
121 
122   h2 = grpc_histogram_create(0.05, 1e10);
123   ASSERT_EQ(grpc_histogram_merge(h1, h2), 0);
124   grpc_histogram_destroy(h2);
125 
126   h2 = grpc_histogram_create(0.05, 1e9);
127   ASSERT_EQ(grpc_histogram_merge(h1, h2), 1);
128   ASSERT_EQ(grpc_histogram_count(h1), 4);
129   ASSERT_EQ(grpc_histogram_minimum(h1), 2.5);
130   ASSERT_EQ(grpc_histogram_maximum(h1), 8);
131   ASSERT_EQ(grpc_histogram_sum(h1), 17);
132   ASSERT_EQ(grpc_histogram_sum_of_squares(h1), 92.5);
133   ASSERT_EQ(grpc_histogram_mean(h1), 4.25);
134   ASSERT_EQ(grpc_histogram_variance(h1), 5.0625);
135   ASSERT_EQ(grpc_histogram_stddev(h1), 2.25);
136   grpc_histogram_destroy(h2);
137 
138   h2 = grpc_histogram_create(0.05, 1e9);
139   grpc_histogram_add(h2, 7.0);
140   grpc_histogram_add(h2, 17.0);
141   grpc_histogram_add(h2, 1.0);
142   ASSERT_EQ(grpc_histogram_merge(h1, h2), 1);
143   ASSERT_EQ(grpc_histogram_count(h1), 7);
144   ASSERT_EQ(grpc_histogram_minimum(h1), 1.0);
145   ASSERT_EQ(grpc_histogram_maximum(h1), 17.0);
146   ASSERT_EQ(grpc_histogram_sum(h1), 42.0);
147   ASSERT_EQ(grpc_histogram_sum_of_squares(h1), 431.5);
148   ASSERT_EQ(grpc_histogram_mean(h1), 6.0);
149 
150   // test monotonicity
151   last = 0.0;
152   for (i = 0; i < 100.0; i += 0.01) {
153     cur = grpc_histogram_percentile(h1, i);
154     ASSERT_GE(cur, last);
155     last = cur;
156   }
157 
158   grpc_histogram_destroy(h1);
159   grpc_histogram_destroy(h2);
160 }
161 
main(int argc,char ** argv)162 int main(int argc, char** argv) {
163   ::testing::InitGoogleTest(&argc, argv);
164   return RUN_ALL_TESTS();
165 }
166