histogram_test.cc
Go to the documentation of this file.
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 
20 
21 #include <grpc/support/log.h>
22 
23 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x);
24 
25 static void test_no_op(void) {
27 }
28 
30  double min_expect, double max_expect) {
31  double got = grpc_histogram_percentile(h, percentile);
32  gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got,
33  max_expect);
34  GPR_ASSERT(min_expect <= got);
35  GPR_ASSERT(got <= max_expect);
36 }
37 
38 static void test_simple(void) {
39  grpc_histogram* h;
40 
41  LOG_TEST("test_simple");
42 
43  h = grpc_histogram_create(0.01, 60e9);
44  grpc_histogram_add(h, 10000);
45  grpc_histogram_add(h, 10000);
46  grpc_histogram_add(h, 11000);
47  grpc_histogram_add(h, 11000);
48 
49  expect_percentile(h, 50, 10001, 10999);
50  GPR_ASSERT(grpc_histogram_mean(h) == 10500);
51 
53 }
54 
55 static void test_percentile(void) {
56  grpc_histogram* h;
57  double last;
58  double i;
59  double cur;
60 
61  LOG_TEST("test_percentile");
62 
63  h = grpc_histogram_create(0.05, 1e9);
64  grpc_histogram_add(h, 2.5);
65  grpc_histogram_add(h, 2.5);
66  grpc_histogram_add(h, 8);
67  grpc_histogram_add(h, 4);
68 
74  GPR_ASSERT(grpc_histogram_mean(h) == 4.25);
75  GPR_ASSERT(grpc_histogram_variance(h) == 5.0625);
77 
78  expect_percentile(h, -10, 2.5, 2.5);
79  expect_percentile(h, 0, 2.5, 2.5);
80  expect_percentile(h, 12.5, 2.5, 2.5);
81  expect_percentile(h, 25, 2.5, 2.5);
82  expect_percentile(h, 37.5, 2.5, 2.8);
83  expect_percentile(h, 50, 3.0, 3.5);
84  expect_percentile(h, 62.5, 3.5, 4.5);
85  expect_percentile(h, 75, 5, 7.9);
86  expect_percentile(h, 100, 8, 8);
87  expect_percentile(h, 110, 8, 8);
88 
89  /* test monotonicity */
90  last = 0.0;
91  for (i = 0; i < 100.0; i += 0.01) {
93  GPR_ASSERT(cur >= last);
94  last = cur;
95  }
96 
98 }
99 
100 static void test_merge(void) {
101  grpc_histogram *h1, *h2;
102  double last;
103  double i;
104  double cur;
105 
106  LOG_TEST("test_merge");
107 
108  h1 = grpc_histogram_create(0.05, 1e9);
109  grpc_histogram_add(h1, 2.5);
110  grpc_histogram_add(h1, 2.5);
111  grpc_histogram_add(h1, 8);
112  grpc_histogram_add(h1, 4);
113 
114  h2 = grpc_histogram_create(0.01, 1e9);
115  GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0);
117 
118  h2 = grpc_histogram_create(0.05, 1e10);
119  GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0);
121 
122  h2 = grpc_histogram_create(0.05, 1e9);
123  GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1);
127  GPR_ASSERT(grpc_histogram_sum(h1) == 17);
129  GPR_ASSERT(grpc_histogram_mean(h1) == 4.25);
130  GPR_ASSERT(grpc_histogram_variance(h1) == 5.0625);
131  GPR_ASSERT(grpc_histogram_stddev(h1) == 2.25);
133 
134  h2 = grpc_histogram_create(0.05, 1e9);
135  grpc_histogram_add(h2, 7.0);
136  grpc_histogram_add(h2, 17.0);
137  grpc_histogram_add(h2, 1.0);
138  GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1);
141  GPR_ASSERT(grpc_histogram_maximum(h1) == 17.0);
142  GPR_ASSERT(grpc_histogram_sum(h1) == 42.0);
144  GPR_ASSERT(grpc_histogram_mean(h1) == 6.0);
145 
146  /* test monotonicity */
147  last = 0.0;
148  for (i = 0; i < 100.0; i += 0.01) {
150  GPR_ASSERT(cur >= last);
151  last = cur;
152  }
153 
156 }
157 
158 int main(void) {
159  test_no_op();
160  test_simple();
161  test_percentile();
162  test_merge();
163  return 0;
164 }
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
test_percentile
static void test_percentile(void)
Definition: histogram_test.cc:55
test_merge
static void test_merge(void)
Definition: histogram_test.cc:100
profile_analyzer.percentile
def percentile(N, percent, key=lambda x:x)
Definition: profile_analyzer.py:187
grpc_histogram_minimum
double grpc_histogram_minimum(grpc_histogram *h)
Definition: histogram.cc:219
test_no_op
static void test_no_op(void)
Definition: histogram_test.cc:25
test_simple
static void test_simple(void)
Definition: histogram_test.cc:38
grpc_histogram_count
double grpc_histogram_count(grpc_histogram *h)
Definition: histogram.cc:221
grpc_histogram_destroy
void grpc_histogram_destroy(grpc_histogram *h)
Definition: histogram.cc:99
grpc_histogram
Definition: histogram.cc:37
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
LOG_TEST
#define LOG_TEST(x)
Definition: histogram_test.cc:23
expect_percentile
static void expect_percentile(grpc_histogram *h, double percentile, double min_expect, double max_expect)
Definition: histogram_test.cc:29
grpc_histogram_percentile
double grpc_histogram_percentile(grpc_histogram *h, double percentile)
Definition: histogram.cc:198
histogram.h
grpc_histogram_stddev
double grpc_histogram_stddev(grpc_histogram *h)
Definition: histogram.cc:207
grpc_histogram_variance
double grpc_histogram_variance(grpc_histogram *h)
Definition: histogram.cc:211
grpc_histogram_create
grpc_histogram * grpc_histogram_create(double resolution, double max_bucket_start)
Definition: histogram.cc:77
memory_diff.cur
def cur
Definition: memory_diff.py:83
grpc_histogram_maximum
double grpc_histogram_maximum(grpc_histogram *h)
Definition: histogram.cc:217
main
int main(void)
Definition: histogram_test.cc:158
grpc_histogram_sum
double grpc_histogram_sum(grpc_histogram *h)
Definition: histogram.cc:223
grpc_histogram_sum_of_squares
double grpc_histogram_sum_of_squares(grpc_histogram *h)
Definition: histogram.cc:225
grpc_histogram_mean
double grpc_histogram_mean(grpc_histogram *h)
Definition: histogram.cc:202
grpc_histogram_add
void grpc_histogram_add(grpc_histogram *h, double x)
Definition: histogram.cc:104
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_histogram_merge
int grpc_histogram_merge(grpc_histogram *dst, const grpc_histogram *src)
Definition: histogram.cc:117


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:13