userver: userver/tracing/scope_time.hpp Source File
Loading...
Searching...
No Matches
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 <optional>
9#include <string>
10
11#include <userver/logging/log_extra.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace tracing {
16
17namespace impl {
18
19class TimeStorage;
20
21} // namespace impl
22
23/// @brief Type to measure execution time of a scope
24///
25/// Use tracing::Span::CreateScopeTime() to construct
26class ScopeTime {
27 public:
28 using Duration = std::chrono::nanoseconds;
29 using DurationMillis = std::chrono::duration<double, std::milli>;
30
31 /// @brief Creates a tracing::ScopeTime attached to
32 /// tracing::Span::CurrentSpan().
33 ///
34 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime()
36
37 /// @brief Creates a tracing::ScopeTime attached to
38 /// tracing::Span::CurrentSpan() and starts measuring execution time.
39 ///
40 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime(scope_name)
41 explicit ScopeTime(std::string scope_name);
42
43 /// @brief If there exists a tracing::Span::CurrentSpan(),
44 /// Creates a tracing::ScopeTime attached to that Span,
45 /// otherwise return std::nullopt.
46 static std::optional<ScopeTime> CreateOptionalScopeTime();
47
48 /// @brief If there exists a tracing::Span::CurrentSpan(),
49 /// Creates a tracing::ScopeTime attached to that Span and starts measuring
50 /// execution time, otherwise return std::nullopt.
51 static std::optional<ScopeTime> CreateOptionalScopeTime(
52 std::string_view name);
53
54 /// @cond
55 // Constructors for internal use
56 explicit ScopeTime(impl::TimeStorage& ts);
57 ScopeTime(impl::TimeStorage& ts, std::string scope_name);
58 /// @endcond
59
60 ScopeTime(const ScopeTime&) = delete;
61 ScopeTime(ScopeTime&&) = default;
62 ~ScopeTime();
63
64 /// Records the current scope time if the name is set, and stops the timer
65 Duration Reset();
66
67 /// Records the current scope time if the name is set, and starts a new one
68 Duration Reset(std::string scope_name);
69
70 /// Stops the timer without recording its value
71 void Discard();
72
73 /// Returns time elapsed since last reset
74 /// Will return 0 if the timer is stopped
75 Duration DurationSinceReset() const;
76
77 /// Returns total time elapsed for a certain scope. If there is no record for
78 /// the scope, returns 0
79 Duration DurationTotal(const std::string& scope_name) const;
80
81 /// Returns total time elapsed for current scope
82 /// Will return 0 if the timer is stopped
83 Duration DurationTotal() const;
84
85 /// Returns time elapsed since last reset, returns 0 if the timer is stopped.
86 ///
87 /// Prefer using ScopeTime::DurationSinceReset()
88 DurationMillis ElapsedSinceReset() const;
89
90 /// Returns total time elapsed for a certain scope. If there is no record for
91 /// the scope, returns 0.
92 ///
93 /// Prefer using ScopeTime::DurationTotal()
94 DurationMillis ElapsedTotal(const std::string& scope_name) const;
95
96 /// Returns total time elapsed for current scope
97 /// Will return 0 if the timer is stopped.
98 ///
99 /// Prefer using ScopeTime::DurationTotal()
100 DurationMillis ElapsedTotal() const;
101
102 const std::string& CurrentScope() const { return scope_name_; }
103
104 private:
105 impl::TimeStorage& ts_;
106 std::chrono::steady_clock::time_point start_;
107 std::string scope_name_;
108};
109
110} // namespace tracing
111
112USERVER_NAMESPACE_END