14#include <unordered_map>
41 template<
class AClass>
42 void log(
const std::string& key,
const AClass& value);
48 template<
size_t MAX_SIZE = 100>
49 void average(
const std::string& key,
double value);
51 template<
size_t AVERAGE_SIZE = 2000>
52 void timeit(
const std::string& key);
54 void logTime(
const std::string& key);
58 std::map<std::string, std::variant<long, double, std::string>>
m_monitoring;
60 std::unordered_map<std::string, std::tuple<std::vector<double>,
size_t>>
m_averages;
62 std::unordered_map<std::string, std::tuple<unsigned long, std::chrono::system_clock::time_point>>
m_timeCounters;
71 std::string
operator()(
const std::string& value);
95 template<
class AClass>
101 template<
size_t MAX_SIZE>
106 if (averages.size() == MAX_SIZE) {
107 averages[index] = value;
108 index = (index + 1) % MAX_SIZE;
110 averages.push_back(value);
113 log(key, std::accumulate(averages.begin(), averages.end(), 0.0) / averages.size());
116 template<
size_t AVERAGE_SIZE>
120 if (calls % AVERAGE_SIZE == 0) {
121 auto current = std::chrono::high_resolution_clock::now();
122 double elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(
123 current - timestamp).count();
125 log(key, AVERAGE_SIZE / elapsed);
130 auto displayTime = std::chrono::system_clock::to_time_t(current);
131 log(key +
"_last_measurement", std::ctime(&displayTime));
Base class for the ZMQ connection to help monitor and log certain values.
void decrement(const std::string &key)
Decrement the value with the given key (only numerical values). If not present, set to -1.
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
void logTime(const std::string &key)
Store the current time as a string under the given key.
void increment(const std::string &key)
Increment the value with the given key (only numerical values). If not present, set to 1.
std::unordered_map< std::string, std::tuple< std::vector< double >, size_t > > m_averages
Internal storage of the previous values when calculating averages.
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
std::unordered_map< std::string, std::tuple< unsigned long, std::chrono::system_clock::time_point > > m_timeCounters
Internal storage how often the timeit function for a given key was called and when it has last reache...
void timeit(const std::string &key)
Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time ...
void log(const std::string &key, const AClass &value)
Store a value under a certain key. Different types of values can be stored, namely long,...
void average(const std::string &key, double value)
Instead of storeing the double value directly under the given key, store the average of the last MAX_...
Abstract base class for different kinds of events.
Visitor Helper for decrementing a numerical value.
void operator()(long &value)
-1 for longs
Visitor Helper for incrementing a numerical value.
void operator()(long &value)
+1 for longs
Visitor Helper for converting a variant value into a JSON string.
std::string operator()(long value)
just stringify longs