Sunshine master
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
22 boost::format one_digit_after_decimal();
23
29 boost::format two_digits_after_decimal();
30
34 template<typename T>
36 public:
40 using callback_function = std::function<void(T stat_min, T stat_max, double stat_avg)>;
41
49 void collect_and_callback_on_interval(T stat, const callback_function &callback, std::chrono::seconds interval_in_seconds) {
50 if (data.calls == 0) {
51 data.last_callback_time = std::chrono::steady_clock::now();
52 } else if (std::chrono::steady_clock::now() > data.last_callback_time + interval_in_seconds) {
53 callback(data.stat_min, data.stat_max, data.stat_total / data.calls);
54 data = {};
55 }
56 data.stat_min = std::min(data.stat_min, stat);
57 data.stat_max = std::max(data.stat_max, stat);
58 data.stat_total += stat;
59 data.calls += 1;
60 }
61
65 void reset() {
66 data = {};
67 }
68
69 private:
70 struct {
71 std::chrono::steady_clock::time_point last_callback_time = std::chrono::steady_clock::now();
72 T stat_min = std::numeric_limits<T>::max();
73 T stat_max = std::numeric_limits<T>::min();
74 double stat_total = 0;
75 uint32_t calls = 0;
76 } data;
77 };
78
79} // namespace stat_trackers
Accumulates minimum, maximum, and average values between periodic callbacks.
Definition stat_trackers.h:35
std::function< void(T stat_min, T stat_max, double stat_avg)> callback_function
Callback invoked with the minimum, maximum, and average for one interval.
Definition stat_trackers.h:40
void collect_and_callback_on_interval(T stat, const callback_function &callback, std::chrono::seconds interval_in_seconds)
Add one statistic sample and invoke the callback when the interval elapses.
Definition stat_trackers.h:49
void reset()
Reset the object to its initial empty state.
Definition stat_trackers.h:65
boost::format one_digit_after_decimal()
Create a Boost formatter with one fractional digit.
Definition stat_trackers.cpp:13
boost::format two_digits_after_decimal()
Create a Boost formatter with two fractional digits.
Definition stat_trackers.cpp:20