Revert "relieve gc pressure maybe"

This reverts commit 40b8310ba9.
This commit is contained in:
Loporrit
2024-02-10 18:45:18 +00:00
parent 40b8310ba9
commit 5d2b25bccb
5 changed files with 68 additions and 85 deletions

View File

@@ -3,6 +3,7 @@ using MareSynchronos.Utils;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Text;
@@ -13,7 +14,7 @@ public sealed class PerformanceCollectorService : IHostedService
private const string _counterSplit = "=>";
private readonly ILogger<PerformanceCollectorService> _logger;
private readonly MareConfigService _mareConfigService;
public ConcurrentDictionary<string, RollingList<Tuple<TimeOnly, long>>> PerformanceCounters { get; } = new(StringComparer.Ordinal);
private readonly ConcurrentDictionary<string, RollingList<Tuple<TimeOnly, long>>> _performanceCounters = new(StringComparer.Ordinal);
private readonly CancellationTokenSource _periodicLogPruneTask = new();
public PerformanceCollectorService(ILogger<PerformanceCollectorService> logger, MareConfigService mareConfigService)
@@ -22,57 +23,49 @@ public sealed class PerformanceCollectorService : IHostedService
_mareConfigService = mareConfigService;
}
public T LogPerformance<T>(object sender, string counterName, Func<T> func, int maxEntries = 10000)
public T LogPerformance<T>(object sender, string counterName, Func<T> func)
{
if (!_mareConfigService.Current.LogPerformance) return func.Invoke();
counterName = sender.GetType().Name + _counterSplit + counterName;
if (!PerformanceCounters.TryGetValue(counterName, out var list))
if (!_performanceCounters.TryGetValue(counterName, out var list))
{
list = PerformanceCounters[counterName] = new(maxEntries);
list = _performanceCounters[counterName] = new(10000);
}
var dt = DateTime.UtcNow.Ticks;
Stopwatch st = Stopwatch.StartNew();
try
{
return func.Invoke();
}
finally
{
var elapsed = DateTime.UtcNow.Ticks - dt;
#if DEBUG
if (TimeSpan.FromTicks(elapsed) > TimeSpan.FromMilliseconds(10))
_logger.LogWarning(">10ms spike on {counterName}: {time}", counterName, TimeSpan.FromTicks(elapsed));
#endif
list.Add(new(TimeOnly.FromDateTime(DateTime.Now), elapsed));
st.Stop();
list.Add(new(TimeOnly.FromDateTime(DateTime.Now), st.ElapsedTicks));
}
}
public void LogPerformance(object sender, string counterName, Action act, int maxEntries = 10000)
public void LogPerformance(object sender, string counterName, Action act)
{
if (!_mareConfigService.Current.LogPerformance) { act.Invoke(); return; }
counterName = sender.GetType().Name + _counterSplit + counterName;
if (!PerformanceCounters.TryGetValue(counterName, out var list))
if (!_performanceCounters.TryGetValue(counterName, out var list))
{
list = PerformanceCounters[counterName] = new(maxEntries);
list = _performanceCounters[counterName] = new(10000);
}
var dt = DateTime.UtcNow.Ticks;
Stopwatch st = Stopwatch.StartNew();
try
{
act.Invoke();
}
finally
{
var elapsed = DateTime.UtcNow.Ticks - dt;
#if DEBUG
if (TimeSpan.FromTicks(elapsed) > TimeSpan.FromMilliseconds(10))
_logger.LogWarning(">10ms spike on {counterName}: {time}", counterName, TimeSpan.FromTicks(elapsed));
#endif
list.Add(new(TimeOnly.FromDateTime(DateTime.Now), elapsed));
st.Stop();
list.Add(new(TimeOnly.FromDateTime(DateTime.Now), st.ElapsedTicks));
}
}
@@ -104,7 +97,7 @@ public sealed class PerformanceCollectorService : IHostedService
{
sb.AppendLine("Performance metrics over total lifetime of each counter");
}
var data = PerformanceCounters.ToList();
var data = _performanceCounters.ToList();
var longestCounterName = data.OrderByDescending(d => d.Key.Length).First().Key.Length + 2;
sb.Append("-Last".PadRight(15, '-'));
sb.Append('|');
@@ -176,12 +169,12 @@ public sealed class PerformanceCollectorService : IHostedService
{
await Task.Delay(TimeSpan.FromMinutes(10), _periodicLogPruneTask.Token).ConfigureAwait(false);
foreach (var entries in PerformanceCounters.ToList())
foreach (var entries in _performanceCounters.ToList())
{
try
{
var last = entries.Value.ToList().Last();
if (last.Item1.AddMinutes(10) < TimeOnly.FromDateTime(DateTime.Now) && !PerformanceCounters.TryRemove(entries.Key, out _))
if (last.Item1.AddMinutes(10) < TimeOnly.FromDateTime(DateTime.Now) && !_performanceCounters.TryRemove(entries.Key, out _))
{
_logger.LogDebug("Could not remove performance counter {counter}", entries.Key);
}