R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
lagometer.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/net/rtt.hpp>
5#include <aegis2d/net/traffic.hpp>
6
8
9#include <array>
10#include <chrono>
11#include <cstddef>
12#include <cstdint>
13#include <deque>
14#include <optional>
15#include <string>
16#include <vector>
17
18namespace rtype {
19
21enum class LagGraph : std::uint8_t {
23 Rtt,
25 Jitter,
27 Loss,
31 BytesIn,
36};
37
39inline constexpr std::size_t lag_graph_count = 7;
40
42enum class LagLevel : std::uint8_t {
44 Good,
46 Warning,
48 Bad,
49};
50
52struct LagSample {
54 std::array<double, lag_graph_count> values{};
55
59 [[nodiscard]] double value(LagGraph graph) const noexcept {
60 return values.at(static_cast<std::size_t>(graph));
61 }
62};
63
70[[nodiscard]] LagSample measure_lag(const aegis2d::net::RttEstimator& rtt,
71 const aegis2d::net::TrafficCounters& traffic,
72 const NetworkStats& stats,
73 std::chrono::steady_clock::time_point now);
74
80[[nodiscard]] LagLevel lag_level(LagGraph graph, double value) noexcept;
81
86[[nodiscard]] std::string lag_label(LagGraph graph, double value);
87
93[[nodiscard]] std::vector<std::string> lag_lines(const LagSample& sample);
94
98class Lagometer {
99public:
101 using Clock = std::chrono::steady_clock;
103 static constexpr std::chrono::milliseconds sample_interval{100};
105 static constexpr std::size_t length = 100;
106
114 void update(const aegis2d::net::RttEstimator& rtt, const aegis2d::net::TrafficCounters& traffic,
115 const NetworkStats& stats, Clock::time_point now, std::uint64_t corrections = 0);
116
119 void add(const LagSample& sample);
120
122 void clear() noexcept;
123
126 [[nodiscard]] const std::deque<LagSample>& samples() const noexcept;
127
130 [[nodiscard]] LagSample latest() const noexcept;
131
136 [[nodiscard]] double scale(LagGraph graph) const noexcept;
137
138private:
139 std::deque<LagSample> samples_;
140 Clock::time_point next_{};
141 // Corrections and time of the last sample, for the corrections per second.
142 std::uint64_t corrections_ = 0;
143 std::optional<Clock::time_point> sampled_;
144};
145
146} // namespace rtype
The last seconds of the network statistics, for the graphs of the lagometer.
Definition lagometer.hpp:98
static constexpr std::chrono::milliseconds sample_interval
Time between two samples.
Definition lagometer.hpp:103
void clear() noexcept
Forgets every sample, for a new session.
LagSample latest() const noexcept
The last sample.
static constexpr std::size_t length
Samples kept: the last 10 seconds.
Definition lagometer.hpp:105
double scale(LagGraph graph) const noexcept
Top of a graph: its largest value kept, but never below a floor (100 ms for the RTT,...
std::chrono::steady_clock Clock
Clock of the samples.
Definition lagometer.hpp:101
void add(const LagSample &sample)
Adds a sample, forgetting the oldest beyond length.
const std::deque< LagSample > & samples() const noexcept
The samples kept.
void update(const aegis2d::net::RttEstimator &rtt, const aegis2d::net::TrafficCounters &traffic, const NetworkStats &stats, Clock::time_point now, std::uint64_t corrections=0)
Takes a sample of the statistics if sample_interval went by since the last one.
Measures the loss and the jitter of the server's datagrams over a sliding window.
Definition network_stats.hpp:35
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
LagSample measure_lag(const aegis2d::net::RttEstimator &rtt, const aegis2d::net::TrafficCounters &traffic, const NetworkStats &stats, std::chrono::steady_clock::time_point now)
Reads the network statistics of the client into a sample.
LagLevel
How a figure compares with a sound link.
Definition lagometer.hpp:42
@ Good
As on a local network.
@ Warning
Players start to notice.
@ Bad
The game is hard to play.
LagGraph
A graph of the lagometer.
Definition lagometer.hpp:21
@ Loss
Share of the server's datagrams lost, in percent.
@ BytesOut
Bytes sent per second.
@ Jitter
Jitter of the snapshots' arrivals, in milliseconds.
@ Corrections
Corrections of the predicted ship per second (ShipPrediction::corrections()).
@ Rtt
Smoothed round trip time, in milliseconds.
@ BytesIn
Bytes received per second.
@ Interval
Mean time between two snapshots, in milliseconds.
std::string lag_label(LagGraph graph, double value)
Label of a value of a graph, for the overlay: "RTT 152 MS", "LOSS 2.5 %".
constexpr std::size_t lag_graph_count
Number of graphs: LagGraph values are 0 to lag_graph_count - 1.
Definition lagometer.hpp:39
std::vector< std::string > lag_lines(const LagSample &sample)
The figures of a sample as lines of the engine's metrics overlay, so that the frame metrics (F12) sho...
LagLevel lag_level(LagGraph graph, double value) noexcept
Level of a value of a graph: the RTT is bad above 200 ms, the loss above 5 %...
Packet loss, snapshot interval and jitter of the datagrams the client receives.
The figures of every graph at one time, in the unit of each graph.
Definition lagometer.hpp:52
std::array< double, lag_graph_count > values
One value per graph, indexed by LagGraph.
Definition lagometer.hpp:54
double value(LagGraph graph) const noexcept
The value of a graph.
Definition lagometer.hpp:59