diff options
author | msherbakov <msherbakov@yandex-team.ru> | 2022-02-10 16:49:16 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:16 +0300 |
commit | c224a621661ddd69699f9476922eb316607ef57e (patch) | |
tree | 33f4d878aa0a9faa964005e06bfab0272313aa71 /library/cpp/monlib/metrics/timer.h | |
parent | 29d0b2eeae154d04156e0698067c0c21a97ea61d (diff) | |
download | ydb-c224a621661ddd69699f9476922eb316607ef57e.tar.gz |
Restoring authorship annotation for <msherbakov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics/timer.h')
-rw-r--r-- | library/cpp/monlib/metrics/timer.h | 114 |
1 files changed, 57 insertions, 57 deletions
diff --git a/library/cpp/monlib/metrics/timer.h b/library/cpp/monlib/metrics/timer.h index 5c4e26e37b..78f82dd448 100644 --- a/library/cpp/monlib/metrics/timer.h +++ b/library/cpp/monlib/metrics/timer.h @@ -26,25 +26,25 @@ namespace NMonitoring { TMetricTimerScope(TMetricTimerScope&) = delete; TMetricTimerScope& operator=(const TMetricTimerScope&) = delete; - + TMetricTimerScope(TMetricTimerScope&& other) { - *this = std::move(other); - } - + *this = std::move(other); + } + TMetricTimerScope& operator=(TMetricTimerScope&& other) { Metric_ = other.Metric_; other.Metric_ = nullptr; - StartTime_ = std::move(other.StartTime_); - - return *this; - } - - void Record() { + StartTime_ = std::move(other.StartTime_); + + return *this; + } + + void Record() { Y_VERIFY_DEBUG(Metric_); if (Metric_ == nullptr) { - return; - } - + return; + } + auto duration = std::chrono::duration_cast<Resolution>(Clock::now() - StartTime_).count(); if constexpr (std::is_same<TMetric, TGauge>::value) { Metric_->Set(duration); @@ -59,69 +59,69 @@ namespace NMonitoring { } else { static_assert(TDependentFalse<TMetric>, "Not supported metric type"); } - + Metric_ = nullptr; } ~TMetricTimerScope() { if (Metric_ == nullptr) { - return; - } - - Record(); - } - + return; + } + + Record(); + } + private: TMetric* Metric_{nullptr}; typename Clock::time_point StartTime_; }; - /** - * @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, + /** + * @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 - * 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. - */ + * 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, - typename Resolution = std::chrono::milliseconds, - typename Clock = std::chrono::high_resolution_clock> - class TFutureFriendlyTimer { - public: + typename Resolution = std::chrono::milliseconds, + typename Clock = std::chrono::high_resolution_clock> + class TFutureFriendlyTimer { + public: explicit TFutureFriendlyTimer(TMetric* metric) : Impl_{metric} - { - } - - TFutureFriendlyTimer(const TFutureFriendlyTimer& other) - : Impl_{std::move(other.Impl_)} - { - } - - TFutureFriendlyTimer& operator=(const TFutureFriendlyTimer& other) { - Impl_ = std::move(other.Impl_); - } - - TFutureFriendlyTimer(TFutureFriendlyTimer&&) = default; - TFutureFriendlyTimer& operator=(TFutureFriendlyTimer&& other) = default; - - void Record() const { - Impl_.Record(); - } - - private: + { + } + + TFutureFriendlyTimer(const TFutureFriendlyTimer& other) + : Impl_{std::move(other.Impl_)} + { + } + + TFutureFriendlyTimer& operator=(const TFutureFriendlyTimer& other) { + Impl_ = std::move(other.Impl_); + } + + TFutureFriendlyTimer(TFutureFriendlyTimer&&) = default; + TFutureFriendlyTimer& operator=(TFutureFriendlyTimer&& other) = default; + + void Record() const { + Impl_.Record(); + } + + private: mutable TMetricTimerScope<TMetric, Resolution, Clock> Impl_; - }; - + }; + template <typename TMetric> TMetricTimerScope<TMetric> ScopeTimer(TMetric* metric) { return TMetricTimerScope<TMetric>{metric}; - } - + } + template <typename TMetric> TFutureFriendlyTimer<TMetric> FutureTimer(TMetric* metric) { return TFutureFriendlyTimer<TMetric>{metric}; - } + } } |