diff options
author | Sergey Polovko <sergey@polovko.me> | 2022-02-10 16:47:02 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:02 +0300 |
commit | 3e0b762a82514bac89c1dd6ea7211e381d8aa248 (patch) | |
tree | c2d1b379ecaf05ca8f11ed0b5da9d1a950e6e554 /library/cpp/monlib/metrics/timer.h | |
parent | ab3783171cc30e262243a0227c86118f7080c896 (diff) | |
download | ydb-3e0b762a82514bac89c1dd6ea7211e381d8aa248.tar.gz |
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics/timer.h')
-rw-r--r-- | library/cpp/monlib/metrics/timer.h | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/library/cpp/monlib/metrics/timer.h b/library/cpp/monlib/metrics/timer.h index 5c4e26e37b..35d7824fac 100644 --- a/library/cpp/monlib/metrics/timer.h +++ b/library/cpp/monlib/metrics/timer.h @@ -1,6 +1,6 @@ #pragma once -#include "metric.h" +#include "metric.h" #include <util/generic/typetraits.h> @@ -12,59 +12,59 @@ namespace NMonitoring { /** * A timing scope to record elapsed time since creation. */ - template <typename TMetric, + template <typename TMetric, typename Resolution = std::chrono::milliseconds, typename Clock = std::chrono::high_resolution_clock> - class TMetricTimerScope { + class TMetricTimerScope { public: - explicit TMetricTimerScope(TMetric* metric) - : Metric_(metric) + explicit TMetricTimerScope(TMetric* metric) + : Metric_(metric) , StartTime_(Clock::now()) { - Y_ENSURE(Metric_); + Y_ENSURE(Metric_); } - TMetricTimerScope(TMetricTimerScope&) = delete; - TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; + TMetricTimerScope(TMetricTimerScope&) = delete; + TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; - TMetricTimerScope(TMetricTimerScope&& other) { + TMetricTimerScope(TMetricTimerScope&& other) { *this = std::move(other); } - TMetricTimerScope& operator=(TMetricTimerScope&& other) { - Metric_ = other.Metric_; - other.Metric_ = nullptr; + TMetricTimerScope& operator=(TMetricTimerScope&& other) { + Metric_ = other.Metric_; + other.Metric_ = nullptr; StartTime_ = std::move(other.StartTime_); return *this; } void Record() { - Y_VERIFY_DEBUG(Metric_); - if (Metric_ == nullptr) { + Y_VERIFY_DEBUG(Metric_); + if (Metric_ == nullptr) { return; } 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) { - Metric_->Set(duration); - } else if constexpr (std::is_same<TMetric, TCounter>::value) { - Metric_->Add(duration); - } else if constexpr (std::is_same<TMetric, TRate>::value) { - Metric_->Add(duration); - } else if constexpr (std::is_same<TMetric, THistogram>::value) { - Metric_->Record(duration); + if constexpr (std::is_same<TMetric, TGauge>::value) { + Metric_->Set(duration); + } else if constexpr (std::is_same<TMetric, TIntGauge>::value) { + Metric_->Set(duration); + } else if constexpr (std::is_same<TMetric, TCounter>::value) { + Metric_->Add(duration); + } else if constexpr (std::is_same<TMetric, TRate>::value) { + Metric_->Add(duration); + } else if constexpr (std::is_same<TMetric, THistogram>::value) { + Metric_->Record(duration); } else { static_assert(TDependentFalse<TMetric>, "Not supported metric type"); } - Metric_ = nullptr; + Metric_ = nullptr; } - ~TMetricTimerScope() { - if (Metric_ == nullptr) { + ~TMetricTimerScope() { + if (Metric_ == nullptr) { return; } @@ -72,7 +72,7 @@ namespace NMonitoring { } private: - TMetric* Metric_{nullptr}; + TMetric* Metric_{nullptr}; typename Clock::time_point StartTime_; }; @@ -80,18 +80,18 @@ namespace NMonitoring { * @brief A class that is supposed to use to measure execution time of an asynchronuous operation. * * In order to be able to capture an object into a lambda which is then passed to TFuture::Subscribe/Apply, - * the object must be copy constructible (limitation of the std::function class). So, we cannot use the TMetricTimerScope + * the object must be copy constructible (limitation of the std::function class). So, we cannot use the TMetricTimerScope * with the abovementioned functions without storing it in a shared pointer or somewhere else. This class works around this * issue with wrapping the timer with a auto_ptr-like hack Also, Record is const so that one doesn't need to make every lambda mutable * just to record time measurement. */ - template <typename TMetric, + template <typename TMetric, typename Resolution = std::chrono::milliseconds, typename Clock = std::chrono::high_resolution_clock> class TFutureFriendlyTimer { public: - explicit TFutureFriendlyTimer(TMetric* metric) - : Impl_{metric} + explicit TFutureFriendlyTimer(TMetric* metric) + : Impl_{metric} { } @@ -112,16 +112,16 @@ namespace NMonitoring { } private: - mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; + mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; }; - template <typename TMetric> - TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { - return TMetricTimerScope<TMetric>{metric}; + template <typename TMetric> + TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { + return TMetricTimerScope<TMetric>{metric}; } - template <typename TMetric> - TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { - return TFutureFriendlyTimer<TMetric>{metric}; + template <typename TMetric> + TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { + return TFutureFriendlyTimer<TMetric>{metric}; } } |