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
5
6#include <atomic>
7#include <chrono>
8#include <optional>
9#include <string>
10
12
13USERVER_NAMESPACE_BEGIN
14
15namespace tracing {
16
17namespace impl {
18
19class TimeStorage;
20
21} // namespace impl
22
26class ScopeTime {
27public:
28 using Duration = std::chrono::nanoseconds;
29 using DurationMillis = std::chrono::duration<double, std::milli>;
30
36
45 explicit ScopeTime(std::string scope_name);
46
50 static std::optional<ScopeTime> CreateOptionalScopeTime();
51
55 static std::optional<ScopeTime> CreateOptionalScopeTime(std::string_view name);
56
58 // Constructors for internal use
59 explicit ScopeTime(impl::TimeStorage& ts);
60 ScopeTime(impl::TimeStorage& ts, std::string scope_name);
62
63 ScopeTime(const ScopeTime&) = delete;
64 ScopeTime(ScopeTime&&) = default;
65 ~ScopeTime();
66
68 Duration Reset();
69
71 Duration Reset(std::string scope_name);
72
74 void Discard();
75
78 Duration DurationSinceReset() const;
79
82 Duration DurationTotal(std::string_view scope_name) const;
83
86 Duration DurationTotal() const;
87
91 DurationMillis ElapsedSinceReset() const;
92
97 DurationMillis ElapsedTotal(std::string_view scope_name) const;
98
103 DurationMillis ElapsedTotal() const;
104
105 const std::string& CurrentScope() const { return scope_name_; }
106
107private:
108 impl::TimeStorage& ts_;
109 std::chrono::steady_clock::time_point start_;
110 std::string scope_name_;
111};
112
113} // namespace tracing
114
115USERVER_NAMESPACE_END