added PerformanceCounter

This commit is contained in:
Evil Factory
2022-06-19 11:50:51 -03:00
parent 78716f4695
commit 64c81146fb
5 changed files with 61 additions and 16 deletions
@@ -350,7 +350,10 @@ namespace Barotrauma
GameMain.LuaCs.Update(); GameMain.LuaCs.Update();
GameMain.LuaCs.Hook.Call("think", new object[] { }); GameMain.LuaCs.Hook.Call("think", new object[] { });
performanceCounterTimer.Stop(); performanceCounterTimer.Stop();
LuaCsTimer.LastUpdateTime = performanceCounterTimer.ElapsedMilliseconds; if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
GameMain.LuaCs.PerformanceCounter.UpdateElapsedTime = (double)performanceCounterTimer.ElapsedTicks / Stopwatch.Frequency;
}
performanceCounterTimer.Reset(); performanceCounterTimer.Reset();
Timing.Accumulator -= Timing.Step; Timing.Accumulator -= Timing.Step;
@@ -8,6 +8,7 @@ using System.Text;
using MoonSharp.Interpreter.Interop; using MoonSharp.Interpreter.Interop;
using static Barotrauma.LuaCsSetup; using static Barotrauma.LuaCsSetup;
using System.Threading; using System.Threading;
using System.Diagnostics;
namespace Barotrauma namespace Barotrauma
{ {
@@ -372,6 +373,8 @@ namespace Barotrauma
} }
private Stopwatch performanceMeasurement = new Stopwatch();
[MoonSharpHidden] [MoonSharpHidden]
public T Call<T>(string name, params object[] args) public T Call<T>(string name, params object[] args)
{ {
@@ -397,6 +400,11 @@ namespace Barotrauma
{ {
try try
{ {
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
performanceMeasurement.Start();
}
var result = tuple.Item1.func(args); var result = tuple.Item1.func(args);
if (result != null) if (result != null)
{ {
@@ -417,6 +425,13 @@ namespace Barotrauma
else lastResult = (T)result; else lastResult = (T)result;
} }
} }
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
performanceMeasurement.Stop();
GameMain.LuaCs.PerformanceCounter.SetHookElapsedTicks(name, key, performanceMeasurement.ElapsedTicks);
performanceMeasurement.Reset();
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Barotrauma
{
public class LuaCsPerformanceCounter
{
public bool EnablePerformanceCounter = false;
public double UpdateElapsedTime;
public Dictionary<string, Dictionary<string, double>> HookElapsedTime = new Dictionary<string, Dictionary<string, double>>();
public static float MemoryUsage
{
get
{
Process proc = Process.GetCurrentProcess();
float memory = MathF.Round(proc.PrivateMemorySize64 / (1024 * 1024), 2);
proc.Dispose();
return memory;
}
}
public void SetHookElapsedTicks(string eventName, string hookName, long ticks)
{
if (!HookElapsedTime.ContainsKey(eventName))
{
HookElapsedTime[eventName] = new Dictionary<string, double>();
}
HookElapsedTime[eventName][hookName] = (double)ticks / Stopwatch.Frequency;
}
}
}
@@ -54,15 +54,14 @@ namespace Barotrauma
lua.Globals["CsScript"] = CsScript; lua.Globals["CsScript"] = CsScript;
} }
public LuaGame Game { get; private set; }
public LuaScriptLoader LuaScriptLoader { get; private set; } public LuaScriptLoader LuaScriptLoader { get; private set; }
public LuaGame Game { get; private set; }
public LuaCsHook Hook { get; private set; } public LuaCsHook Hook { get; private set; }
public LuaCsTimer Timer { get; private set; } public LuaCsTimer Timer { get; private set; }
public LuaCsNetworking Networking { get; private set; } public LuaCsNetworking Networking { get; private set; }
public LuaCsSteam Steam { get; private set; } public LuaCsSteam Steam { get; private set; }
public LuaCsPerformanceCounter PerformanceCounter { get; private set; }
public LuaCsModStore ModStore { get; private set; } public LuaCsModStore ModStore { get; private set; }
private LuaRequire require { get; set; } private LuaRequire require { get; set; }
@@ -333,6 +332,7 @@ namespace Barotrauma
Networking = new LuaCsNetworking(); Networking = new LuaCsNetworking();
Timer = new LuaCsTimer(); Timer = new LuaCsTimer();
Steam = new LuaCsSteam(); Steam = new LuaCsSteam();
PerformanceCounter = new LuaCsPerformanceCounter();
LuaScriptLoader = null; LuaScriptLoader = null;
lua = null; lua = null;
Lua = null; Lua = null;
@@ -381,6 +381,7 @@ namespace Barotrauma
Networking = new LuaCsNetworking(); Networking = new LuaCsNetworking();
Timer = new LuaCsTimer(); Timer = new LuaCsTimer();
Steam = new LuaCsSteam(); Steam = new LuaCsSteam();
PerformanceCounter = new LuaCsPerformanceCounter();
Hook.Initialize(); Hook.Initialize();
ModStore.Initialize(); ModStore.Initialize();
@@ -396,6 +397,7 @@ namespace Barotrauma
UserData.RegisterType<LuaCsNetworking>(); UserData.RegisterType<LuaCsNetworking>();
UserData.RegisterType<LuaCsSteam>(); UserData.RegisterType<LuaCsSteam>();
UserData.RegisterType<LuaUserData>(); UserData.RegisterType<LuaUserData>();
UserData.RegisterType<LuaCsPerformanceCounter>();
UserData.RegisterType<IUserDataDescriptor>(); UserData.RegisterType<IUserDataDescriptor>();
lua.Globals["printerror"] = (Action<object>)PrintError; lua.Globals["printerror"] = (Action<object>)PrintError;
@@ -418,6 +420,7 @@ namespace Barotrauma
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>(); lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
lua.Globals["Networking"] = Networking; lua.Globals["Networking"] = Networking;
lua.Globals["Steam"] = Steam; lua.Globals["Steam"] = Steam;
lua.Globals["PerformanceCounter"] = PerformanceCounter;
lua.Globals["ExecutionNumber"] = executionNumber; lua.Globals["ExecutionNumber"] = executionNumber;
lua.Globals["CSActive"] = csActive; lua.Globals["CSActive"] = csActive;
@@ -6,21 +6,9 @@ namespace Barotrauma
{ {
public class LuaCsTimer public class LuaCsTimer
{ {
public static long LastUpdateTime = 0;
public static double Time => Timing.TotalTime; public static double Time => Timing.TotalTime;
public static double GetTime() => Time; public static double GetTime() => Time;
public static float GetUsageMemory()
{
Process proc = Process.GetCurrentProcess();
float memory = MathF.Round(proc.PrivateMemorySize64 / (1024 * 1024), 2);
proc.Dispose();
return memory;
}
private class TimerComparer : IComparer<TimedAction> private class TimerComparer : IComparer<TimedAction>
{ {
public int Compare(TimedAction timedAction1, TimedAction timedAction2) public int Compare(TimedAction timedAction1, TimedAction timedAction2)