userver: userver/tracing/scope_time.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
scope_time.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/tracing/scope_time.hpp
4/// @brief @copybrief tracing::ScopeTime
5
6#include <atomic>
7#include <chrono>
8#include <string>
9
10#include <userver/logging/log_extra.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace tracing {
15
16namespace impl {
17
18class TimeStorage;
19
20} // namespace impl
21
22/// @brief Type to measure execution time of a scope
23///
24/// Use tracing::Span::CreateScopeTime() to construct
25class ScopeTime {
26 public:
27 using Duration = std::chrono::nanoseconds;
28 using DurationMillis = std::chrono::duration<double, std::milli>;
29
30 /// @brief Creates a tracing::ScopeTime attached to
31 /// tracing::Span::CurrentSpan().
32 ///
33 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime()
35
36 /// @brief Creates a tracing::ScopeTime attached to
37 /// tracing::Span::CurrentSpan() and starts measuring execution time.
38 ///
39 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime(scope_name)
40 explicit ScopeTime(std::string scope_name);
41
42 /// @cond
43 // Constructors for internal use
44 explicit ScopeTime(impl::TimeStorage& ts);
45 ScopeTime(impl::TimeStorage& ts, std::string scope_name);
46 /// @endcond
47
48 ScopeTime(const ScopeTime&) = delete;
49 ScopeTime(ScopeTime&&) = default;
50 ~ScopeTime();
51
52 /// Records the current scope time if the name is set, and stops the timer
53 Duration Reset();
54
55 /// Records the current scope time if the name is set, and starts a new one
56 Duration Reset(std::string scope_name);
57
58 /// Stops the timer without recording its value
59 void Discard();
60
61 /// Returns time elapsed since last reset
62 /// Will return 0 if the timer is stopped
63 Duration DurationSinceReset() const;
64
65 /// Returns total time elapsed for a certain scope. If there is no record for
66 /// the scope, returns 0
67 Duration DurationTotal(const std::string& scope_name) const;
68
69 /// Returns total time elapsed for current scope
70 /// Will return 0 if the timer is stopped
71 Duration DurationTotal() const;
72
73 /// Returns time elapsed since last reset, returns 0 if the timer is stopped.
74 ///
75 /// Prefer using ScopeTime::DurationSinceReset()
76 DurationMillis ElapsedSinceReset() const;
77
78 /// Returns total time elapsed for a certain scope. If there is no record for
79 /// the scope, returns 0.
80 ///
81 /// Prefer using ScopeTime::DurationTotal()
82 DurationMillis ElapsedTotal(const std::string& scope_name) const;
83
84 /// Returns total time elapsed for current scope
85 /// Will return 0 if the timer is stopped.
86 ///
87 /// Prefer using ScopeTime::DurationTotal()
88 DurationMillis ElapsedTotal() const;
89
90 const std::string& CurrentScope() const { return scope_name_; }
91
92 private:
93 impl::TimeStorage& ts_;
94 std::chrono::steady_clock::time_point start_;
95 std::string scope_name_;
96};
97
98} // namespace tracing
99
100USERVER_NAMESPACE_END