diff options
author | Kshitij Gupta <kshitij.gupta@mongodb.com> | 2021-06-29 19:23:48 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-07-23 20:08:08 +0000 |
commit | a83aed652d99d3e5c21f2ab83c99b39afd2600c3 (patch) | |
tree | 2db92b525cce56179db1d5fae110de86634567d2 /src | |
parent | 394ba6d936777fb26b10ef8f530fb61c11c68921 (diff) | |
download | mongo-a83aed652d99d3e5c21f2ab83c99b39afd2600c3.tar.gz |
SERVER-57742: Create a non-specialized version of the
OperationLatencyHistogram class.
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/util/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/util/integer_histogram.h | 119 | ||||
-rw-r--r-- | src/mongo/util/integer_histogram_test.cpp | 169 |
3 files changed, 289 insertions, 0 deletions
diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript index a574b11efdc..b60dae5cb1b 100644 --- a/src/mongo/util/SConscript +++ b/src/mongo/util/SConscript @@ -649,6 +649,7 @@ icuEnv.CppUnitTest( 'future_test_promise_void.cpp', 'future_test_shared_future.cpp', 'future_util_test.cpp', + 'integer_histogram_test.cpp', 'hierarchical_acquisition_test.cpp', 'icu_test.cpp', 'static_immortal_test.cpp', diff --git a/src/mongo/util/integer_histogram.h b/src/mongo/util/integer_histogram.h new file mode 100644 index 00000000000..cab7447c26e --- /dev/null +++ b/src/mongo/util/integer_histogram.h @@ -0,0 +1,119 @@ +/** + * Copyright (C) 2021-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/commands.h" +#include <vector> + +namespace mongo { + +/** + * Generalized version of OperationLatencyHistogram that can track latencies for any operation type + * with custom lower bounds. For some provided lower bounds {x,y} a number z will be counted in the + * x-y bucket if z ∈ [x, y). in the y-inf bucket if z ∈ [y, inf). and in the (-inf, x) bucket if z < + * x. + */ +template <std::size_t numLowerBounds> +class IntegerHistogram { +public: + const std::string kKey; + + IntegerHistogram(std::string key, std::array<int64_t, numLowerBounds> lowerBounds) + : kKey(std::move(key)) { + invariant(!lowerBounds.empty(), "Lower bounds must not be empty"); + _lowerBoundBuckets[0].lowerBound = std::numeric_limits<int64_t>::min(); + int64_t prevVal = std::numeric_limits<int64_t>::min(); + for (size_t i = 1; i < _lowerBoundBuckets.size(); ++i) { + auto lowerBoundVal = lowerBounds.at(i - 1); + invariant(lowerBoundVal > prevVal, + "Lower bounds must be strictly monotonically increasing"); + + _lowerBoundBuckets[i].lowerBound = lowerBoundVal; + prevVal = lowerBoundVal; + } + } + + void append(BSONObjBuilder& builder, bool shouldAppendAdditionalInfo) const { + BSONObjBuilder histogramBuilder(builder.subobjStart(kKey)); + auto offsetToString = [this](size_t offset) { + if (offset == 0) + return std::string("-inf"); + if (offset < _lowerBoundBuckets.size()) + return std::to_string(_lowerBoundBuckets[offset].lowerBound); + return std::string("inf"); + }; + + for (size_t i = 0; i < _lowerBoundBuckets.size(); i++) { + auto count = _lowerBoundBuckets[i].count.load(); + if (count == 0) + continue; + + auto key = fmt::format("{} - {}", offsetToString(i), offsetToString(i + 1)); + BSONObjBuilder entryBuilder(histogramBuilder.subobjStart(key)); + entryBuilder.append("count", (long long)(count)); + entryBuilder.doneFast(); + } + + auto totalCount = _entryCount.load(); + histogramBuilder.append("ops", (long long)totalCount); + if (shouldAppendAdditionalInfo && totalCount != 0) { + auto sum = _sum.load(); + histogramBuilder.append("sum", (long long)sum); + histogramBuilder.append("mean", static_cast<double>(sum) / totalCount); + } + histogramBuilder.doneFast(); + } + + void increment(int64_t data) { + auto insertionIndex = std::upper_bound(_lowerBoundBuckets.begin(), + _lowerBoundBuckets.end(), + data, + [](const int64_t a, const LowerBoundBucket& b) { + return a < b.lowerBound; + }) - + 1; + + insertionIndex->count.addAndFetch(1); + _entryCount.addAndFetch(1); + _sum.addAndFetch(data); + } + +private: + struct LowerBoundBucket { + int64_t lowerBound; + AtomicWord<int64_t> count; + }; + + std::array<LowerBoundBucket, numLowerBounds + 1> _lowerBoundBuckets; + AtomicWord<int64_t> _entryCount; + AtomicWord<int64_t> _sum; +}; +} // namespace mongo diff --git a/src/mongo/util/integer_histogram_test.cpp b/src/mongo/util/integer_histogram_test.cpp new file mode 100644 index 00000000000..1a615cb5078 --- /dev/null +++ b/src/mongo/util/integer_histogram_test.cpp @@ -0,0 +1,169 @@ +/** + * Copyright (C) 2021-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" +#include "mongo/unittest/death_test.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/integer_histogram.h" + +namespace mongo { + +namespace { + +TEST(IntegerHistogram, EnsureCountsIncrementedAndStored) { + std::array<int64_t, 4> lowerBounds{0, 5, 8, 12}; + IntegerHistogram<4> hist("testKey", lowerBounds); + int64_t sum = 0; + int64_t numInserts = 15; + for (int64_t i = 0; i < numInserts; i++) { + hist.increment(i); + sum += i; + } + + auto out = [&] { + BSONObjBuilder builder; + hist.append(builder, true); + return builder.obj(); + }(); + + auto buckets = out["testKey"]; + ASSERT_EQUALS(buckets["0 - 5"]["count"].Long(), 5); + ASSERT_EQUALS(buckets["5 - 8"]["count"].Long(), 3); + ASSERT_EQUALS(buckets["8 - 12"]["count"].Long(), 4); + ASSERT_EQUALS(buckets["12 - inf"]["count"].Long(), 3); + ASSERT_EQUALS(buckets["ops"].Long(), numInserts); + ASSERT_EQUALS(buckets["sum"].Long(), sum); + ASSERT_EQUALS(buckets["mean"].Double(), static_cast<double>(sum) / numInserts); +} + +TEST(IntegerHistogram, EnsureCountsIncrementedInSmallestBucket) { + std::array<int64_t, 3> lowerBounds{5, 8, 12}; + IntegerHistogram<3> hist("testKey2", lowerBounds); + int64_t sum = 0; + int64_t numInserts = 5; + for (int64_t i = 0; i < numInserts; i++) { + hist.increment(i); + sum += i; + } + + auto out = [&] { + BSONObjBuilder builder; + hist.append(builder, true); + return builder.obj(); + }(); + + auto buckets = out["testKey2"]; + ASSERT_EQUALS(buckets["-inf - 5"]["count"].Long(), 5); + ASSERT_EQUALS(buckets["ops"].Long(), numInserts); + ASSERT_EQUALS(buckets["sum"].Long(), sum); + ASSERT_EQUALS(buckets["mean"].Double(), static_cast<double>(sum) / numInserts); +} + +TEST(IntegerHistogram, EnsureCountsCorrectlyIncrementedAtBoundary) { + std::array<int64_t, 3> lowerBounds{5, 8, 12}; + IntegerHistogram<3> hist("testKey3", lowerBounds); + int64_t sum = 0; + int64_t numInserts = 3; + for (auto& boundary : lowerBounds) { + hist.increment(boundary); + sum += boundary; + } + + auto out = [&] { + BSONObjBuilder builder; + hist.append(builder, true); + return builder.obj(); + }(); + + auto buckets = out["testKey3"]; + ASSERT_EQUALS(buckets["5 - 8"]["count"].Long(), 1); + ASSERT_EQUALS(buckets["8 - 12"]["count"].Long(), 1); + ASSERT_EQUALS(buckets["12 - inf"]["count"].Long(), 1); + ASSERT_EQUALS(buckets["ops"].Long(), numInserts); + ASSERT_EQUALS(buckets["sum"].Long(), sum); + ASSERT_EQUALS(buckets["mean"].Double(), static_cast<double>(sum) / numInserts); +} + +TEST(IntegerHistogram, EnsureNegativeCountsIncrementBucketsCorrectly) { + std::array<int64_t, 3> lowerBounds{-12, -8, 5}; + IntegerHistogram<3> hist("testKey4", lowerBounds); + int64_t sum = 0; + int64_t numInserts = 25; + for (int64_t i = -15; i < 10; i++) { + hist.increment(i); + sum += i; + } + + auto out = [&] { + BSONObjBuilder builder; + hist.append(builder, true); + return builder.obj(); + }(); + + auto buckets = out["testKey4"]; + ASSERT_EQUALS(buckets["-inf - -12"]["count"].Long(), 3); + ASSERT_EQUALS(buckets["-12 - -8"]["count"].Long(), 4); + ASSERT_EQUALS(buckets["-8 - 5"]["count"].Long(), 13); + ASSERT_EQUALS(buckets["5 - inf"]["count"].Long(), 5); + ASSERT_EQUALS(buckets["ops"].Long(), numInserts); + ASSERT_EQUALS(buckets["sum"].Long(), sum); + ASSERT_EQUALS(buckets["mean"].Double(), static_cast<double>(sum) / numInserts); +} + +TEST(IntegerHistogram, SkipsEmptyBuckets) { + std::array<int64_t, 2> lowerBounds{0, 5}; + IntegerHistogram<2> hist("testKey6", lowerBounds); + hist.increment(6); + + auto out = [&] { + BSONObjBuilder builder; + hist.append(builder, true); + return builder.obj(); + }(); + + auto buckets = out["testKey6"]; + ASSERT_THROWS(buckets["0 - 5"]["count"].Long(), DBException); + ASSERT_EQ(buckets["5 - inf"]["count"].Long(), 1); +} + +DEATH_TEST(IntegerHistogram, + FailIfFirstLowerBoundIsMin, + "Lower bounds must be strictly monotonically increasing") { + std::array<int64_t, 2> lowerBounds{std::numeric_limits<int64_t>::min(), 5}; + IntegerHistogram<2> hist("testKey5", lowerBounds); +} + +DEATH_TEST(IntegerHistogram, + FailsWhenLowerBoundNotMonotonic, + "Lower bounds must be strictly monotonically increasing") { + std::array<int64_t, 2> lowerBounds{5, 0}; + IntegerHistogram<2>("testKey7", lowerBounds); +} +} // namespace +} // namespace mongo |