From 1b905205a3073c56e29c46b5e57c4a9cb2ca5832 Mon Sep 17 00:00:00 2001 From: Drachenkaetzchen Date: Tue, 21 Jan 2020 12:20:06 +0100 Subject: Added commands to enable and disable performance counters. Peak is now using the default interval --- .../PerformanceCounter/PerformanceCounter.cs | 2 +- .../PerformanceCounterCollection.cs | 31 ++++++++++++++++++++++ .../PerformanceCounterManager.cs | 19 +++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounter.cs b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounter.cs index 3d902e16..e9dfcb14 100644 --- a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounter.cs +++ b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounter.cs @@ -133,7 +133,7 @@ namespace StardewModdingAPI.Framework.PerformanceCounter DateTime start = relativeTo.Value.Subtract(range); - var entries = this._counter.Where(x => (x.EventTime >= start) && (x.EventTime <= relativeTo)); + var entries = this._counter.Where(x => (x.EventTime >= start) && (x.EventTime <= relativeTo)).ToList(); if (!entries.Any()) return 0; diff --git a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterCollection.cs b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterCollection.cs index fe14ebf8..f469eceb 100644 --- a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterCollection.cs +++ b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterCollection.cs @@ -82,6 +82,15 @@ namespace StardewModdingAPI.Framework.PerformanceCounter p.Key != Constants.GamePerformanceCounterName).Sum(p => p.Value.GetAverage()); } + /// Returns the average execution time for all non-game internal sources. + /// The interval for which to get the average, relative to now + /// The average execution time in milliseconds + public double GetModsAverageExecutionTime(TimeSpan interval) + { + return this.PerformanceCounters.Where(p => + p.Key != Constants.GamePerformanceCounterName).Sum(p => p.Value.GetAverage(interval)); + } + /// Returns the overall average execution time. /// The average execution time in milliseconds public double GetAverageExecutionTime() @@ -89,6 +98,14 @@ namespace StardewModdingAPI.Framework.PerformanceCounter return this.PerformanceCounters.Sum(p => p.Value.GetAverage()); } + /// Returns the overall average execution time. + /// The interval for which to get the average, relative to now + /// The average execution time in milliseconds + public double GetAverageExecutionTime(TimeSpan interval) + { + return this.PerformanceCounters.Sum(p => p.Value.GetAverage(interval)); + } + /// Returns the average execution time for game-internal sources. /// The average execution time in milliseconds public double GetGameAverageExecutionTime() @@ -99,6 +116,20 @@ namespace StardewModdingAPI.Framework.PerformanceCounter return 0; } + /// Returns the average execution time for game-internal sources. + /// The average execution time in milliseconds + public double GetGameAverageExecutionTime(TimeSpan interval) + { + if (this.PerformanceCounters.TryGetValue(Constants.GamePerformanceCounterName, out PerformanceCounter gameExecTime)) + return gameExecTime.GetAverage(interval); + + return 0; + } + + /// Returns the peak execution time + /// The interval for which to get the peak, relative to + /// The DateTime which the is relative to, or DateTime.Now if not given + /// The peak execution time public double GetPeakExecutionTime(TimeSpan range, DateTime? relativeTo = null) { if (this.PeakInvocations.IsEmpty) diff --git a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterManager.cs b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterManager.cs index a8e20eda..bd964442 100644 --- a/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterManager.cs +++ b/src/SMAPI/Framework/PerformanceCounter/PerformanceCounterManager.cs @@ -23,6 +23,9 @@ namespace StardewModdingAPI.Framework.PerformanceCounter /// Specifies if alerts should be paused. public bool PauseAlerts { get; set; } + /// Specifies if performance counter tracking should be enabled. + public bool EnableTracking { get; set; } + /// Constructs a performance counter manager. /// The monitor for output logging. public PerformanceCounterManager(IMonitor monitor) @@ -49,6 +52,11 @@ namespace StardewModdingAPI.Framework.PerformanceCounter /// The collection name public void BeginTrackInvocation(string collectionName) { + if (!this.EnableTracking) + { + return; + } + this.GetOrCreateCollectionByName(collectionName).BeginTrackInvocation(); } @@ -56,6 +64,11 @@ namespace StardewModdingAPI.Framework.PerformanceCounter /// public void EndTrackInvocation(string collectionName) { + if (!this.EnableTracking) + { + return; + } + this.GetOrCreateCollectionByName(collectionName).EndTrackInvocation(); } @@ -65,6 +78,12 @@ namespace StardewModdingAPI.Framework.PerformanceCounter /// The action to execute and track invocation time for. public void Track(string collectionName, string sourceName, Action action) { + if (!this.EnableTracking) + { + action(); + return; + } + DateTime eventTime = DateTime.UtcNow; this.InvocationStopwatch.Reset(); this.InvocationStopwatch.Start(); -- cgit