Add ScopedTimer, Remove some comments

This commit is contained in:
Lion Kortlepel 2021-10-31 01:27:21 +02:00
parent 50589fbe3d
commit fdc205f521
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
6 changed files with 52 additions and 5 deletions

View File

@ -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}")

19
include/TScopedTimer.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <chrono>
#include <functional>
#include <string>
class TScopedTimer {
public:
TScopedTimer();
TScopedTimer(const std::string& Name);
TScopedTimer(std::function<void(size_t)> OnDestroy);
~TScopedTimer();
std::function<void(size_t /* time_ms */)> OnDestroy { nullptr };
private:
std::chrono::high_resolution_clock::time_point mStartTime;
std::string Name;
};

View File

@ -2,6 +2,7 @@
#include "IThreaded.h"
#include "RWMutex.h"
#include "TScopedTimer.h"
#include <functional>
#include <memory>
#include <mutex>
@ -27,6 +28,7 @@ public:
static void GlobalParser(const std::weak_ptr<TClient>& 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" };
};

27
src/TScopedTimer.cpp Normal file
View File

@ -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<void(size_t)> 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 ");
}
}

View File

@ -93,7 +93,6 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& 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<TClient>& 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<TClient>& 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':

View File

@ -11,6 +11,7 @@
#include "TNetwork.h"
#include "TPPSMonitor.h"
#include "TResourceManager.h"
#include "TScopedTimer.h"
#include "TServer.h"
#include <iostream>
@ -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);