WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
profiler.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <chrono>
6#include <concepts>
7#include <string>
8#include <string_view>
9#include <utility>
10
11namespace slp {
12
13/// Records the number of profiler measurements (start/stop pairs) and the
14/// average duration between each start and stop call.
16 public:
17 /// Constructs a SetupProfiler.
18 ///
19 /// @param name Name of measurement to show in diagnostics.
20 explicit SetupProfiler(std::string_view name) : m_name{name} {}
21
22 /// Starts setup time measurement.
23 void start() {
24#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
25 m_setup_start_time = std::chrono::steady_clock::now();
26#endif
27 }
28
29 /// Stops setup time measurement.
30 void stop() {
31#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
32 m_setup_stop_time = std::chrono::steady_clock::now();
33 m_setup_duration = m_setup_stop_time - m_setup_start_time;
34#endif
35 }
36
37 /// Returns name of measurement to show in diagnostics.
38 ///
39 /// @return Name of measurement to show in diagnostics.
40 std::string_view name() const { return m_name; }
41
42 /// Returns the setup duration in milliseconds as a double.
43 ///
44 /// @return The setup duration in milliseconds as a double.
45 const std::chrono::duration<double>& duration() const {
46 return m_setup_duration;
47 }
48
49 private:
50 /// Name of measurement to show in diagnostics.
51 std::string m_name;
52
53 std::chrono::steady_clock::time_point m_setup_start_time;
54 std::chrono::steady_clock::time_point m_setup_stop_time;
55 std::chrono::duration<double> m_setup_duration{0.0};
56};
57
58/// Records the number of profiler measurements (start/stop pairs) and the
59/// average duration between each start and stop call.
61 public:
62 /// Constructs a SolveProfiler.
63 ///
64 /// @param name Name of measurement to show in diagnostics.
65 explicit SolveProfiler(std::string_view name) : m_name{name} {}
66
67 /// Starts solve time measurement.
68 void start() {
69#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
70 m_current_solve_start_time = std::chrono::steady_clock::now();
71#endif
72 }
73
74 /// Stops solve time measurement, increments the number of averages, and
75 /// incorporates the latest measurement into the average.
76 void stop() {
77#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
78 m_current_solve_stop_time = std::chrono::steady_clock::now();
79 m_current_solve_duration =
80 m_current_solve_stop_time - m_current_solve_start_time;
81 m_total_solve_duration += m_current_solve_duration;
82
83 ++m_num_solves;
84 m_average_solve_duration =
85 (m_num_solves - 1.0) / m_num_solves * m_average_solve_duration +
86 1.0 / m_num_solves * m_current_solve_duration;
87#endif
88 }
89
90 /// Returns name of measurement to show in diagnostics.
91 ///
92 /// @return Name of measurement to show in diagnostics.
93 std::string_view name() const { return m_name; }
94
95 /// Returns the number of solves.
96 ///
97 /// @return The number of solves.
98 int num_solves() const { return m_num_solves; }
99
100 /// Returns the most recent solve duration in seconds.
101 ///
102 /// @return The most recent solve duration in seconds.
103 const std::chrono::duration<double>& current_duration() const {
104 return m_current_solve_duration;
105 }
106
107 /// Returns the average solve duration in seconds.
108 ///
109 /// @return The average solve duration in seconds.
110 const std::chrono::duration<double>& average_duration() const {
111 return m_average_solve_duration;
112 }
113
114 /// Returns the sum of all solve durations in seconds.
115 ///
116 /// @return The sum of all solve durations in seconds.
117 const std::chrono::duration<double>& total_duration() const {
118 return m_total_solve_duration;
119 }
120
121 private:
122 /// Name of measurement to show in diagnostics.
123 std::string m_name;
124
125 std::chrono::steady_clock::time_point m_current_solve_start_time;
126 std::chrono::steady_clock::time_point m_current_solve_stop_time;
127 std::chrono::duration<double> m_current_solve_duration{0.0};
128 std::chrono::duration<double> m_total_solve_duration{0.0};
129
130 int m_num_solves = 0;
131 std::chrono::duration<double> m_average_solve_duration{0.0};
132};
133
134/// Starts a profiler in the constructor and stops it in the destructor.
135template <typename Profiler>
136 requires std::same_as<Profiler, SetupProfiler> ||
137 std::same_as<Profiler, SolveProfiler>
139 public:
140 /// Starts a profiler.
141 ///
142 /// @param profiler The profiler.
143 explicit ScopedProfiler(Profiler& profiler) noexcept : m_profiler{&profiler} {
144 m_profiler->start();
145 }
146
147 /// Stops a profiler.
149 if (m_active) {
150 m_profiler->stop();
151 }
152 }
153
154 /// Move constructor.
155 ///
156 /// @param rhs The other ScopedProfiler.
158 : m_profiler{std::move(rhs.m_profiler)}, m_active{rhs.m_active} {
159 rhs.m_active = false;
160 }
161
164
165 /// Stops the profiler.
166 ///
167 /// If this is called, the destructor is a no-op.
168 void stop() {
169 if (m_active) {
170 m_profiler->stop();
171 m_active = false;
172 }
173 }
174
175 /// Returns the most recent solve duration in milliseconds as a double.
176 ///
177 /// @return The most recent solve duration in milliseconds as a double.
178 const std::chrono::duration<double>& current_duration() const {
179 return m_profiler->current_duration();
180 }
181
182 private:
183 Profiler* m_profiler;
184 bool m_active = true;
185};
186
187} // namespace slp
ScopedProfiler & operator=(const ScopedProfiler &)=delete
ScopedProfiler(ScopedProfiler &&rhs) noexcept
Move constructor.
Definition profiler.hpp:157
void stop()
Stops the profiler.
Definition profiler.hpp:168
~ScopedProfiler()
Stops a profiler.
Definition profiler.hpp:148
ScopedProfiler(Profiler &profiler) noexcept
Starts a profiler.
Definition profiler.hpp:143
ScopedProfiler(const ScopedProfiler &)=delete
const std::chrono::duration< double > & current_duration() const
Returns the most recent solve duration in milliseconds as a double.
Definition profiler.hpp:178
const std::chrono::duration< double > & duration() const
Returns the setup duration in milliseconds as a double.
Definition profiler.hpp:45
SetupProfiler(std::string_view name)
Constructs a SetupProfiler.
Definition profiler.hpp:20
std::string_view name() const
Returns name of measurement to show in diagnostics.
Definition profiler.hpp:40
void stop()
Stops setup time measurement.
Definition profiler.hpp:30
void start()
Starts setup time measurement.
Definition profiler.hpp:23
const std::chrono::duration< double > & average_duration() const
Returns the average solve duration in seconds.
Definition profiler.hpp:110
const std::chrono::duration< double > & current_duration() const
Returns the most recent solve duration in seconds.
Definition profiler.hpp:103
SolveProfiler(std::string_view name)
Constructs a SolveProfiler.
Definition profiler.hpp:65
const std::chrono::duration< double > & total_duration() const
Returns the sum of all solve durations in seconds.
Definition profiler.hpp:117
std::string_view name() const
Returns name of measurement to show in diagnostics.
Definition profiler.hpp:93
void start()
Starts solve time measurement.
Definition profiler.hpp:68
int num_solves() const
Returns the number of solves.
Definition profiler.hpp:98
void stop()
Stops solve time measurement, increments the number of averages, and incorporates the latest measurem...
Definition profiler.hpp:76
Definition to_underlying.hpp:7