Skip to content

Commit ca40cdb

Browse files
rdemellowdaverigby
authored andcommitted
MB-33735 Make HdrHistogram mean test more reliable
Make HdrHistogram mean test more reliable by testing the error percentage of the mean, calculated by HdrHistogram. This error percentage should be no more than +/-0.05%. Change-Id: I10e3486b9e7498eb8c22fd98015260184fca8953 Reviewed-on: http://review.couchbase.org/107948 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent c2c3088 commit ca40cdb

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

engines/ep/tests/module_tests/hdrhistogram_test.cc

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <hdr_histogram.h>
2323

2424
#include <cmath>
25+
#include <iomanip>
2526
#include <memory>
2627
#include <random>
2728
#include <thread>
@@ -173,14 +174,13 @@ TEST(HdrHistogramTest, addValueAndCountTest) {
173174
#define LOG_NORMAL_STD 2.0
174175
#define LOG_NORMAL_SCALE_UP_MULT 35000
175176
#define LOG_NORMAL_MIN 50000
176-
177+
static std::vector<uint64_t> valuesToAdd(10000);
178+
static bool initialised = false;
177179
// static function to return a log normal value scaled by
178180
// LOG_NORMAL_SCALE_UP_MULT. It creates an array of 10000 static values that
179181
// using std::lognormal_distribution and returners them in an incrementing
180182
// linear fashion so that they can be used for the meanTest
181183
static uint64_t GetNextLogNormalValue() {
182-
static bool initialised = false;
183-
static std::vector<uint64_t> valuesToAdd(10000);
184184
static unsigned int i = 0;
185185

186186
if (!initialised) {
@@ -219,7 +219,7 @@ static uint64_t GetNextLogNormalValue() {
219219
TEST(HdrHistogramTest, meanTest) {
220220
HdrHistogram histogram{0, 60000000, 3};
221221
uint64_t sum = 0;
222-
double total_count = 0;
222+
uint64_t total_count = 0;
223223

224224
for (uint64_t i = 0; i < 1000000; i++) {
225225
uint64_t count = GetNextLogNormalValue();
@@ -236,24 +236,13 @@ TEST(HdrHistogramTest, meanTest) {
236236
}
237237

238238
// calculate the mean
239-
double_t avg = (sum / total_count);
240-
int normaliseBy = 1;
241-
242-
// the mean will only be accurate to getSigFigAccuracy() in this case
243-
// 3 sig fig. Which means we only want to compare the 3 sig fig, thus
244-
// we need to normalise our mean to the one returned by the histogram.
245-
// To do this we work out the power of ten of our mean and then remove two
246-
// from this to leave us with two sig fig
247-
int tens = static_cast<int>(log10(avg)) + 1 - histogram.getSigFigAccuracy();
248-
249-
// no point in normalising if the mean is 3 sig fig or less.
250-
if (tens <= histogram.getSigFigAccuracy()) {
251-
normaliseBy = static_cast<int>(std::pow(10, tens));
252-
}
239+
double_t avg = (sum / static_cast<double_t>(total_count));
240+
241+
uint64_t meanDiff = std::abs(avg - histogram.getMean());
242+
double_t errorPer = (meanDiff / avg) * 100.0;
253243

254-
EXPECT_EQ(static_cast<unsigned int>(round(avg) / normaliseBy),
255-
static_cast<unsigned int>(round(histogram.getMean()) /
256-
normaliseBy));
244+
// check that the error percentage is less than 0.05%
245+
EXPECT_GT(0.05, std::abs(errorPer));
257246
}
258247

259248
void addValuesThread(HdrHistogram& histo,

0 commit comments

Comments
 (0)