mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-06-09 16:24:22 +00:00
chore: simplify metrics (#1987)
* chore: simplify metrics rename to hpp and remove unused includes * feedback
This commit is contained in:
@@ -1,105 +1,77 @@
|
||||
#include "Metrics.hpp"
|
||||
#include "Metrics.h"
|
||||
|
||||
#include "StringifiedEnum.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
std::unordered_map<MetricVariable, Metric*> Metrics::m_Metrics = {};
|
||||
std::vector<MetricVariable> Metrics::m_Variables = {
|
||||
MetricVariable::GameLoop,
|
||||
MetricVariable::PacketHandling,
|
||||
MetricVariable::UpdateEntities,
|
||||
MetricVariable::UpdateSpawners,
|
||||
MetricVariable::Physics,
|
||||
MetricVariable::UpdateReplica,
|
||||
MetricVariable::Ghosting,
|
||||
MetricVariable::CPUTime,
|
||||
MetricVariable::Sleep,
|
||||
MetricVariable::Frame,
|
||||
};
|
||||
namespace {
|
||||
std::unordered_map<MetricVariable, Metric> g_Metrics = {};
|
||||
std::vector<MetricVariable> g_Variables = {
|
||||
MetricVariable::GameLoop,
|
||||
MetricVariable::PacketHandling,
|
||||
MetricVariable::UpdateEntities,
|
||||
MetricVariable::UpdateSpawners,
|
||||
MetricVariable::Physics,
|
||||
MetricVariable::UpdateReplica,
|
||||
MetricVariable::Ghosting,
|
||||
MetricVariable::CPUTime,
|
||||
MetricVariable::Sleep,
|
||||
MetricVariable::Frame,
|
||||
};
|
||||
}
|
||||
|
||||
void Metrics::AddMeasurement(MetricVariable variable, int64_t value) {
|
||||
const auto& iter = m_Metrics.find(variable);
|
||||
|
||||
Metric* metric;
|
||||
|
||||
if (iter == m_Metrics.end()) {
|
||||
metric = new Metric();
|
||||
|
||||
m_Metrics[variable] = metric;
|
||||
} else {
|
||||
metric = iter->second;
|
||||
}
|
||||
auto& metric = g_Metrics[variable];
|
||||
|
||||
AddMeasurement(metric, value);
|
||||
}
|
||||
|
||||
void Metrics::AddMeasurement(Metric* metric, int64_t value) {
|
||||
const auto index = metric->measurementIndex;
|
||||
void Metrics::AddMeasurement(Metric& metric, int64_t value) {
|
||||
const auto index = metric.measurementIndex;
|
||||
|
||||
metric->measurements[index] = value;
|
||||
metric.measurements[index] = value;
|
||||
|
||||
if (metric->max == -1 || value > metric->max) {
|
||||
metric->max = value;
|
||||
} else if (metric->min == -1 || metric->min > value) {
|
||||
metric->min = value;
|
||||
if (metric.max == -1 || value > metric.max) {
|
||||
metric.max = value;
|
||||
} else if (metric.min == -1 || metric.min > value) {
|
||||
metric.min = value;
|
||||
}
|
||||
|
||||
if (metric->measurementSize < MAX_MEASURMENT_POINTS) {
|
||||
metric->measurementSize++;
|
||||
if (metric.measurementSize < MAX_MEASURMENT_POINTS) {
|
||||
metric.measurementSize++;
|
||||
}
|
||||
|
||||
metric->measurementIndex = (index + 1) % MAX_MEASURMENT_POINTS;
|
||||
metric.measurementIndex = (index + 1) % MAX_MEASURMENT_POINTS;
|
||||
}
|
||||
|
||||
const Metric* Metrics::GetMetric(MetricVariable variable) {
|
||||
const auto& iter = m_Metrics.find(variable);
|
||||
|
||||
if (iter == m_Metrics.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Metric* metric = iter->second;
|
||||
const Metric& Metrics::GetMetric(MetricVariable variable) {
|
||||
auto& metric = g_Metrics[variable];
|
||||
|
||||
int64_t average = 0;
|
||||
|
||||
for (size_t i = 0; i < metric->measurementSize; i++) {
|
||||
average += metric->measurements[i];
|
||||
for (size_t i = 0; i < metric.measurementSize; i++) {
|
||||
average += metric.measurements[i];
|
||||
}
|
||||
|
||||
average /= metric->measurementSize;
|
||||
average /= metric.measurementSize;
|
||||
|
||||
metric->average = average;
|
||||
metric.average = average;
|
||||
|
||||
return metric;
|
||||
}
|
||||
|
||||
void Metrics::StartMeasurement(MetricVariable variable) {
|
||||
const auto& iter = m_Metrics.find(variable);
|
||||
auto& metric = g_Metrics[variable];
|
||||
|
||||
Metric* metric;
|
||||
|
||||
if (iter == m_Metrics.end()) {
|
||||
metric = new Metric();
|
||||
|
||||
m_Metrics[variable] = metric;
|
||||
} else {
|
||||
metric = iter->second;
|
||||
}
|
||||
|
||||
metric->activeMeasurement = std::chrono::high_resolution_clock::now();
|
||||
metric.activeMeasurement = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Metrics::EndMeasurement(MetricVariable variable) {
|
||||
const auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
const auto& iter = m_Metrics.find(variable);
|
||||
auto& metric = g_Metrics[variable];
|
||||
|
||||
if (iter == m_Metrics.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Metric* metric = iter->second;
|
||||
|
||||
const auto elapsed = end - metric->activeMeasurement;
|
||||
const auto elapsed = end - metric.activeMeasurement;
|
||||
|
||||
const auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count();
|
||||
|
||||
@@ -110,44 +82,12 @@ float Metrics::ToMiliseconds(int64_t nanoseconds) {
|
||||
return static_cast<float>(nanoseconds) / 1e6;
|
||||
}
|
||||
|
||||
std::string Metrics::MetricVariableToString(MetricVariable variable) {
|
||||
switch (variable) {
|
||||
case MetricVariable::GameLoop:
|
||||
return "GameLoop";
|
||||
case MetricVariable::PacketHandling:
|
||||
return "PacketHandling";
|
||||
case MetricVariable::UpdateEntities:
|
||||
return "UpdateEntities";
|
||||
case MetricVariable::UpdateSpawners:
|
||||
return "UpdateSpawners";
|
||||
case MetricVariable::Physics:
|
||||
return "Physics";
|
||||
case MetricVariable::UpdateReplica:
|
||||
return "UpdateReplica";
|
||||
case MetricVariable::Sleep:
|
||||
return "Sleep";
|
||||
case MetricVariable::CPUTime:
|
||||
return "CPUTime";
|
||||
case MetricVariable::Frame:
|
||||
return "Frame";
|
||||
case MetricVariable::Ghosting:
|
||||
return "Ghosting";
|
||||
|
||||
default:
|
||||
return "Invalid";
|
||||
}
|
||||
const std::string_view Metrics::MetricVariableToString(MetricVariable variable) {
|
||||
return StringifiedEnum::ToString(variable);
|
||||
}
|
||||
|
||||
const std::vector<MetricVariable>& Metrics::GetAllMetrics() {
|
||||
return m_Variables;
|
||||
}
|
||||
|
||||
void Metrics::Clear() {
|
||||
for (const auto& pair : m_Metrics) {
|
||||
delete pair.second;
|
||||
}
|
||||
|
||||
m_Metrics.clear();
|
||||
return g_Variables;
|
||||
}
|
||||
|
||||
/* RSS Memory utilities
|
||||
|
||||
48
dCommon/Metrics.h
Normal file
48
dCommon/Metrics.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
|
||||
#define MAX_MEASURMENT_POINTS 1024
|
||||
|
||||
enum class MetricVariable : int32_t {
|
||||
GameLoop,
|
||||
PacketHandling,
|
||||
UpdateEntities,
|
||||
UpdateSpawners,
|
||||
Physics,
|
||||
UpdateReplica,
|
||||
Ghosting,
|
||||
CPUTime,
|
||||
Sleep,
|
||||
Frame,
|
||||
};
|
||||
|
||||
struct Metric {
|
||||
int64_t measurements[MAX_MEASURMENT_POINTS] = {};
|
||||
size_t measurementIndex = 0;
|
||||
size_t measurementSize = 0;
|
||||
int64_t max = -1;
|
||||
int64_t min = -1;
|
||||
int64_t average = 0;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> activeMeasurement;
|
||||
};
|
||||
|
||||
namespace Metrics {
|
||||
void AddMeasurement(MetricVariable variable, int64_t value);
|
||||
void AddMeasurement(Metric& metric, int64_t value);
|
||||
const Metric& GetMetric(MetricVariable variable);
|
||||
void StartMeasurement(MetricVariable variable);
|
||||
void EndMeasurement(MetricVariable variable);
|
||||
float ToMiliseconds(int64_t nanoseconds);
|
||||
const std::string_view MetricVariableToString(MetricVariable variable);
|
||||
const std::vector<MetricVariable>& GetAllMetrics();
|
||||
|
||||
size_t GetPeakRSS();
|
||||
size_t GetCurrentRSS();
|
||||
size_t GetProcessID();
|
||||
};
|
||||
@@ -1,61 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
|
||||
#define MAX_MEASURMENT_POINTS 1024
|
||||
|
||||
enum class MetricVariable : int32_t
|
||||
{
|
||||
GameLoop,
|
||||
PacketHandling,
|
||||
UpdateEntities,
|
||||
UpdateSpawners,
|
||||
Physics,
|
||||
UpdateReplica,
|
||||
Ghosting,
|
||||
CPUTime,
|
||||
Sleep,
|
||||
Frame,
|
||||
};
|
||||
|
||||
struct Metric
|
||||
{
|
||||
int64_t measurements[MAX_MEASURMENT_POINTS] = {};
|
||||
size_t measurementIndex = 0;
|
||||
size_t measurementSize = 0;
|
||||
int64_t max = -1;
|
||||
int64_t min = -1;
|
||||
int64_t average = 0;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> activeMeasurement;
|
||||
};
|
||||
|
||||
class Metrics
|
||||
{
|
||||
public:
|
||||
~Metrics();
|
||||
|
||||
static void AddMeasurement(MetricVariable variable, int64_t value);
|
||||
static void AddMeasurement(Metric* metric, int64_t value);
|
||||
static const Metric* GetMetric(MetricVariable variable);
|
||||
static void StartMeasurement(MetricVariable variable);
|
||||
static void EndMeasurement(MetricVariable variable);
|
||||
static float ToMiliseconds(int64_t nanoseconds);
|
||||
static std::string MetricVariableToString(MetricVariable variable);
|
||||
static const std::vector<MetricVariable>& GetAllMetrics();
|
||||
|
||||
static size_t GetPeakRSS();
|
||||
static size_t GetCurrentRSS();
|
||||
static size_t GetProcessID();
|
||||
|
||||
static void Clear();
|
||||
|
||||
private:
|
||||
Metrics();
|
||||
|
||||
static std::unordered_map<MetricVariable, Metric*> m_Metrics;
|
||||
static std::vector<MetricVariable> m_Variables;
|
||||
};
|
||||
Reference in New Issue
Block a user