/Users/deen/code/yugabyte-db/src/yb/util/hdr_histogram-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. 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, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <gtest/gtest.h> |
34 | | |
35 | | #include "yb/util/hdr_histogram.h" |
36 | | #include "yb/util/test_util.h" |
37 | | |
38 | | namespace yb { |
39 | | |
40 | | static const int kSigDigits = 2; |
41 | | |
42 | | class HdrHistogramTest : public YBTest { |
43 | | }; |
44 | | |
45 | 1 | TEST_F(HdrHistogramTest, SimpleTest) { |
46 | 1 | uint64_t highest_val = 10000LU; |
47 | | |
48 | 1 | HdrHistogram hist(highest_val, kSigDigits); |
49 | 1 | ASSERT_EQ(0, hist.CountInBucketForValue(1)); |
50 | 1 | hist.Increment(1); |
51 | 1 | ASSERT_EQ(1, hist.CountInBucketForValue(1)); |
52 | 1 | hist.IncrementBy(1, 3); |
53 | 1 | ASSERT_EQ(4, hist.CountInBucketForValue(1)); |
54 | 1 | hist.Increment(10); |
55 | 1 | ASSERT_EQ(1, hist.CountInBucketForValue(10)); |
56 | 1 | hist.Increment(20); |
57 | 1 | ASSERT_EQ(1, hist.CountInBucketForValue(20)); |
58 | 1 | ASSERT_EQ(0, hist.CountInBucketForValue(1000)); |
59 | 1 | hist.Increment(1000); |
60 | 1 | hist.Increment(1001); |
61 | 1 | ASSERT_EQ(2, hist.CountInBucketForValue(1000)); |
62 | | |
63 | 1 | ASSERT_EQ(1 + 1 * 3 + 10 + 20 + 1000 + 1001, |
64 | 1 | hist.TotalSum()); |
65 | 1 | } |
66 | | |
67 | 1 | TEST_F(HdrHistogramTest, TestCoordinatedOmission) { |
68 | 1 | uint64_t interval = 1000; |
69 | 1 | int loop_iters = 100; |
70 | 1 | int64_t normal_value = 10; |
71 | 1 | HdrHistogram hist(1000000LU, kSigDigits); |
72 | 101 | for (int i = 1; i <= loop_iters; i++) { |
73 | | // Simulate a periodic "large value" that would exhibit coordinated |
74 | | // omission were this loop to sleep on 'interval'. |
75 | 90 | int64_t value = (i % normal_value == 0) ? interval * 10 : normal_value; |
76 | | |
77 | 100 | hist.IncrementWithExpectedInterval(value, interval); |
78 | 100 | } |
79 | 1 | ASSERT_EQ(loop_iters - (loop_iters / normal_value), |
80 | 1 | hist.CountInBucketForValue(normal_value)); |
81 | 11 | for (auto i = interval; i <= interval * 10; i += interval) { |
82 | 10 | ASSERT_EQ(loop_iters / normal_value, hist.CountInBucketForValue(i)); |
83 | 10 | } |
84 | 1 | } |
85 | | |
86 | | static const int kExpectedSum = |
87 | | 10 * 80 + 100 * 10 + 1000 * 5 + 10000 * 3 + 100000 * 1 + 1000000 * 1; |
88 | | static const int kExpectedMax = 1000000; |
89 | | static const int kExpectedCount = 100; |
90 | | static const int kExpectedMin = 10; |
91 | 1 | static void load_percentiles(HdrHistogram* hist) { |
92 | 1 | hist->IncrementBy(10, 80); |
93 | 1 | hist->IncrementBy(100, 10); |
94 | 1 | hist->IncrementBy(1000, 5); |
95 | 1 | hist->IncrementBy(10000, 3); |
96 | 1 | hist->IncrementBy(100000, 1); |
97 | 1 | hist->IncrementBy(1000000, 1); |
98 | 1 | } |
99 | | |
100 | 2 | static void validate_percentiles(HdrHistogram* hist, uint64_t specified_max) { |
101 | 2 | double expected_mean = |
102 | 2 | static_cast<double>(kExpectedSum) / (80 + 10 + 5 + 3 + 1 + 1); |
103 | | |
104 | 2 | ASSERT_EQ(kExpectedMin, hist->MinValue()); |
105 | 2 | ASSERT_EQ(kExpectedMax, hist->MaxValue()); |
106 | 2 | ASSERT_EQ(kExpectedSum, hist->TotalSum()); |
107 | 2 | ASSERT_NEAR(expected_mean, hist->MeanValue(), 0.001); |
108 | 2 | ASSERT_EQ(kExpectedCount, hist->TotalCount()); |
109 | 2 | ASSERT_EQ(10, hist->ValueAtPercentile(80)); |
110 | 2 | ASSERT_EQ(kExpectedCount, hist->ValueAtPercentile(90)); |
111 | 2 | ASSERT_EQ(hist->LowestEquivalentValue(specified_max), hist->ValueAtPercentile(99)); |
112 | 2 | ASSERT_EQ(hist->LowestEquivalentValue(specified_max), hist->ValueAtPercentile(99.99)); |
113 | 2 | ASSERT_EQ(hist->LowestEquivalentValue(specified_max), hist->ValueAtPercentile(100)); |
114 | 2 | } |
115 | | |
116 | 1 | TEST_F(HdrHistogramTest, PercentileAndCopyTest) { |
117 | 1 | uint64_t specified_max = 10000; |
118 | 1 | HdrHistogram hist(specified_max, kSigDigits); |
119 | 1 | load_percentiles(&hist); |
120 | 1 | ASSERT_NO_FATALS(validate_percentiles(&hist, specified_max)); |
121 | | |
122 | 1 | HdrHistogram copy(hist); |
123 | 1 | ASSERT_NO_FATALS(validate_percentiles(©, specified_max)); |
124 | | |
125 | 1 | ASSERT_EQ(hist.TotalSum(), copy.TotalSum()); |
126 | 1 | } |
127 | | |
128 | | } // namespace yb |