KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
ratemonitor.h
1/**
2 * RateMonitor
3 *
4 * Calculate the rate at which something is happening (e.g. packets-per-second)
5 *
6 * Every time the thing happens, call addEvent().
7 *
8 * Use eventsPerSecond() to obtain the actual rate.
9 *
10 * setEvaluationTime() sets the sliding time window over which the calculation
11 * is performed.
12 *
13 * Stephen Punak, July 08 2019
14 */
15#ifndef RATEMONITOR_H
16#define RATEMONITOR_H
17
18#include <QDateTime>
19#include <QList>
20#include <QMutex>
21#include "kanoopcommon.h"
22
23/**
24 * @brief Tracks event counts over a sliding time window to compute a real-time rate.
25 *
26 * Call addEvent() each time the measured event occurs. Use eventsPerSecond() to
27 * retrieve the computed rate over the configured evaluation window.
28 */
29class KANOOP_EXPORT RateMonitor
30{
31public:
32 /** @brief Construct a RateMonitor with a default 1-second evaluation window. */
34 _evaluationMsecs(1000) {}
35
36 /**
37 * @brief Construct a RateMonitor with a custom evaluation window.
38 * @param evaluationMsecs Sliding window duration in milliseconds
39 */
40 RateMonitor(int evaluationMsecs) :
41 _evaluationMsecs(evaluationMsecs) {}
42
43public slots:
44 /**
45 * @brief Record one or more occurrences of the monitored event.
46 * @param count Number of events to record (default 1)
47 */
48 void addEvent(int count = 1);
49
50public:
51 /**
52 * @brief Compute the current event rate over the evaluation window.
53 * @return Number of events per second
54 */
56
57 /**
58 * @brief Return the arbitrary data payload attached to this monitor.
59 * @return Stored QVariant data
60 */
61 QVariant data() const { return _data; }
62
63 /**
64 * @brief Attach arbitrary data to this monitor for caller use.
65 * @param value Data to store
66 */
67 void setData(QVariant value) { _data = value; }
68
69 /**
70 * @brief Return the evaluation window duration in milliseconds.
71 * @return Evaluation window in milliseconds
72 */
73 int evaluationMsecs() const { return _evaluationMsecs; }
74
75 /**
76 * @brief Set the evaluation window duration.
77 * @param msecs New window duration in milliseconds
78 */
79 void setEvaluationTime(int msecs) { _evaluationMsecs = msecs; }
80
81private:
82 class RateEvent
83 {
84 friend class RateMonitor;
85 public:
86 RateEvent() :
87 _timestamp(0),
88 _count(0) {}
89 RateEvent(quint64 timestamp, qint64 count) :
90 _timestamp(timestamp),
91 _count(count) {}
92 private:
93 qint64 _timestamp;
94 qint64 _count;
95 };
96
97 void pruneOldEvents(qint64 now);
98
99 int _evaluationMsecs = 1000;
100 QVariant _data;
101
102 QList<RateEvent> _events;
103 QMutex _lock;
104 qint64 _lastPruneTime = 0;
105 static const qint64 PruneIntervalMsecs = 1000;
106};
107
108#endif // RATEMONITOR_H
RateMonitor.
Definition ratemonitor.h:30
RateMonitor()
Construct a RateMonitor with a default 1-second evaluation window.
Definition ratemonitor.h:33
double eventsPerSecond()
Compute the current event rate over the evaluation window.
QVariant data() const
Return the arbitrary data payload attached to this monitor.
Definition ratemonitor.h:61
void setEvaluationTime(int msecs)
Set the evaluation window duration.
Definition ratemonitor.h:79
void setData(QVariant value)
Attach arbitrary data to this monitor for caller use.
Definition ratemonitor.h:67
RateMonitor(int evaluationMsecs)
Construct a RateMonitor with a custom evaluation window.
Definition ratemonitor.h:40
int evaluationMsecs() const
Return the evaluation window duration in milliseconds.
Definition ratemonitor.h:73
void addEvent(int count=1)
Record one or more occurrences of the monitored event.