mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
measurer.hpp
Go to the documentation of this file.
1#pragma once
2#include "debug.hpp"
3#include "timer.hpp"
9namespace mal_toolkit
10{
15 template <class clock>
16 struct Measurer
17 {
18 public:
23 struct Entry
24 {
26 float elapsed;
27 };
28
36#ifndef ENGINE_NO_SOURCE_LOCATION
37 Measurer(std::string_view s = "Measurer", bool log_automatically = true, float time_to_flush = 30,
38 std::source_location location = std::source_location::current())
40 {
41 output = CurrentSourceLocation(location) + std::basic_string(s) + " ";
42 }
43#else
44 Measurer(std::string_view s = "Measurer", bool log_automatically = true, float time_to_flush = 30)
46 {
47 output = s;
48 }
49#endif
50
54 void begin() { measure.reset_to_now(); }
55
60 float end()
61 {
62 const float t = measure.elapsed();
63 entries.push_back({ entry_time.elapsed(), measure.elapsed() });
64 if (log_automatically && flush.elapsed() > time_to_flush)
65 {
66 log();
67 flush.reset();
68 }
69 return t;
70 }
71
75 void log()
76 {
77 const size_t entries_amount = entries.size() - index;
78 index = entries.size() - 1;
79
80 const float average = avg();
81 const float avg_over_the_flush = avg(entries_amount);
82
83 std::stringstream out;
84 out << output << std::endl;
85 out << "Amount of calls over the last " << std::setprecision(3) << flush.elapsed() << " seconds: ";
86 out << entries_amount << std::endl;
87 out << "Average % of time the function took over the last ";
88 out << std::setprecision(3) << flush.elapsed() << " seconds: ";
89 out << std::setprecision(7) << avg_over_the_flush * entries_amount / flush.elapsed() * 100;
90 out << std::endl;
91 out << "Average time the function took over " << entries.size() << " calls: ";
92 out << std::setprecision(7) << average * 1000 << " milliseconds" << std::endl;
93 out << "Average time the function took over the last " << entries_amount << " calls: ";
94 out << std::setprecision(7) << avg_over_the_flush * 1000 << " milliseconds" << std::endl;
95
96 spdlog::info(out.str());
97 }
98
104 float avg(const size_t last_n_entries = std::numeric_limits<size_t>::max())
105 {
106 float rv = 0;
107 size_t counter = 0;
108 for (int64_t i = static_cast<int64_t>(entries.size()) - 1; counter < last_n_entries && i >= 0;
109 ++counter, --i)
110 {
111 rv += entries[i].elapsed;
112 }
113 return rv / counter;
114 }
115
121 float avg_over_the_last(float seconds)
122 {
123 float rv = 0;
124 int64_t i = static_cast<int64_t>(entries.size()) - 1;
125 for (; i >= 0; --i)
126 {
127 rv += entries[i].elapsed;
128 if (entries[i].entry_time < entry_time.elapsed() - seconds)
129 {
130 break;
131 }
132 }
133 return rv / seconds;
134 }
135
141 float avg_over_the_last_limited(const float seconds)
142 {
143 float rv = 0;
144 int64_t i = static_cast<int64_t>(entries.size()) - 1;
145 for (; i >= 0; --i)
146 {
147 rv += entries[i].elapsed;
148 if (entries[i].entry_time < entry_time.elapsed() - seconds)
149 {
150 break;
151 }
152 }
153 if (i < 0 && entries.size() > 0)
154 {
155 return rv / entries.back().entry_time;
156 }
157 return rv / seconds;
158 }
159
165 uint64_t amount_of_calls(float seconds)
166 {
167 uint64_t rv = 0;
168 int64_t i = int64_t(entries.size()) - 1;
169 for (; i >= 0; --i)
170 {
171 ++rv;
172 if (entries[i].entry_time < entry_time.elapsed() - seconds)
173 {
174 break;
175 }
176 }
177 return rv;
178 }
179
184 float elapsed() { return entry_time.elapsed(); }
185
186 float time_to_flush = 30;
187 size_t maximum_entries = std::numeric_limits<size_t>::max();
188 bool log_automatically = true;
189
190 private:
191 std::string output;
192 std::vector<Entry> entries;
193 size_t index = 0;
197 };
198} // namespace mal_toolkit
A simple timer class for measuring elapsed time using various clock types.
Definition timer.hpp:17
Contains debugging-related macros and utilities for assisting with debugging tasks.
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition backoffs.hpp:7
std::string CurrentSourceLocation()
Returns an empty string representing the current source location.
Definition measurer.hpp:24
float entry_time
Definition measurer.hpp:25
float elapsed
Definition measurer.hpp:26
A class for measuring and logging the execution time of functions.
Definition measurer.hpp:17
std::string output
The string identifier for the measurer.
Definition measurer.hpp:191
float time_to_flush
Interval in seconds for automatic log flushing.
Definition measurer.hpp:186
float end()
End measurement and log the result.
Definition measurer.hpp:60
Timer< clock > entry_time
Timer for tracking entry times.
Definition measurer.hpp:196
Timer< clock > flush
Timer for automatic log flushing.
Definition measurer.hpp:194
float elapsed()
Get the total elapsed time since the start of the measurement.
Definition measurer.hpp:184
float avg_over_the_last_limited(const float seconds)
Calculate the average execution time over a specific time interval with a limit.
Definition measurer.hpp:141
Measurer(std::string_view s="Measurer", bool log_automatically=true, float time_to_flush=30, std::source_location location=std::source_location::current())
Construct a Measurer object.
Definition measurer.hpp:37
Timer< clock > measure
Timer for measuring execution time.
Definition measurer.hpp:195
uint64_t amount_of_calls(float seconds)
Calculate the amount of function calls within a specific time interval.
Definition measurer.hpp:165
bool log_automatically
Determines if measurements are logged automatically.
Definition measurer.hpp:188
float avg_over_the_last(float seconds)
Calculate the average execution time over a specific time interval.
Definition measurer.hpp:121
float avg(const size_t last_n_entries=std::numeric_limits< size_t >::max())
Calculate the average execution time of the last N entries.
Definition measurer.hpp:104
std::vector< Entry > entries
A vector of measurement entries.
Definition measurer.hpp:192
size_t maximum_entries
Maximum number of measurement entries.
Definition measurer.hpp:187
void begin()
Start measuring execution time.
Definition measurer.hpp:54
size_t index
The current index in the entries vector.
Definition measurer.hpp:193
void log()
Log the accumulated measurements.
Definition measurer.hpp:75
Implements timer utilities for measuring elapsed time and intervals.