2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2013-01-29 20:23:31 +00:00
|
|
|
#include "util/histogram.h"
|
|
|
|
|
|
|
|
#include "util/testharness.h"
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class HistogramTest : public testing::Test {};
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(HistogramTest, BasicOperation) {
|
2013-02-15 19:53:17 +00:00
|
|
|
HistogramImpl histogram;
|
2013-01-29 20:23:31 +00:00
|
|
|
for (uint64_t i = 1; i <= 100; i++) {
|
|
|
|
histogram.Add(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
double median = histogram.Median();
|
|
|
|
// ASSERT_LE(median, 50);
|
|
|
|
ASSERT_GT(median, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
double percentile100 = histogram.Percentile(100.0);
|
|
|
|
ASSERT_LE(percentile100, 100.0);
|
|
|
|
ASSERT_GT(percentile100, 0.0);
|
|
|
|
double percentile99 = histogram.Percentile(99.0);
|
|
|
|
double percentile85 = histogram.Percentile(85.0);
|
|
|
|
ASSERT_LE(percentile99, 99.0);
|
|
|
|
ASSERT_TRUE(percentile99 >= percentile85);
|
|
|
|
}
|
|
|
|
|
2015-04-25 09:14:27 +00:00
|
|
|
ASSERT_EQ(histogram.Average(), 50.5); // avg is acurately calculated.
|
2013-01-29 20:23:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(HistogramTest, EmptyHistogram) {
|
2013-02-15 19:53:17 +00:00
|
|
|
HistogramImpl histogram;
|
2013-01-29 20:23:31 +00:00
|
|
|
ASSERT_EQ(histogram.Median(), 0.0);
|
|
|
|
ASSERT_EQ(histogram.Percentile(85.0), 0.0);
|
|
|
|
ASSERT_EQ(histogram.Average(), 0.0);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(HistogramTest, ClearHistogram) {
|
2013-02-15 19:53:17 +00:00
|
|
|
HistogramImpl histogram;
|
2013-01-29 20:23:31 +00:00
|
|
|
for (uint64_t i = 1; i <= 100; i++) {
|
|
|
|
histogram.Add(i);
|
|
|
|
}
|
|
|
|
histogram.Clear();
|
|
|
|
ASSERT_EQ(histogram.Median(), 0);
|
|
|
|
ASSERT_EQ(histogram.Percentile(85.0), 0);
|
|
|
|
ASSERT_EQ(histogram.Average(), 0);
|
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2013-01-29 20:23:31 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-01-29 20:23:31 +00:00
|
|
|
}
|