From fdc205f52139c636ba5be12b42f81c905605ee70 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 31 Oct 2021 01:27:21 +0200 Subject: [PATCH] Add ScopedTimer, Remove some comments --- CMakeLists.txt | 1 + include/TScopedTimer.h | 19 +++++++++++++++++++ include/TServer.h | 3 +++ src/TScopedTimer.cpp | 27 +++++++++++++++++++++++++++ src/TServer.cpp | 4 ---- src/main.cpp | 3 ++- 6 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 include/TScopedTimer.h create mode 100644 src/TScopedTimer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d7a13f..4be7b20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,7 @@ add_executable(BeamMP-Server include/TPPSMonitor.h src/TPPSMonitor.cpp include/TNetwork.h src/TNetwork.cpp include/LuaAPI.h src/LuaAPI.cpp + include/TScopedTimer.h src/TScopedTimer.cpp include/SignalHandling.h src/SignalHandling.cpp) target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}") diff --git a/include/TScopedTimer.h b/include/TScopedTimer.h new file mode 100644 index 0000000..3eea0ee --- /dev/null +++ b/include/TScopedTimer.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +class TScopedTimer { +public: + TScopedTimer(); + TScopedTimer(const std::string& Name); + TScopedTimer(std::function OnDestroy); + ~TScopedTimer(); + + std::function OnDestroy { nullptr }; + +private: + std::chrono::high_resolution_clock::time_point mStartTime; + std::string Name; +}; diff --git a/include/TServer.h b/include/TServer.h index 472bfce..71025d1 100644 --- a/include/TServer.h +++ b/include/TServer.h @@ -2,6 +2,7 @@ #include "IThreaded.h" #include "RWMutex.h" +#include "TScopedTimer.h" #include #include #include @@ -27,6 +28,7 @@ public: static void GlobalParser(const std::weak_ptr& Client, std::string Packet, TPPSMonitor& PPSMonitor, TNetwork& Network); static void HandleEvent(TClient& c, const std::string& Data); RWMutex& GetClientMutex() const { return mClientsMutex; } + private: TClientSet mClients; mutable RWMutex mClientsMutex; @@ -34,4 +36,5 @@ private: static bool ShouldSpawn(TClient& c, const std::string& CarJson, int ID); static bool IsUnicycle(TClient& c, const std::string& CarJson); static void Apply(TClient& c, int VID, const std::string& pckt); + TScopedTimer mLifeTimer { "Server" }; }; diff --git a/src/TScopedTimer.cpp b/src/TScopedTimer.cpp new file mode 100644 index 0000000..9e15056 --- /dev/null +++ b/src/TScopedTimer.cpp @@ -0,0 +1,27 @@ +#include "TScopedTimer.h" +#include "Common.h" + +TScopedTimer::TScopedTimer() + : mStartTime(std::chrono::high_resolution_clock::now()) { +} + +TScopedTimer::TScopedTimer(const std::string& mName) + : mStartTime(std::chrono::high_resolution_clock::now()) + , Name(mName) { +} + +TScopedTimer::TScopedTimer(std::function OnDestroy) + : OnDestroy(OnDestroy) + , mStartTime(std::chrono::high_resolution_clock::now()) { +} + +TScopedTimer::~TScopedTimer() { + auto EndTime = std::chrono::high_resolution_clock::now(); + auto Delta = EndTime - mStartTime; + size_t TimeDelta = Delta / std::chrono::milliseconds(1); + if (OnDestroy) { + OnDestroy(TimeDelta); + } else { + beammp_info("Scoped timer: \"" + Name + "\" took " + std::to_string(TimeDelta) + "ms "); + } +} diff --git a/src/TServer.cpp b/src/TServer.cpp index 3d9f7a7..6ef4b07 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -93,7 +93,6 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac switch (Code) { case 'H': // initial connection beammp_trace(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")"); - beammp_debug(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")"); if (!Network.SyncClient(Client)) { // TODO handle } @@ -116,12 +115,10 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac return; case 'J': beammp_trace(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - beammp_debug(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); Network.SendToAll(LockedClient.get(), Packet, false, true); return; case 'C': { beammp_trace(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - beammp_debug(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos) break; auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 1)); @@ -140,7 +137,6 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac } case 'E': beammp_trace(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); - beammp_debug(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); HandleEvent(*LockedClient, Packet); return; case 'N': diff --git a/src/main.cpp b/src/main.cpp index d8c5255..a2b8aa1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "TNetwork.h" #include "TPPSMonitor.h" #include "TResourceManager.h" +#include "TScopedTimer.h" #include "TServer.h" #include @@ -49,7 +50,6 @@ int main(int argc, char** argv) try { RegisterThread("Main"); beammp_trace("Running in debug mode on a debug build"); - Sentry.SetupUser(); Sentry.PrintWelcome(); TResourceManager ResourceManager; @@ -66,6 +66,7 @@ int main(int argc, char** argv) try { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } beammp_info("Shutdown."); + return 0; } catch (const std::exception& e) { beammp_error(e.what()); Sentry.LogException(e, _file_basename, _line);