From 6c82e1be0902616bfbf8c3fd51ae27c99759e986 Mon Sep 17 00:00:00 2001 From: Andrea Lattuada Date: Tue, 13 Nov 2012 19:55:25 -0500 Subject: SERVER-7592 descriptive statistics estimators --- src/mongo/util/descriptive_stats_test.cpp | 126 ++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/mongo/util/descriptive_stats_test.cpp (limited to 'src/mongo/util/descriptive_stats_test.cpp') diff --git a/src/mongo/util/descriptive_stats_test.cpp b/src/mongo/util/descriptive_stats_test.cpp new file mode 100644 index 00000000000..9f6955645cd --- /dev/null +++ b/src/mongo/util/descriptive_stats_test.cpp @@ -0,0 +1,126 @@ +/** + * Tests for mongo/util/descriptive_stats.h + */ + +#include +#include +#include +#include + +#include "mongo/unittest/unittest.h" +#include "mongo/util/descriptive_stats.h" + +using namespace std; + +namespace { + + const size_t NumQuantiles = 99; + + TEST(DistributionEstimators, TestNominalResults) { + mongo::DistributionEstimators d; + + for (int i = 0; i < 100000; i++) { + d << double(i) / 100000; + } + ASSERT_TRUE(d.quantilesReady()); + for (size_t quant = 1; quant <= NumQuantiles; ++quant) { + ASSERT_EQUALS(d.probability(quant), double(quant) / 100); + ASSERT_APPROX_EQUAL(d.quantile(quant), double(quant) / 100, 0.05); + double prob = double(quant) / 100; + ASSERT_APPROX_EQUAL(d.icdf(prob), prob, 0.05); + } + ASSERT_APPROX_EQUAL(d.min(), 0.0, 0.05); + ASSERT_APPROX_EQUAL(d.max(), 1.0, 0.05); + ASSERT_APPROX_EQUAL(d.median(), 0.5, 0.05); + } + + TEST(DistributionEstimators, TestAppendQuantilesToBSONArrayBuilder) { + mongo::DistributionEstimators d; + + for (int i = 0; i < 10000; i++) { + d << static_cast(i) / 10000; + } + + mongo::BSONArrayBuilder arrayBuilder; + d.appendQuantilesToBSONArrayBuilder(arrayBuilder); + mongo::BSONArray arr = arrayBuilder.arr(); + + for (size_t i = 0; i <= NumQuantiles + 1; i++) { + ASSERT_EQUALS(arr[i].Number(), d.quantile(i)); + } + } + + TEST(BasicEstimators, TestNominalResults) { + mongo::BasicEstimators d; + + unsigned int count = 0; + // [50, 51, 52, ..., 99949, 99950] + for (int i = 50; i <= 100000 - 50; i++) { + d << unsigned(i); + count++; + } + ASSERT_EQUALS(d.min(), 50u); + ASSERT_EQUALS(d.max(), 100000u - 50u); + ASSERT_APPROX_EQUAL(d.mean(), 100000 / 2, 1e-15); + ASSERT_APPROX_EQUAL(d.stddev(), sqrt((static_cast(count) * count - 1) / 12), 1e-15); + } + + TEST(BasicEstimators, TestAppendBasicToBSONObjBuilder) { + mongo::BasicEstimators b; + + for (int i = 0; i < 10000; i++) { + b << i; + } + + mongo::BSONObjBuilder builder; + b.appendBasicToBSONObjBuilder(builder); + mongo::BSONObj obj = builder.obj(); + + ASSERT_EQUALS(obj["count"].Number(), b.count()); + ASSERT_EQUALS(obj["mean"].Number(), b.mean()); + ASSERT_EQUALS(obj["stddev"].Number(), b.stddev()); + ASSERT_EQUALS(obj["min"].Number(), b.min()); + ASSERT_EQUALS(obj["max"].Number(), b.max()); + } + + TEST(SummaryEstimators, TestNominalResults) { + mongo::SummaryEstimators d; + + for (int a = -200; a <= 200; a++) { + d << a; + } + ASSERT_TRUE(d.quantilesReady()); + for (size_t i = 0; i < d.numberOfQuantiles; i++) { + ASSERT_APPROX_EQUAL(d.quantile(i), -200 + static_cast(i) * 4, 1); + } + ASSERT_EQUALS(d.min(), -200); + ASSERT_EQUALS(d.max(), 200); + ASSERT_APPROX_EQUAL(d.mean(), 0, 1e-15); + ASSERT_APPROX_EQUAL(d.icdf(.25), -100, 1e-15); + } + + TEST(SummaryEstimators, TestStatisticSummaryToBSONObj) { + mongo::SummaryEstimators e; + + for (int i = 0; i < 10000; i++) { + e << static_cast(i) / 100; + } + verify(e.quantilesReady()); + + mongo::BSONObj obj = e.statisticSummaryToBSONObj(); + + ASSERT_EQUALS(obj["count"].Number(), e.count()); + ASSERT_EQUALS(obj["mean"].Number(), e.mean()); + ASSERT_EQUALS(obj["stddev"].Number(), e.stddev()); + ASSERT_EQUALS(obj["min"].Number(), e.min()); + ASSERT_EQUALS(obj["max"].Number(), e.max()); + + mongo::BSONObj quantiles = obj["quantiles"].Obj(); + ASSERT_EQUALS(static_cast(quantiles.nFields()), NumQuantiles); + for (mongo::BSONObjIterator it = quantiles.begin(); it.more(); ++it) { + ASSERT_EQUALS((*it).Number(), e.icdf(atof((*it).fieldName()))); + } + } + +} // namespace + -- cgit v1.2.1