diff options
author | Igor Makunin <igor.makunin@gmail.com> | 2022-02-10 16:49:35 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:35 +0300 |
commit | c617191a3b33c0f5e1be6390361dbe540775d158 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/metrics | |
parent | f7a438035f19b364b5479caf536990e82b174f38 (diff) | |
download | ydb-c617191a3b33c0f5e1be6390361dbe540775d158.tar.gz |
Restoring authorship annotation for Igor Makunin <igor.makunin@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics')
-rw-r--r-- | library/cpp/monlib/metrics/metric.h | 8 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/timer.h | 56 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/timer_ut.cpp | 206 | ||||
-rw-r--r-- | library/cpp/monlib/metrics/ut/ya.make | 2 |
4 files changed, 136 insertions, 136 deletions
diff --git a/library/cpp/monlib/metrics/metric.h b/library/cpp/monlib/metrics/metric.h index fe97d2b475..b8ce12d753 100644 --- a/library/cpp/monlib/metrics/metric.h +++ b/library/cpp/monlib/metrics/metric.h @@ -371,13 +371,13 @@ namespace NMonitoring { } void Accept(TInstant time, IMetricConsumer* consumer) const override { - consumer->OnHistogram(time, TakeSnapshot()); + consumer->OnHistogram(time, TakeSnapshot()); } IHistogramSnapshotPtr TakeSnapshot() const override { - return Collector_->Snapshot(); - } - + return Collector_->Snapshot(); + } + void Reset() override { Collector_->Reset(); } diff --git a/library/cpp/monlib/metrics/timer.h b/library/cpp/monlib/metrics/timer.h index b45cc0bc9b..5c4e26e37b 100644 --- a/library/cpp/monlib/metrics/timer.h +++ b/library/cpp/monlib/metrics/timer.h @@ -1,29 +1,29 @@ -#pragma once - +#pragma once + #include "metric.h" - + #include <util/generic/typetraits.h> -#include <chrono> - - -namespace NMonitoring { - - /** - * A timing scope to record elapsed time since creation. - */ +#include <chrono> + + +namespace NMonitoring { + + /** + * A timing scope to record elapsed time since creation. + */ template <typename TMetric, - typename Resolution = std::chrono::milliseconds, - typename Clock = std::chrono::high_resolution_clock> + typename Resolution = std::chrono::milliseconds, + typename Clock = std::chrono::high_resolution_clock> class TMetricTimerScope { - public: + public: explicit TMetricTimerScope(TMetric* metric) : Metric_(metric) - , StartTime_(Clock::now()) - { + , StartTime_(Clock::now()) + { Y_ENSURE(Metric_); - } - + } + TMetricTimerScope(TMetricTimerScope&) = delete; TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; @@ -45,7 +45,7 @@ namespace NMonitoring { return; } - auto duration = std::chrono::duration_cast<Resolution>(Clock::now() - StartTime_).count(); + auto duration = std::chrono::duration_cast<Resolution>(Clock::now() - StartTime_).count(); if constexpr (std::is_same<TMetric, TGauge>::value) { Metric_->Set(duration); } else if constexpr (std::is_same<TMetric, TIntGauge>::value) { @@ -56,13 +56,13 @@ namespace NMonitoring { Metric_->Add(duration); } else if constexpr (std::is_same<TMetric, THistogram>::value) { Metric_->Record(duration); - } else { + } else { static_assert(TDependentFalse<TMetric>, "Not supported metric type"); - } + } Metric_ = nullptr; - } - + } + ~TMetricTimerScope() { if (Metric_ == nullptr) { return; @@ -71,11 +71,11 @@ namespace NMonitoring { Record(); } - private: + private: TMetric* Metric_{nullptr}; - typename Clock::time_point StartTime_; - }; - + typename Clock::time_point StartTime_; + }; + /** * @brief A class that is supposed to use to measure execution time of an asynchronuous operation. * @@ -124,4 +124,4 @@ namespace NMonitoring { TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { return TFutureFriendlyTimer<TMetric>{metric}; } -} +} diff --git a/library/cpp/monlib/metrics/timer_ut.cpp b/library/cpp/monlib/metrics/timer_ut.cpp index 9827edcda0..c244a8c9e1 100644 --- a/library/cpp/monlib/metrics/timer_ut.cpp +++ b/library/cpp/monlib/metrics/timer_ut.cpp @@ -1,120 +1,120 @@ -#include "timer.h" - +#include "timer.h" + #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/threading/future/async.h> #include <library/cpp/threading/future/future.h> - -using namespace NMonitoring; + +using namespace NMonitoring; using namespace NThreading; - -Y_UNIT_TEST_SUITE(TTimerTest) { - - using namespace std::chrono; - - struct TTestClock { - using time_point = time_point<high_resolution_clock>; - - static time_point TimePoint; - - static time_point now() { - return TimePoint; - } - }; - - TTestClock::time_point TTestClock::TimePoint; - - - Y_UNIT_TEST(Gauge) { - TTestClock::TimePoint = TTestClock::time_point::min(); - - TGauge gauge(0); - { + +Y_UNIT_TEST_SUITE(TTimerTest) { + + using namespace std::chrono; + + struct TTestClock { + using time_point = time_point<high_resolution_clock>; + + static time_point TimePoint; + + static time_point now() { + return TimePoint; + } + }; + + TTestClock::time_point TTestClock::TimePoint; + + + Y_UNIT_TEST(Gauge) { + TTestClock::TimePoint = TTestClock::time_point::min(); + + TGauge gauge(0); + { TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; - TTestClock::TimePoint += milliseconds(10); - } - UNIT_ASSERT_EQUAL(10, gauge.Get()); - - { + TTestClock::TimePoint += milliseconds(10); + } + UNIT_ASSERT_EQUAL(10, gauge.Get()); + + { TMetricTimerScope<TGauge, milliseconds, TTestClock> t{&gauge}; - TTestClock::TimePoint += milliseconds(20); - } - UNIT_ASSERT_EQUAL(20, gauge.Get()); - } - - Y_UNIT_TEST(IntGauge) { - TTestClock::TimePoint = TTestClock::time_point::min(); - - TIntGauge gauge(0); - { + TTestClock::TimePoint += milliseconds(20); + } + UNIT_ASSERT_EQUAL(20, gauge.Get()); + } + + Y_UNIT_TEST(IntGauge) { + TTestClock::TimePoint = TTestClock::time_point::min(); + + TIntGauge gauge(0); + { TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; - TTestClock::TimePoint += milliseconds(10); - } - UNIT_ASSERT_EQUAL(10, gauge.Get()); - - { + TTestClock::TimePoint += milliseconds(10); + } + UNIT_ASSERT_EQUAL(10, gauge.Get()); + + { TMetricTimerScope<TIntGauge, milliseconds, TTestClock> t{&gauge}; - TTestClock::TimePoint += milliseconds(20); - } - UNIT_ASSERT_EQUAL(20, gauge.Get()); - } - - Y_UNIT_TEST(CounterNew) { - TTestClock::TimePoint = TTestClock::time_point::min(); - + TTestClock::TimePoint += milliseconds(20); + } + UNIT_ASSERT_EQUAL(20, gauge.Get()); + } + + Y_UNIT_TEST(CounterNew) { + TTestClock::TimePoint = TTestClock::time_point::min(); + TCounter counter(0); - { + { TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; - TTestClock::TimePoint += milliseconds(10); - } - UNIT_ASSERT_EQUAL(10, counter.Get()); - - { + TTestClock::TimePoint += milliseconds(10); + } + UNIT_ASSERT_EQUAL(10, counter.Get()); + + { TMetricTimerScope<TCounter, milliseconds, TTestClock> t{&counter}; - TTestClock::TimePoint += milliseconds(20); - } - UNIT_ASSERT_EQUAL(30, counter.Get()); - } - - Y_UNIT_TEST(Rate) { - TTestClock::TimePoint = TTestClock::time_point::min(); - - TRate rate(0); - { + TTestClock::TimePoint += milliseconds(20); + } + UNIT_ASSERT_EQUAL(30, counter.Get()); + } + + Y_UNIT_TEST(Rate) { + TTestClock::TimePoint = TTestClock::time_point::min(); + + TRate rate(0); + { TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; - TTestClock::TimePoint += milliseconds(10); - } - UNIT_ASSERT_EQUAL(10, rate.Get()); - - { + TTestClock::TimePoint += milliseconds(10); + } + UNIT_ASSERT_EQUAL(10, rate.Get()); + + { TMetricTimerScope<TRate, milliseconds, TTestClock> t{&rate}; - TTestClock::TimePoint += milliseconds(20); - } - UNIT_ASSERT_EQUAL(30, rate.Get()); - } - - Y_UNIT_TEST(Histogram) { - TTestClock::TimePoint = TTestClock::time_point::min(); - - auto assertHistogram = [](const TVector<ui64>& expected, IHistogramSnapshotPtr snapshot) { - UNIT_ASSERT_EQUAL(expected.size(), snapshot->Count()); - for (size_t i = 0; i < expected.size(); ++i) { - UNIT_ASSERT_EQUAL(expected[i], snapshot->Value(i)); - } - }; - - THistogram histogram(ExplicitHistogram({10, 20, 30}), true); - { + TTestClock::TimePoint += milliseconds(20); + } + UNIT_ASSERT_EQUAL(30, rate.Get()); + } + + Y_UNIT_TEST(Histogram) { + TTestClock::TimePoint = TTestClock::time_point::min(); + + auto assertHistogram = [](const TVector<ui64>& expected, IHistogramSnapshotPtr snapshot) { + UNIT_ASSERT_EQUAL(expected.size(), snapshot->Count()); + for (size_t i = 0; i < expected.size(); ++i) { + UNIT_ASSERT_EQUAL(expected[i], snapshot->Value(i)); + } + }; + + THistogram histogram(ExplicitHistogram({10, 20, 30}), true); + { TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; - TTestClock::TimePoint += milliseconds(5); - } - assertHistogram({1, 0, 0, 0}, histogram.TakeSnapshot()); - - { + TTestClock::TimePoint += milliseconds(5); + } + assertHistogram({1, 0, 0, 0}, histogram.TakeSnapshot()); + + { TMetricTimerScope<THistogram, milliseconds, TTestClock> t{&histogram}; - TTestClock::TimePoint += milliseconds(15); - } - assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot()); - } + TTestClock::TimePoint += milliseconds(15); + } + assertHistogram({1, 1, 0, 0}, histogram.TakeSnapshot()); + } Y_UNIT_TEST(Moving) { TTestClock::TimePoint = TTestClock::time_point::min(); @@ -154,4 +154,4 @@ Y_UNIT_TEST_SUITE(TTimerTest) { UNIT_ASSERT_EQUAL(counter.Get(), 5); } -} +} diff --git a/library/cpp/monlib/metrics/ut/ya.make b/library/cpp/monlib/metrics/ut/ya.make index 698920571f..aec9974fbd 100644 --- a/library/cpp/monlib/metrics/ut/ya.make +++ b/library/cpp/monlib/metrics/ut/ya.make @@ -15,7 +15,7 @@ SRCS( metric_sub_registry_ut.cpp metric_value_ut.cpp summary_collector_ut.cpp - timer_ut.cpp + timer_ut.cpp ) RESOURCE( |