summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
authorDrachenkaetzchen <felicia@drachenkatze.org>2020-01-21 12:20:06 +0100
committerDrachenkaetzchen <felicia@drachenkatze.org>2020-01-21 12:20:06 +0100
commit1b905205a3073c56e29c46b5e57c4a9cb2ca5832 (patch)
tree677358e51b8b3a8ba37ffd480f4a12778a2cafd9 /src/SMAPI/Framework
parent84973ce5727ad20fe8b8ba4f89e59c8b754f799e (diff)
downloadSMAPI-1b905205a3073c56e29c46b5e57c4a9cb2ca5832.tar.gz
SMAPI-1b905205a3073c56e29c46b5e57c4a9cb2ca5832.tar.bz2
SMAPI-1b905205a3073c56e29c46b5e57c4a9cb2ca5832.zip
Added commands to enable and disable performance counters. Peak is now using the default interval
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/PerformanceCounter/PerformanceCounter.cs2
-rw-r--r--src/SMAPI/Framework/PerformanceCounter/PerformanceCounterCollection.cs31
-rw-r--r--src/SMAPI/Framework/PerformanceCounter/PerformanceCounterManager.cs19
3 files changed, 51 insertions, 1 deletions
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());
}
+ /// <summary>Returns the average execution time for all non-game internal sources.</summary>
+ /// <param name="interval">The interval for which to get the average, relative to now</param>
+ /// <returns>The average execution time in milliseconds</returns>
+ public double GetModsAverageExecutionTime(TimeSpan interval)
+ {
+ return this.PerformanceCounters.Where(p =>
+ p.Key != Constants.GamePerformanceCounterName).Sum(p => p.Value.GetAverage(interval));
+ }
+
/// <summary>Returns the overall average execution time.</summary>
/// <returns>The average execution time in milliseconds</returns>
public double GetAverageExecutionTime()
@@ -89,6 +98,14 @@ namespace StardewModdingAPI.Framework.PerformanceCounter
return this.PerformanceCounters.Sum(p => p.Value.GetAverage());
}
+ /// <summary>Returns the overall average execution time.</summary>
+ /// <param name="interval">The interval for which to get the average, relative to now</param>
+ /// <returns>The average execution time in milliseconds</returns>
+ public double GetAverageExecutionTime(TimeSpan interval)
+ {
+ return this.PerformanceCounters.Sum(p => p.Value.GetAverage(interval));
+ }
+
/// <summary>Returns the average execution time for game-internal sources.</summary>
/// <returns>The average execution time in milliseconds</returns>
public double GetGameAverageExecutionTime()
@@ -99,6 +116,20 @@ namespace StardewModdingAPI.Framework.PerformanceCounter
return 0;
}
+ /// <summary>Returns the average execution time for game-internal sources.</summary>
+ /// <returns>The average execution time in milliseconds</returns>
+ public double GetGameAverageExecutionTime(TimeSpan interval)
+ {
+ if (this.PerformanceCounters.TryGetValue(Constants.GamePerformanceCounterName, out PerformanceCounter gameExecTime))
+ return gameExecTime.GetAverage(interval);
+
+ return 0;
+ }
+
+ /// <summary>Returns the peak execution time</summary>
+ /// <param name="interval">The interval for which to get the peak, relative to <paramref name="relativeTo"/></param>
+ /// <param name="relativeTo">The DateTime which the <paramref name="interval"/> is relative to, or DateTime.Now if not given</param>
+ /// <returns>The peak execution time</returns>
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
/// <summary>Specifies if alerts should be paused.</summary>
public bool PauseAlerts { get; set; }
+ /// <summary>Specifies if performance counter tracking should be enabled.</summary>
+ public bool EnableTracking { get; set; }
+
/// <summary>Constructs a performance counter manager.</summary>
/// <param name="monitor">The monitor for output logging.</param>
public PerformanceCounterManager(IMonitor monitor)
@@ -49,6 +52,11 @@ namespace StardewModdingAPI.Framework.PerformanceCounter
/// <param name="collectionName">The collection name</param>
public void BeginTrackInvocation(string collectionName)
{
+ if (!this.EnableTracking)
+ {
+ return;
+ }
+
this.GetOrCreateCollectionByName(collectionName).BeginTrackInvocation();
}
@@ -56,6 +64,11 @@ namespace StardewModdingAPI.Framework.PerformanceCounter
/// <param name="collectionName"></param>
public void EndTrackInvocation(string collectionName)
{
+ if (!this.EnableTracking)
+ {
+ return;
+ }
+
this.GetOrCreateCollectionByName(collectionName).EndTrackInvocation();
}
@@ -65,6 +78,12 @@ namespace StardewModdingAPI.Framework.PerformanceCounter
/// <param name="action">The action to execute and track invocation time for.</param>
public void Track(string collectionName, string sourceName, Action action)
{
+ if (!this.EnableTracking)
+ {
+ action();
+ return;
+ }
+
DateTime eventTime = DateTime.UtcNow;
this.InvocationStopwatch.Reset();
this.InvocationStopwatch.Start();