netprof udp byte averages

This commit is contained in:
Green Sky 2025-01-20 15:39:02 +01:00
parent d416d3d595
commit ca59e51e9e
No known key found for this signature in database
2 changed files with 45 additions and 6 deletions

View File

@ -1,6 +1,7 @@
#include "./tox_netprof_ui.hpp" #include "./tox_netprof_ui.hpp"
#include <imgui/imgui.h> #include <imgui/imgui.h>
#include <string>
static const char* typedPkgIDToString(Tox_Netprof_Packet_Type type, uint8_t id) { static const char* typedPkgIDToString(Tox_Netprof_Packet_Type type, uint8_t id) {
// pain // pain
@ -69,7 +70,11 @@ void ToxNetprofUI::tick(float time_delta) {
_time_since_last_add += time_delta; _time_since_last_add += time_delta;
if (_time_since_last_add >= _value_add_interval) { if (_time_since_last_add >= _value_add_interval) {
_time_since_last_add = 0.f; // very loose if (_time_since_last_add >= 20.f * _value_add_interval) {
_time_since_last_add = 0.f; // cut our losses
} else {
_time_since_last_add -= _value_add_interval;
}
if (_udp_tctx.empty()) { if (_udp_tctx.empty()) {
_udp_tctx.push_back(0.f); _udp_tctx.push_back(0.f);
@ -107,9 +112,6 @@ void ToxNetprofUI::tick(float time_delta) {
_udp_tbrx_prev = new_value; _udp_tbrx_prev = new_value;
} }
if (_udp_tbrx.empty()) {
}
// TODO: limit // TODO: limit
while (_udp_tctx.size() > 5*60) { while (_udp_tctx.size() > 5*60) {
_udp_tctx.erase(_udp_tctx.begin()); _udp_tctx.erase(_udp_tctx.begin());
@ -126,6 +128,22 @@ void ToxNetprofUI::tick(float time_delta) {
while (_udp_tbrx.size() > 5*60) { while (_udp_tbrx.size() > 5*60) {
_udp_tbrx.erase(_udp_tbrx.begin()); _udp_tbrx.erase(_udp_tbrx.begin());
} }
_udp_tbtx_avg = 0.f;
if (!_udp_tbtx.empty()) {
for (const auto bytes : _udp_tbtx) {
_udp_tbtx_avg += bytes;
}
_udp_tbtx_avg /= (_udp_tbtx.size() * _value_add_interval);
}
_udp_tbrx_avg = 0.f;
if (!_udp_tbrx.empty()) {
for (const auto bytes : _udp_tbrx) {
_udp_tbrx_avg += bytes;
}
_udp_tbrx_avg /= (_udp_tbrx.size() * _value_add_interval);
}
} }
} }
@ -290,8 +308,26 @@ float ToxNetprofUI::render(float time_delta) {
const float line_height = ImGui::GetTextLineHeight(); const float line_height = ImGui::GetTextLineHeight();
ImGui::PlotHistogram("udp total packets sent##histograms", _udp_tctx.data(), _udp_tctx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height}); ImGui::PlotHistogram("udp total packets sent##histograms", _udp_tctx.data(), _udp_tctx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height});
ImGui::PlotHistogram("udp total packets received##histograms", _udp_tcrx.data(), _udp_tcrx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height}); ImGui::PlotHistogram("udp total packets received##histograms", _udp_tcrx.data(), _udp_tcrx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height});
ImGui::PlotHistogram("udp total bytes sent##histograms", _udp_tbtx.data(), _udp_tbtx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height});
ImGui::PlotHistogram("udp total bytes received##histograms", _udp_tbrx.data(), _udp_tbrx.size(), 0, nullptr, 0.f, FLT_MAX, {0, 3*line_height}); std::string udp_tbtx_avg_str {"avg " + std::to_string(_udp_tbtx_avg) + " B/s"};
ImGui::PlotHistogram(
"udp total bytes sent##histograms",
_udp_tbtx.data(), _udp_tbtx.size(),
0,
udp_tbtx_avg_str.c_str(),
0.f, FLT_MAX,
{0, 3*line_height}
);
std::string udp_tbrx_avg_str {"avg " + std::to_string(_udp_tbrx_avg) + " B/s"};
ImGui::PlotHistogram(
"udp total bytes received##histograms",
_udp_tbrx.data(), _udp_tbrx.size(),
0,
udp_tbrx_avg_str.c_str(),
0.f, FLT_MAX,
{0, 3*line_height}
);
} else { } else {
ImGui::TextUnformatted("logging disabled!"); ImGui::TextUnformatted("logging disabled!");
if (ImGui::Button("enable")) { if (ImGui::Button("enable")) {

View File

@ -43,6 +43,9 @@ class ToxNetprofUI {
std::vector<float> _udp_tbtx; std::vector<float> _udp_tbtx;
std::vector<float> _udp_tbrx; std::vector<float> _udp_tbrx;
float _udp_tbtx_avg {0.f};
float _udp_tbrx_avg {0.f};
const float _value_add_interval {1.f}; // every second const float _value_add_interval {1.f}; // every second
float _time_since_last_add {0.f}; float _time_since_last_add {0.f};