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