diff options
author | Andrea Lattuada <andrea.lattuada@10gen.com> | 2012-11-13 19:55:25 -0500 |
---|---|---|
committer | Andrea Lattuada <andrea.lattuada@10gen.com> | 2012-11-13 19:55:25 -0500 |
commit | 6c82e1be0902616bfbf8c3fd51ae27c99759e986 (patch) | |
tree | 3898f39fe5d5895346a2e79d8ae02983fc9eb568 /src/mongo/util/descriptive_stats_test.cpp | |
parent | 74855308ab2d4c7d40e1da8829796744c5259797 (diff) | |
download | mongo-6c82e1be0902616bfbf8c3fd51ae27c99759e986.tar.gz |
SERVER-7592 descriptive statistics estimators
Diffstat (limited to 'src/mongo/util/descriptive_stats_test.cpp')
-rw-r--r-- | src/mongo/util/descriptive_stats_test.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
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 <cstdlib> +#include <cmath> +#include <limits> +#include <string> + +#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<NumQuantiles> 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<NumQuantiles> d; + + for (int i = 0; i < 10000; i++) { + d << static_cast<double>(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<unsigned int> 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<double>(count) * count - 1) / 12), 1e-15); + } + + TEST(BasicEstimators, TestAppendBasicToBSONObjBuilder) { + mongo::BasicEstimators<unsigned int> 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<int, NumQuantiles> 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<int>(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<double, NumQuantiles> e; + + for (int i = 0; i < 10000; i++) { + e << static_cast<double>(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<size_t>(quantiles.nFields()), NumQuantiles); + for (mongo::BSONObjIterator it = quantiles.begin(); it.more(); ++it) { + ASSERT_EQUALS((*it).Number(), e.icdf(atof((*it).fieldName()))); + } + } + +} // namespace + |