Moved most PerformanceCounter logic out of SCore into the new PerformanceCounterManager, some namespace refactoring

This commit is contained in:
Drachenkaetzchen 2020-01-10 14:08:25 +01:00
parent a751252c4e
commit 47f626cc99
8 changed files with 104 additions and 83 deletions

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using StardewModdingAPI.Framework.Utilities; using StardewModdingAPI.Framework.Utilities;
using PerformanceCounter = StardewModdingAPI.Framework.Utilities.PerformanceCounter; using PerformanceCounter = StardewModdingAPI.Framework.PerformanceCounter.PerformanceCounter;
namespace StardewModdingAPI.Framework.Events namespace StardewModdingAPI.Framework.Events
{ {
@ -32,7 +32,7 @@ namespace StardewModdingAPI.Framework.Events
/// <summary>The cached invocation list.</summary> /// <summary>The cached invocation list.</summary>
private EventHandler<TEventArgs>[] CachedInvocationList; private EventHandler<TEventArgs>[] CachedInvocationList;
public IDictionary<string, PerformanceCounter> PerformanceCounters { get; } = new Dictionary<string, PerformanceCounter>(); public IDictionary<string, PerformanceCounter.PerformanceCounter> PerformanceCounters { get; } = new Dictionary<string, PerformanceCounter.PerformanceCounter>();
private readonly Stopwatch Stopwatch = new Stopwatch(); private readonly Stopwatch Stopwatch = new Stopwatch();
@ -47,7 +47,7 @@ namespace StardewModdingAPI.Framework.Events
public double GetGameAverageExecutionTime() public double GetGameAverageExecutionTime()
{ {
if (this.PerformanceCounters.TryGetValue(Constants.GamePerformanceCounterName, out PerformanceCounter gameExecTime)) if (this.PerformanceCounters.TryGetValue(Constants.GamePerformanceCounterName, out PerformanceCounter.PerformanceCounter gameExecTime))
{ {
return gameExecTime.GetAverage(); return gameExecTime.GetAverage();
} }
@ -151,7 +151,7 @@ namespace StardewModdingAPI.Framework.Events
if (!this.PerformanceCounters.ContainsKey(modName)) if (!this.PerformanceCounters.ContainsKey(modName))
{ {
this.PerformanceCounters.Add(modName, new PerformanceCounter($"{modName}.{this.EventName}")); this.PerformanceCounters.Add(modName, new PerformanceCounter.PerformanceCounter($"{modName}.{this.EventName}"));
} }
this.PerformanceCounters[modName].Add(performanceCounterEntry); this.PerformanceCounters[modName].Add(performanceCounterEntry);

View File

@ -7,7 +7,7 @@ namespace StardewModdingAPI.Framework.Utilities
{ {
string GetEventName(); string GetEventName();
long GetAverageCallsPerSecond(); long GetAverageCallsPerSecond();
IDictionary<string, PerformanceCounter> PerformanceCounters { get; } IDictionary<string, PerformanceCounter.PerformanceCounter> PerformanceCounters { get; }
double GetGameAverageExecutionTime(); double GetGameAverageExecutionTime();
double GetModsAverageExecutionTime(); double GetModsAverageExecutionTime();

View File

@ -2,16 +2,17 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using Cyotek.Collections.Generic; using Cyotek.Collections.Generic;
using StardewModdingAPI.Framework.Utilities;
namespace StardewModdingAPI.Framework.Utilities namespace StardewModdingAPI.Framework.PerformanceCounter
{ {
public class PerformanceCounter public class PerformanceCounter
{ {
private const int MaxCount = 16384; private const int MAX_ENTRIES = 16384;
public string Name { get; } public string Name { get; }
public static Stopwatch Stopwatch = new Stopwatch(); public static Stopwatch Stopwatch = new Stopwatch();
public static long EventsLogged; public static long TotalNumEventsLogged;
private readonly CircularBuffer<PerformanceCounterEntry> _counter; private readonly CircularBuffer<PerformanceCounterEntry> _counter;
@ -21,7 +22,7 @@ namespace StardewModdingAPI.Framework.Utilities
public PerformanceCounter(string name) public PerformanceCounter(string name)
{ {
this.Name = name; this.Name = name;
this._counter = new CircularBuffer<PerformanceCounterEntry>(PerformanceCounter.MaxCount); this._counter = new CircularBuffer<PerformanceCounterEntry>(PerformanceCounter.MAX_ENTRIES);
} }
public int GetAverageCallsPerSecond() public int GetAverageCallsPerSecond()
@ -53,7 +54,7 @@ namespace StardewModdingAPI.Framework.Utilities
} }
PerformanceCounter.Stopwatch.Stop(); PerformanceCounter.Stopwatch.Stop();
EventsLogged++; PerformanceCounter.TotalNumEventsLogged++;
} }
public PerformanceCounterEntry? GetPeak() public PerformanceCounterEntry? GetPeak()

View File

@ -0,0 +1,82 @@
using System.Collections.Generic;
using StardewModdingAPI.Framework.Events;
using StardewModdingAPI.Framework.Utilities;
namespace StardewModdingAPI.Framework.PerformanceCounter
{
internal class PerformanceCounterManager
{
public HashSet<EventPerformanceCounterCategory> PerformanceCounterEvents = new HashSet<EventPerformanceCounterCategory>();
private readonly EventManager EventManager;
public PerformanceCounterManager(EventManager eventManager)
{
this.EventManager = eventManager;
this.InitializePerformanceCounterEvents();
}
private void InitializePerformanceCounterEvents()
{
this.PerformanceCounterEvents = new HashSet<EventPerformanceCounterCategory>()
{
new EventPerformanceCounterCategory(this.EventManager.MenuChanged, false),
// Rendering Events
new EventPerformanceCounterCategory(this.EventManager.Rendering, true),
new EventPerformanceCounterCategory(this.EventManager.Rendered, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingWorld, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedWorld, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingActiveMenu, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedActiveMenu, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingHud, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedHud, true),
new EventPerformanceCounterCategory(this.EventManager.WindowResized, false),
new EventPerformanceCounterCategory(this.EventManager.GameLaunched, false),
new EventPerformanceCounterCategory(this.EventManager.UpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.UpdateTicked, true),
new EventPerformanceCounterCategory(this.EventManager.OneSecondUpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.OneSecondUpdateTicked, true),
new EventPerformanceCounterCategory(this.EventManager.SaveCreating, false),
new EventPerformanceCounterCategory(this.EventManager.SaveCreated, false),
new EventPerformanceCounterCategory(this.EventManager.Saving, false),
new EventPerformanceCounterCategory(this.EventManager.Saved, false),
new EventPerformanceCounterCategory(this.EventManager.DayStarted, false),
new EventPerformanceCounterCategory(this.EventManager.DayEnding, false),
new EventPerformanceCounterCategory(this.EventManager.TimeChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ReturnedToTitle, false),
new EventPerformanceCounterCategory(this.EventManager.ButtonPressed, true),
new EventPerformanceCounterCategory(this.EventManager.ButtonReleased, true),
new EventPerformanceCounterCategory(this.EventManager.CursorMoved, true),
new EventPerformanceCounterCategory(this.EventManager.MouseWheelScrolled, true),
new EventPerformanceCounterCategory(this.EventManager.PeerContextReceived, true),
new EventPerformanceCounterCategory(this.EventManager.ModMessageReceived, true),
new EventPerformanceCounterCategory(this.EventManager.PeerDisconnected, true),
new EventPerformanceCounterCategory(this.EventManager.InventoryChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LevelChanged, true),
new EventPerformanceCounterCategory(this.EventManager.Warped, true),
new EventPerformanceCounterCategory(this.EventManager.LocationListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.BuildingListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LocationListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.DebrisListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LargeTerrainFeatureListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.NpcListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ObjectListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ChestInventoryChanged, true),
new EventPerformanceCounterCategory(this.EventManager.TerrainFeatureListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LoadStageChanged, false),
new EventPerformanceCounterCategory(this.EventManager.UnvalidatedUpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.UnvalidatedUpdateTicked, true),
};
}
}
}

View File

@ -34,7 +34,7 @@ using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Toolkit.Utilities;
using StardewValley; using StardewValley;
using Object = StardewValley.Object; using Object = StardewValley.Object;
using PerformanceCounter = StardewModdingAPI.Framework.Utilities.PerformanceCounter; using PerformanceCounterManager = StardewModdingAPI.Framework.PerformanceCounter.PerformanceCounterManager;
using ThreadState = System.Threading.ThreadState; using ThreadState = System.Threading.ThreadState;
namespace StardewModdingAPI.Framework namespace StardewModdingAPI.Framework
@ -79,11 +79,11 @@ namespace StardewModdingAPI.Framework
/// <remarks>This is initialized after the game starts.</remarks> /// <remarks>This is initialized after the game starts.</remarks>
private readonly ModRegistry ModRegistry = new ModRegistry(); private readonly ModRegistry ModRegistry = new ModRegistry();
private HashSet<EventPerformanceCounterCategory> PerformanceCounterEvents = new HashSet<EventPerformanceCounterCategory>();
/// <summary>Manages SMAPI events for mods.</summary> /// <summary>Manages SMAPI events for mods.</summary>
private readonly EventManager EventManager; private readonly EventManager EventManager;
private readonly PerformanceCounterManager PerformanceCounterManager;
/// <summary>Whether the game is currently running.</summary> /// <summary>Whether the game is currently running.</summary>
private bool IsGameRunning; private bool IsGameRunning;
@ -166,7 +166,7 @@ namespace StardewModdingAPI.Framework
}; };
this.MonitorForGame = this.GetSecondaryMonitor("game"); this.MonitorForGame = this.GetSecondaryMonitor("game");
this.EventManager = new EventManager(this.Monitor, this.ModRegistry); this.EventManager = new EventManager(this.Monitor, this.ModRegistry);
this.InitializePerformanceCounterEvents(); this.PerformanceCounterManager = new PerformanceCounterManager(this.EventManager);
SCore.DeprecationManager = new DeprecationManager(this.Monitor, this.ModRegistry); SCore.DeprecationManager = new DeprecationManager(this.Monitor, this.ModRegistry);
@ -206,69 +206,6 @@ namespace StardewModdingAPI.Framework
#endif #endif
} }
private void InitializePerformanceCounterEvents()
{
this.PerformanceCounterEvents = new HashSet<EventPerformanceCounterCategory>()
{
new EventPerformanceCounterCategory(this.EventManager.MenuChanged, false),
// Rendering Events
new EventPerformanceCounterCategory(this.EventManager.Rendering, true),
new EventPerformanceCounterCategory(this.EventManager.Rendered, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingWorld, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedWorld, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingActiveMenu, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedActiveMenu, true),
new EventPerformanceCounterCategory(this.EventManager.RenderingHud, true),
new EventPerformanceCounterCategory(this.EventManager.RenderedHud, true),
new EventPerformanceCounterCategory(this.EventManager.WindowResized, false),
new EventPerformanceCounterCategory(this.EventManager.GameLaunched, false),
new EventPerformanceCounterCategory(this.EventManager.UpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.UpdateTicked, true),
new EventPerformanceCounterCategory(this.EventManager.OneSecondUpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.OneSecondUpdateTicked, true),
new EventPerformanceCounterCategory(this.EventManager.SaveCreating, false),
new EventPerformanceCounterCategory(this.EventManager.SaveCreated, false),
new EventPerformanceCounterCategory(this.EventManager.Saving, false),
new EventPerformanceCounterCategory(this.EventManager.Saved, false),
new EventPerformanceCounterCategory(this.EventManager.DayStarted, false),
new EventPerformanceCounterCategory(this.EventManager.DayEnding, false),
new EventPerformanceCounterCategory(this.EventManager.TimeChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ReturnedToTitle, false),
new EventPerformanceCounterCategory(this.EventManager.ButtonPressed, true),
new EventPerformanceCounterCategory(this.EventManager.ButtonReleased, true),
new EventPerformanceCounterCategory(this.EventManager.CursorMoved, true),
new EventPerformanceCounterCategory(this.EventManager.MouseWheelScrolled, true),
new EventPerformanceCounterCategory(this.EventManager.PeerContextReceived, true),
new EventPerformanceCounterCategory(this.EventManager.ModMessageReceived, true),
new EventPerformanceCounterCategory(this.EventManager.PeerDisconnected, true),
new EventPerformanceCounterCategory(this.EventManager.InventoryChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LevelChanged, true),
new EventPerformanceCounterCategory(this.EventManager.Warped, true),
new EventPerformanceCounterCategory(this.EventManager.LocationListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.BuildingListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LocationListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.DebrisListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LargeTerrainFeatureListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.NpcListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ObjectListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.ChestInventoryChanged, true),
new EventPerformanceCounterCategory(this.EventManager.TerrainFeatureListChanged, true),
new EventPerformanceCounterCategory(this.EventManager.LoadStageChanged, false),
new EventPerformanceCounterCategory(this.EventManager.UnvalidatedUpdateTicking, true),
new EventPerformanceCounterCategory(this.EventManager.UnvalidatedUpdateTicked, true),
};
}
/// <summary>Launch SMAPI.</summary> /// <summary>Launch SMAPI.</summary>
[HandleProcessCorruptedStateExceptions, SecurityCritical] // let try..catch handle corrupted state exceptions [HandleProcessCorruptedStateExceptions, SecurityCritical] // let try..catch handle corrupted state exceptions
public void RunInteractively() public void RunInteractively()
@ -1471,7 +1408,7 @@ namespace StardewModdingAPI.Framework
} }
else else
{ {
var data = this.PerformanceCounterEvents.Where(p => p.Event.GetEventName().ToLowerInvariant().Contains(filterByName.ToLowerInvariant())); var data = this.PerformanceCounterManager.PerformanceCounterEvents.Where(p => p.Event.GetEventName().ToLowerInvariant().Contains(filterByName.ToLowerInvariant()));
foreach (var i in data) foreach (var i in data)
{ {
@ -1479,8 +1416,8 @@ namespace StardewModdingAPI.Framework
} }
} }
double avgTime = PerformanceCounter.Stopwatch.ElapsedMilliseconds / (double)PerformanceCounter.EventsLogged; double avgTime = PerformanceCounter.PerformanceCounter.Stopwatch.ElapsedMilliseconds / (double)PerformanceCounter.PerformanceCounter.TotalNumEventsLogged;
this.Monitor.Log($"Logged {PerformanceCounter.EventsLogged} events in {PerformanceCounter.Stopwatch.ElapsedMilliseconds}ms (avg {avgTime:F4}ms / event)"); this.Monitor.Log($"Logged {PerformanceCounter.PerformanceCounter.TotalNumEventsLogged} events in {PerformanceCounter.PerformanceCounter.Stopwatch.ElapsedMilliseconds}ms (avg {avgTime:F4}ms / event)");
} }
@ -1492,17 +1429,17 @@ namespace StardewModdingAPI.Framework
if (eventNameFilter != null) if (eventNameFilter != null)
{ {
data = this.PerformanceCounterEvents.Where(p => p.Event.GetEventName().ToLowerInvariant().Contains(eventNameFilter.ToLowerInvariant())); data = this.PerformanceCounterManager.PerformanceCounterEvents.Where(p => p.Event.GetEventName().ToLowerInvariant().Contains(eventNameFilter.ToLowerInvariant()));
} }
else else
{ {
if (showOnlyImportant) if (showOnlyImportant)
{ {
data = this.PerformanceCounterEvents.Where(p => p.IsImportant); data = this.PerformanceCounterManager.PerformanceCounterEvents.Where(p => p.IsImportant);
} }
else else
{ {
data = this.PerformanceCounterEvents; data = this.PerformanceCounterManager.PerformanceCounterEvents;
} }
} }

View File

@ -11,6 +11,7 @@ using StardewModdingAPI.Framework;
using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Toolkit.Utilities;
[assembly: InternalsVisibleTo("SMAPI.Tests")] [assembly: InternalsVisibleTo("SMAPI.Tests")]
[assembly: InternalsVisibleTo("SMAPI.Mods.ConsoleCommands")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // Moq for unit testing [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // Moq for unit testing
namespace StardewModdingAPI namespace StardewModdingAPI
{ {