Sunshine latest
Self-hosted game stream host for Moonlight.
stat_trackers.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <chrono>
9#include <functional>
10#include <limits>
11
12// lib includes
13#include <boost/format.hpp>
14
15namespace stat_trackers {
16
17 boost::format one_digit_after_decimal();
18
19 boost::format two_digits_after_decimal();
20
21 template<typename T>
23 public:
24 using callback_function = std::function<void(T stat_min, T stat_max, double stat_avg)>;
25
26 void collect_and_callback_on_interval(T stat, const callback_function &callback, std::chrono::seconds interval_in_seconds) {
27 if (data.calls == 0) {
28 data.last_callback_time = std::chrono::steady_clock::now();
29 } else if (std::chrono::steady_clock::now() > data.last_callback_time + interval_in_seconds) {
30 callback(data.stat_min, data.stat_max, data.stat_total / data.calls);
31 data = {};
32 }
33 data.stat_min = std::min(data.stat_min, stat);
34 data.stat_max = std::max(data.stat_max, stat);
35 data.stat_total += stat;
36 data.calls += 1;
37 }
38
39 void reset() {
40 data = {};
41 }
42
43 private:
44 struct {
45 std::chrono::steady_clock::time_point last_callback_time = std::chrono::steady_clock::now();
46 T stat_min = std::numeric_limits<T>::max();
47 T stat_max = std::numeric_limits<T>::min();
48 double stat_total = 0;
49 uint32_t calls = 0;
50 } data;
51 };
52
53} // namespace stat_trackers
Definition stat_trackers.h:22