forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driftcounter.cpp
46 lines (35 loc) · 1.42 KB
/
driftcounter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "driftcounter.h"
#include <algorithm>
#include <numeric>
#include "settings.h"
DriftCounter::DriftCounter()
{
}
void DriftCounter::update(std::chrono::time_point<std::chrono::steady_clock> call_time)
{
auto now = std::chrono::steady_clock::now();
std::chrono::milliseconds diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - call_time);
diff = std::max<std::chrono::milliseconds>(diff, std::chrono::milliseconds(0));
many_drifts.at(many_index++ & (many_drifts.size() - 1)) = diff;
last_update = now;
last_drift = diff;
}
std::chrono::milliseconds DriftCounter::getDrift() const
{
auto now = std::chrono::steady_clock::now();
const auto last_update_copy = last_update;
if (last_update_copy + (std::chrono::milliseconds(HEARTBEAT_INTERVAL) * 2 ) < now)
return std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update_copy);
return last_drift;
}
/**
* @brief DriftCounter::getAvgDrift returns the averate of the last x updates. It's not thread safe, so you may
* average updates from different calls to update().
* @return
*/
std::chrono::milliseconds DriftCounter::getAvgDrift() const
{
const double total = std::accumulate(many_drifts.begin(), many_drifts.end(), std::chrono::milliseconds(0)).count();
std::chrono::milliseconds avg = std::chrono::milliseconds(static_cast<int>(total / static_cast<double>(many_drifts.size())));
return avg;
}