mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-18 14:20:14 +00:00
add Util.DebugExecutionTime
This commit is contained in:
@@ -49,6 +49,7 @@ set(PRJ_HEADERS
|
|||||||
include/TServer.h
|
include/TServer.h
|
||||||
include/VehicleData.h
|
include/VehicleData.h
|
||||||
include/Env.h
|
include/Env.h
|
||||||
|
include/Profiling.h
|
||||||
)
|
)
|
||||||
# add all source files (.cpp) to this, except the one with main()
|
# add all source files (.cpp) to this, except the one with main()
|
||||||
set(PRJ_SOURCES
|
set(PRJ_SOURCES
|
||||||
@@ -72,6 +73,7 @@ set(PRJ_SOURCES
|
|||||||
src/TServer.cpp
|
src/TServer.cpp
|
||||||
src/VehicleData.cpp
|
src/VehicleData.cpp
|
||||||
src/Env.cpp
|
src/Env.cpp
|
||||||
|
src/Profiling.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(Lua REQUIRED)
|
find_package(Lua REQUIRED)
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ struct UnitProfileCollection {
|
|||||||
/// Returns the number of elements the moving average is calculated over.
|
/// Returns the number of elements the moving average is calculated over.
|
||||||
size_t measurement_count(const std::string& unit);
|
size_t measurement_count(const std::string& unit);
|
||||||
|
|
||||||
|
/// Returns the averages for all stored units.
|
||||||
|
std::unordered_map<std::string, double> all_average_durations();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::synchronized_value<std::unordered_map<std::string, UnitExecutionTime>> m_map;
|
boost::synchronized_value<std::unordered_map<std::string, UnitExecutionTime>> m_map;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -18,9 +18,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Profiling.h"
|
||||||
#include "TNetwork.h"
|
#include "TNetwork.h"
|
||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
#include <any>
|
#include <any>
|
||||||
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
@@ -253,6 +255,8 @@ private:
|
|||||||
sol::table Lua_FS_ListFiles(const std::string& Path);
|
sol::table Lua_FS_ListFiles(const std::string& Path);
|
||||||
sol::table Lua_FS_ListDirectories(const std::string& Path);
|
sol::table Lua_FS_ListDirectories(const std::string& Path);
|
||||||
|
|
||||||
|
prof::UnitProfileCollection mProfile {};
|
||||||
|
|
||||||
std::string mName;
|
std::string mName;
|
||||||
TLuaStateId mStateId;
|
TLuaStateId mStateId;
|
||||||
lua_State* mState;
|
lua_State* mState;
|
||||||
|
|||||||
@@ -36,6 +36,15 @@ void prof::UnitExecutionTime::add_sample(const Duration& dur) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prof::UnitExecutionTime::UnitExecutionTime()
|
prof::UnitExecutionTime::UnitExecutionTime()
|
||||||
: m_measurements(boost::circular_buffer<Duration>(16)) {
|
: m_measurements(boost::circular_buffer<Duration>(100)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_map<std::string, double> prof::UnitProfileCollection::all_average_durations() {
|
||||||
|
auto map = m_map.synchronize();
|
||||||
|
std::unordered_map<std::string, double> result {};
|
||||||
|
for (const auto& [name, time] : *map) {
|
||||||
|
result[name] = time.average_duration().count();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,11 +21,13 @@
|
|||||||
#include "CustomAssert.h"
|
#include "CustomAssert.h"
|
||||||
#include "Http.h"
|
#include "Http.h"
|
||||||
#include "LuaAPI.h"
|
#include "LuaAPI.h"
|
||||||
|
#include "Profiling.h"
|
||||||
#include "TLuaPlugin.h"
|
#include "TLuaPlugin.h"
|
||||||
#include "sol/object.hpp"
|
#include "sol/object.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <fmt/core.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -847,6 +849,15 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
|||||||
UtilTable.set_function("RandomIntRange", [this](int64_t min, int64_t max) -> int64_t {
|
UtilTable.set_function("RandomIntRange", [this](int64_t min, int64_t max) -> int64_t {
|
||||||
return std::uniform_int_distribution(min, max)(mMersenneTwister);
|
return std::uniform_int_distribution(min, max)(mMersenneTwister);
|
||||||
});
|
});
|
||||||
|
UtilTable.set_function("DebugExecutionTime", [this]() -> sol::table {
|
||||||
|
sol::state_view StateView(mState);
|
||||||
|
sol::table Result = StateView.create_table();
|
||||||
|
auto durs = mProfile.all_average_durations();
|
||||||
|
for (const auto& [name, dur] : durs) {
|
||||||
|
Result[name] = dur;
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
});
|
||||||
|
|
||||||
auto HttpTable = StateView.create_named_table("Http");
|
auto HttpTable = StateView.create_named_table("Http");
|
||||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||||
@@ -984,6 +995,7 @@ void TLuaEngine::StateThreadData::operator()() {
|
|||||||
std::chrono::milliseconds(500),
|
std::chrono::milliseconds(500),
|
||||||
[&]() -> bool { return !mStateFunctionQueue.empty(); });
|
[&]() -> bool { return !mStateFunctionQueue.empty(); });
|
||||||
if (NotExpired) {
|
if (NotExpired) {
|
||||||
|
auto ProfStart = prof::now();
|
||||||
auto TheQueuedFunction = std::move(mStateFunctionQueue.front());
|
auto TheQueuedFunction = std::move(mStateFunctionQueue.front());
|
||||||
mStateFunctionQueue.erase(mStateFunctionQueue.begin());
|
mStateFunctionQueue.erase(mStateFunctionQueue.begin());
|
||||||
Lock.unlock();
|
Lock.unlock();
|
||||||
@@ -1042,6 +1054,9 @@ void TLuaEngine::StateThreadData::operator()() {
|
|||||||
Result->ErrorMessage = BeamMPFnNotFoundError; // special error kind that we can ignore later
|
Result->ErrorMessage = BeamMPFnNotFoundError; // special error kind that we can ignore later
|
||||||
Result->MarkAsReady();
|
Result->MarkAsReady();
|
||||||
}
|
}
|
||||||
|
auto ProfEnd = prof::now();
|
||||||
|
auto ProfDuration = prof::duration(ProfStart, ProfEnd);
|
||||||
|
mProfile.add_sample(FnName, ProfDuration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1101,7 +1116,7 @@ void TLuaResult::MarkAsReady() {
|
|||||||
void TLuaResult::WaitUntilReady() {
|
void TLuaResult::WaitUntilReady() {
|
||||||
std::unique_lock readyLock(*this->ReadyMutex);
|
std::unique_lock readyLock(*this->ReadyMutex);
|
||||||
// wait if not ready yet
|
// wait if not ready yet
|
||||||
if(!this->Ready)
|
if (!this->Ready)
|
||||||
this->ReadyCondition->wait(readyLock);
|
this->ReadyCondition->wait(readyLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user