mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 02:30:54 +00:00
add Util.DebugExecutionTime
This commit is contained in:
@@ -49,6 +49,7 @@ set(PRJ_HEADERS
|
||||
include/TServer.h
|
||||
include/VehicleData.h
|
||||
include/Env.h
|
||||
include/Profiling.h
|
||||
)
|
||||
# add all source files (.cpp) to this, except the one with main()
|
||||
set(PRJ_SOURCES
|
||||
@@ -72,6 +73,7 @@ set(PRJ_SOURCES
|
||||
src/TServer.cpp
|
||||
src/VehicleData.cpp
|
||||
src/Env.cpp
|
||||
src/Profiling.cpp
|
||||
)
|
||||
|
||||
find_package(Lua REQUIRED)
|
||||
|
||||
@@ -45,6 +45,9 @@ struct UnitProfileCollection {
|
||||
/// Returns the number of elements the moving average is calculated over.
|
||||
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:
|
||||
boost::synchronized_value<std::unordered_map<std::string, UnitExecutionTime>> m_map;
|
||||
};
|
||||
|
||||
@@ -18,9 +18,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Profiling.h"
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
#include <any>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <initializer_list>
|
||||
@@ -253,6 +255,8 @@ private:
|
||||
sol::table Lua_FS_ListFiles(const std::string& Path);
|
||||
sol::table Lua_FS_ListDirectories(const std::string& Path);
|
||||
|
||||
prof::UnitProfileCollection mProfile {};
|
||||
|
||||
std::string mName;
|
||||
TLuaStateId mStateId;
|
||||
lua_State* mState;
|
||||
|
||||
@@ -36,6 +36,15 @@ void prof::UnitExecutionTime::add_sample(const Duration& dur) {
|
||||
}
|
||||
|
||||
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 "Http.h"
|
||||
#include "LuaAPI.h"
|
||||
#include "Profiling.h"
|
||||
#include "TLuaPlugin.h"
|
||||
#include "sol/object.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <fmt/core.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <random>
|
||||
#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 {
|
||||
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");
|
||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||
@@ -984,6 +995,7 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
std::chrono::milliseconds(500),
|
||||
[&]() -> bool { return !mStateFunctionQueue.empty(); });
|
||||
if (NotExpired) {
|
||||
auto ProfStart = prof::now();
|
||||
auto TheQueuedFunction = std::move(mStateFunctionQueue.front());
|
||||
mStateFunctionQueue.erase(mStateFunctionQueue.begin());
|
||||
Lock.unlock();
|
||||
@@ -1042,6 +1054,9 @@ void TLuaEngine::StateThreadData::operator()() {
|
||||
Result->ErrorMessage = BeamMPFnNotFoundError; // special error kind that we can ignore later
|
||||
Result->MarkAsReady();
|
||||
}
|
||||
auto ProfEnd = prof::now();
|
||||
auto ProfDuration = prof::duration(ProfStart, ProfEnd);
|
||||
mProfile.add_sample(FnName, ProfDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user