QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
DataRateTracker.cc
Go to the documentation of this file.
1#include "DataRateTracker.h"
2
3#include <utility>
4
6 : _clock(clock ? std::move(clock) : Clock{MonotonicClock::nowUs})
7{
8 _windowStartedUs = _clock();
9}
10
11void DataRateTracker::recordBytes(qsizetype bytes)
12{
13 _windowBytes += bytes;
14 _totalBytes += static_cast<quint64>(bytes);
15
16 refresh();
17}
18
20{
21 const quint64 now = _clock();
22 const quint64 elapsed = now >= _windowStartedUs ? now - _windowStartedUs : 0;
23 if (elapsed >= kWindowUs) {
24 _currentRate = static_cast<double>(_windowBytes) / static_cast<double>(elapsed) * 1000000.0;
25 _windowBytes = 0;
26 _rateUpdated = true;
27 _windowStartedUs = now;
28 } else {
29 _rateUpdated = false;
30 }
31}
32
34{
35 _totalBytes = 0;
36 resetRate();
37}
38
40{
41 _windowBytes = 0;
42 _currentRate = 0.0;
43 _rateUpdated = false;
44 _windowStartedUs = _clock();
45}
void reset()
Reset all counters and restart the rate window.
DataRateTracker(Clock clock=MonotonicClock::nowUs)
std::function< quint64()> Clock
Monotonic microseconds, injectable for deterministic window tests.
void recordBytes(qsizetype bytes)
Record incoming/outgoing bytes. Call whenever data passes through.
void refresh()
Advance the rate window without changing cumulative bytes.
void resetRate()
Zero the rate and restart its window; cumulative bytes are kept.