proxygen
HistogramTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (Histogram, Test100)
 
 TEST (Histogram, TestEmpty)
 
 TEST (Histogram, Test1)
 
 TEST (Histogram, TestOverflowMin)
 
 TEST (Histogram, TestOverflowMax)
 
 TEST (Histogram, TestOverflowBucket)
 
 TEST (Histogram, TestDouble)
 
 TEST (Histogram, TestDoubleInexactWidth)
 
 TEST (Histogram, TestDoubleWidthTooBig)
 
 TEST (Histogram, Counts)
 

Function Documentation

TEST ( Histogram  ,
Test100   
)

Definition at line 25 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, EXPECT_FLOAT_EQ, folly::Histogram< T >::getNumBuckets(), folly::Histogram< T >::getPercentileBucketIdx(), folly::Histogram< T >::getPercentileEstimate(), and h.

25  {
26  Histogram<int64_t> h(1, 0, 100);
27 
28  for (unsigned int n = 0; n < 100; ++n) {
29  h.addValue(n);
30  }
31 
32  // 100 buckets, plus 1 for below min, and 1 for above max
33  EXPECT_EQ(h.getNumBuckets(), 102);
34 
35  double epsilon = 1e-6;
36  for (unsigned int n = 0; n <= 100; ++n) {
37  double pct = n / 100.0;
38 
39  // Floating point arithmetic isn't 100% accurate, and if we just divide
40  // (n / 100) the value should be exactly on a bucket boundary. Add espilon
41  // to ensure we fall in the upper bucket.
42  if (n < 100) {
43  double lowPct = -1.0;
44  double highPct = -1.0;
45  unsigned int bucketIdx =
46  h.getPercentileBucketIdx(pct + epsilon, &lowPct, &highPct);
47  EXPECT_EQ(n + 1, bucketIdx);
48  EXPECT_FLOAT_EQ(n / 100.0, lowPct);
49  EXPECT_FLOAT_EQ((n + 1) / 100.0, highPct);
50  }
51 
52  // Also test n - epsilon, to test falling in the lower bucket.
53  if (n > 0) {
54  double lowPct = -1.0;
55  double highPct = -1.0;
56  unsigned int bucketIdx =
57  h.getPercentileBucketIdx(pct - epsilon, &lowPct, &highPct);
58  EXPECT_EQ(n, bucketIdx);
59  EXPECT_FLOAT_EQ((n - 1) / 100.0, lowPct);
60  EXPECT_FLOAT_EQ(n / 100.0, highPct);
61  }
62 
63  // Check getPercentileEstimate()
64  EXPECT_EQ(n, h.getPercentileEstimate(pct));
65  }
66 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FLOAT_EQ(val1, val2)
Definition: gtest.h:2027
TEST ( Histogram  ,
TestEmpty   
)

Definition at line 70 of file HistogramTest.cpp.

References EXPECT_EQ, EXPECT_FLOAT_EQ, folly::Histogram< T >::getPercentileBucketIdx(), folly::Histogram< T >::getPercentileEstimate(), and h.

70  {
71  Histogram<int64_t> h(1, 0, 100);
72 
73  for (unsigned int n = 0; n <= 100; ++n) {
74  double pct = n / 100.0;
75 
76  double lowPct = -1.0;
77  double highPct = -1.0;
78  unsigned int bucketIdx = h.getPercentileBucketIdx(pct, &lowPct, &highPct);
79  EXPECT_EQ(1, bucketIdx);
80  EXPECT_FLOAT_EQ(0.0, lowPct);
81  EXPECT_FLOAT_EQ(0.0, highPct);
82 
83  EXPECT_EQ(0, h.getPercentileEstimate(pct));
84  }
85 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FLOAT_EQ(val1, val2)
Definition: gtest.h:2027
TEST ( Histogram  ,
Test1   
)

Definition at line 89 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, EXPECT_FLOAT_EQ, folly::Histogram< T >::getPercentileBucketIdx(), folly::Histogram< T >::getPercentileEstimate(), and h.

89  {
90  Histogram<int64_t> h(1, 0, 100);
91  h.addValue(42);
92 
93  for (unsigned int n = 0; n < 100; ++n) {
94  double pct = n / 100.0;
95 
96  double lowPct = -1.0;
97  double highPct = -1.0;
98  unsigned int bucketIdx = h.getPercentileBucketIdx(pct, &lowPct, &highPct);
99  EXPECT_EQ(43, bucketIdx);
100  EXPECT_FLOAT_EQ(0.0, lowPct);
101  EXPECT_FLOAT_EQ(1.0, highPct);
102 
103  EXPECT_EQ(42, h.getPercentileEstimate(pct));
104  }
105 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_FLOAT_EQ(val1, val2)
Definition: gtest.h:2027
TEST ( Histogram  ,
TestOverflowMin   
)

Definition at line 109 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, folly::Histogram< T >::getPercentileEstimate(), h, int64_t, and min.

109  {
110  Histogram<int64_t> h(1, 0, 100);
111 
112  for (unsigned int n = 0; n < 9; ++n) {
113  h.addValue(-0x0fffffffffffffff);
114  }
115 
116  // Compute a percentile estimate. We only added values to the "below min"
117  // bucket, so this should check that bucket. We're mainly verifying that the
118  // code doesn't crash here when the bucket average is larger than the max
119  // value that is supposed to be in the bucket.
120  int64_t estimate = h.getPercentileEstimate(0.05);
121  // The code will return the smallest possible value when it detects an
122  // overflow beyond the minimum value.
124 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogLevel min
Definition: LogLevel.cpp:30
TEST ( Histogram  ,
TestOverflowMax   
)

Definition at line 128 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, folly::Histogram< T >::getPercentileEstimate(), h, int64_t, and max.

128  {
129  Histogram<int64_t> h(1, 0, 100);
130 
131  for (unsigned int n = 0; n < 9; ++n) {
132  h.addValue(0x0fffffffffffffff);
133  }
134 
135  // The code will return the maximum possible value when it detects an
136  // overflow beyond the max value.
137  int64_t estimate = h.getPercentileEstimate(0.95);
139 }
*than *hazptr_holder h
Definition: Hazptr.h:116
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Histogram  ,
TestOverflowBucket   
)

Definition at line 143 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, folly::Histogram< T >::getPercentileEstimate(), h, and int64_t.

143  {
144  Histogram<int64_t> h(0x0100000000000000, 0, 0x1000000000000000);
145 
146  for (unsigned int n = 0; n < 9; ++n) {
147  h.addValue(0x0fffffffffffffff);
148  }
149 
150  // The histogram code should return the bucket midpoint
151  // when it detects overflow.
152  int64_t estimate = h.getPercentileEstimate(0.95);
153  EXPECT_EQ(0x0f80000000000000, estimate);
154 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Histogram  ,
TestDouble   
)

Definition at line 156 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, folly::Histogram< T >::getNumBuckets(), folly::Histogram< T >::getPercentileEstimate(), and h.

156  {
157  // Insert 100 evenly spaced values into a histogram
158  Histogram<double> h(100.0, 0.0, 5000.0);
159  for (double n = 50; n < 5000; n += 100) {
160  h.addValue(n);
161  }
162  EXPECT_EQ(52, h.getNumBuckets());
163  EXPECT_EQ(2500.0, h.getPercentileEstimate(0.5));
164  EXPECT_EQ(4500.0, h.getPercentileEstimate(0.9));
165 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Histogram  ,
TestDoubleInexactWidth   
)

Definition at line 168 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, folly::Histogram< T >::getBucketByIndex(), folly::Histogram< T >::getNumBuckets(), folly::Histogram< T >::getPercentileEstimate(), and h.

168  {
169  Histogram<double> h(100.0, 0.0, 4970.0);
170  for (double n = 50; n < 5000; n += 100) {
171  h.addValue(n);
172  }
173  EXPECT_EQ(52, h.getNumBuckets());
174  EXPECT_EQ(2500.0, h.getPercentileEstimate(0.5));
175  EXPECT_EQ(4500.0, h.getPercentileEstimate(0.9));
176 
177  EXPECT_EQ(0, h.getBucketByIndex(51).count);
178  h.addValue(4990);
179  h.addValue(5100);
180  EXPECT_EQ(2, h.getBucketByIndex(51).count);
181  EXPECT_EQ(2600.0, h.getPercentileEstimate(0.5));
182 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( Histogram  ,
TestDoubleWidthTooBig   
)

Definition at line 187 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), EXPECT_EQ, EXPECT_NEAR, folly::Histogram< T >::getBucketByIndex(), folly::Histogram< T >::getNumBuckets(), folly::Histogram< T >::getPercentileEstimate(), and h.

187  {
188  Histogram<double> h(100.0, 0.0, 7.0);
189  EXPECT_EQ(3, h.getNumBuckets());
190 
191  for (double n = 0; n < 7; n += 1) {
192  h.addValue(n);
193  }
194  EXPECT_EQ(0, h.getBucketByIndex(0).count);
195  EXPECT_EQ(7, h.getBucketByIndex(1).count);
196  EXPECT_EQ(0, h.getBucketByIndex(2).count);
197  EXPECT_EQ(3.0, h.getPercentileEstimate(0.5));
198 
199  h.addValue(-1.0);
200  EXPECT_EQ(1, h.getBucketByIndex(0).count);
201  h.addValue(7.5);
202  EXPECT_EQ(1, h.getBucketByIndex(2).count);
203  EXPECT_NEAR(3.0, h.getPercentileEstimate(0.5), 1e-14);
204 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NEAR(val1, val2, abs_error)
Definition: gtest.h:2043
TEST ( Histogram  ,
Counts   
)

Definition at line 207 of file HistogramTest.cpp.

References folly::Histogram< T >::addValue(), folly::Histogram< T >::computeTotalCount(), EXPECT_EQ, folly::Histogram< T >::getNumBuckets(), h, i, and int32_t.

207  {
208  Histogram<int32_t> h(1, 0, 10);
209  EXPECT_EQ(12, h.getNumBuckets());
210  EXPECT_EQ(0, h.computeTotalCount());
211 
212  // Add one to each bucket, make sure the counts match
213  for (int32_t i = 0; i < 10; i++) {
214  h.addValue(i);
215  EXPECT_EQ(i + 1, h.computeTotalCount());
216  }
217 
218  // Add a lot to one bucket, make sure the counts still make sense
219  for (int32_t i = 0; i < 100; i++) {
220  h.addValue(0);
221  }
222  EXPECT_EQ(110, h.computeTotalCount());
223 }
*than *hazptr_holder h
Definition: Hazptr.h:116
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922