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

View File

@@ -350,7 +350,10 @@ namespace Barotrauma
GameMain.LuaCs.Update();
GameMain.LuaCs.Hook.Call("think", new object[] { });
performanceCounterTimer.Stop();
LuaCsTimer.LastUpdateTime = performanceCounterTimer.ElapsedMilliseconds;
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
GameMain.LuaCs.PerformanceCounter.UpdateElapsedTime = (double)performanceCounterTimer.ElapsedTicks / Stopwatch.Frequency;
}
performanceCounterTimer.Reset();
Timing.Accumulator -= Timing.Step;

View File

@@ -8,6 +8,7 @@ using System.Text;
using MoonSharp.Interpreter.Interop;
using static Barotrauma.LuaCsSetup;
using System.Threading;
using System.Diagnostics;
namespace Barotrauma
{
@@ -372,6 +373,8 @@ namespace Barotrauma
}
private Stopwatch performanceMeasurement = new Stopwatch();
[MoonSharpHidden]
public T Call<T>(string name, params object[] args)
{
@@ -397,6 +400,11 @@ namespace Barotrauma
{
try
{
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
performanceMeasurement.Start();
}
var result = tuple.Item1.func(args);
if (result != null)
{
@@ -417,6 +425,13 @@ namespace Barotrauma
else lastResult = (T)result;
}
}
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
{
performanceMeasurement.Stop();
GameMain.LuaCs.PerformanceCounter.SetHookElapsedTicks(name, key, performanceMeasurement.ElapsedTicks);
performanceMeasurement.Reset();
}
}
catch (Exception e)
{

View File

@@ -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;
}
}
}

View File

@@ -54,15 +54,14 @@ namespace Barotrauma
lua.Globals["CsScript"] = CsScript;
}
public LuaGame Game { get; private set; }
public LuaScriptLoader LuaScriptLoader { get; private set; }
public LuaGame Game { get; private set; }
public LuaCsHook Hook { get; private set; }
public LuaCsTimer Timer { get; private set; }
public LuaCsNetworking Networking { get; private set; }
public LuaCsSteam Steam { get; private set; }
public LuaCsPerformanceCounter PerformanceCounter { get; private set; }
public LuaCsModStore ModStore { get; private set; }
private LuaRequire require { get; set; }
@@ -333,6 +332,7 @@ namespace Barotrauma
Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
Steam = new LuaCsSteam();
PerformanceCounter = new LuaCsPerformanceCounter();
LuaScriptLoader = null;
lua = null;
Lua = null;
@@ -381,6 +381,7 @@ namespace Barotrauma
Networking = new LuaCsNetworking();
Timer = new LuaCsTimer();
Steam = new LuaCsSteam();
PerformanceCounter = new LuaCsPerformanceCounter();
Hook.Initialize();
ModStore.Initialize();
@@ -396,6 +397,7 @@ namespace Barotrauma
UserData.RegisterType<LuaCsNetworking>();
UserData.RegisterType<LuaCsSteam>();
UserData.RegisterType<LuaUserData>();
UserData.RegisterType<LuaCsPerformanceCounter>();
UserData.RegisterType<IUserDataDescriptor>();
lua.Globals["printerror"] = (Action<object>)PrintError;
@@ -418,6 +420,7 @@ namespace Barotrauma
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
lua.Globals["Networking"] = Networking;
lua.Globals["Steam"] = Steam;
lua.Globals["PerformanceCounter"] = PerformanceCounter;
lua.Globals["ExecutionNumber"] = executionNumber;
lua.Globals["CSActive"] = csActive;

View File

@@ -6,20 +6,8 @@ namespace Barotrauma
{
public class LuaCsTimer
{
public static long LastUpdateTime = 0;
public static double Time => Timing.TotalTime;
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>
{