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