From ea2a2846790f2482f2127822bdfa2064df3cbed8 Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Thu, 9 May 2019 15:03:50 +0200 Subject: [PATCH] Unique values in the buffer --- README.md | 6 ++++++ include/Monitoring/Monitoring.h | 7 +++++++ src/Monitoring.cxx | 20 +++++++++++++++++++- test/testMonitoring.cxx | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 124287ee5..c464f53bd 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,12 @@ monitoring->flushBuffer(); See how it works in the example: [examples/10-Buffering.cxx](examples/10-Buffering.cxx). +### Unique metric buffering +In addition to above, you may want to keep only unique metrics (defined by metric name) within the buffer (only the last metric is kept). +```cpp +monitoring->enableUniqueBuffering(const std::size_t maxSize) +``` + ### Calculating derived metrics The module can calculate derived metrics. To do so, use optional `DerivedMetricMode mode` parameter of `send` method: ``` diff --git a/include/Monitoring/Monitoring.h b/include/Monitoring/Monitoring.h index 7aed192b0..d9b8eb569 100644 --- a/include/Monitoring/Monitoring.h +++ b/include/Monitoring/Monitoring.h @@ -82,6 +82,10 @@ class Monitoring /// Flushes metric buffer (this can also happen when buffer is full) void flushBuffer(); + /// Enables unique metric buffering + /// There might be only unique metric name within a buffer + void enableUniqueBuffering(const std::size_t size = 128); + /// Enables metric buffering /// \param size buffer size void enableBuffering(const std::size_t size = 128); @@ -137,6 +141,9 @@ class Monitoring /// Metric buffer std::unordered_map::type, std::vector> mStorage; + /// Flag that states whether unique buffering is enabled + bool mUniqueBuffering; + /// Flag stating whether metric buffering is enabled bool mBuffering; diff --git a/src/Monitoring.cxx b/src/Monitoring.cxx index 9d9facb9d..6f9af1c7f 100644 --- a/src/Monitoring.cxx +++ b/src/Monitoring.cxx @@ -37,12 +37,19 @@ Monitoring::Monitoring() { mProcessMonitor = std::make_unique(); mDerivedHandler = std::make_unique(); + mUniqueBuffering = false; mBuffering = false; mProcessMonitoringInterval = 0; mAutoPushInterval = 0; mMonitorRunning = false; } +void Monitoring::enableUniqueBuffering(const std::size_t size) +{ + mUniqueBuffering = true; + enableBuffering(size); +} + void Monitoring::enableBuffering(const std::size_t size) { mBufferSize = size; @@ -182,7 +189,18 @@ void Monitoring::transmit(Metric&& metric) { if (mBuffering) { auto index = static_cast::type>(metric.getVerbosity()); - mStorage[index].push_back(std::move(metric)); + if (mUniqueBuffering) { + auto find = std::find_if(mStorage[index].begin(), mStorage[index].end(), [&metric](Metric const& m) { + return m.getName() == metric.getName(); + }); + if (find == std::end(mStorage[index])) { + mStorage[index].push_back(std::move(metric)); + } else { + std::swap(*find, metric); + } + } else { + mStorage[index].push_back(std::move(metric)); + } if (mStorage[index].size() >= mBufferSize) { flushBuffer(index); } diff --git a/test/testMonitoring.cxx b/test/testMonitoring.cxx index 998a16c6b..e99a6e3e4 100644 --- a/test/testMonitoring.cxx +++ b/test/testMonitoring.cxx @@ -68,6 +68,21 @@ BOOST_AUTO_TEST_CASE(testSymbols) BOOST_WARN_MESSAGE(BOOST_IS_DEFINED( O2_MONITORING_OS_MAC ), "Mac OS detected"); } +BOOST_AUTO_TEST_CASE(uniqueBuffering) +{ + auto monitoring = Monitoring::Get("stdout://"); + monitoring->enableUniqueBuffering(4); + monitoring->send({10, "myMetricIntUnique"}); + monitoring->send({11, "myMetricInt"}); + monitoring->send({12, "myMetricInt"}); + monitoring->send({13, "myMetricIntAnother"}); + monitoring->send({14, "myMetricInt"}); + monitoring->send({15, "myMetricInt"}); + monitoring->send({16, "myMetricInt"}); + monitoring->send({17, "myMetricIntFlush"}); + monitoring->send({18, "myMetricInt"}); +} + } // namespace Test } // namespace monitoring } // namespace o2