Files
YwxAppWpfBarrage/YwxAppWpfDanMu/Services/DanMuPerformanceMonitor.cs

75 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace YwxAppWpfDanMu.Services
{
public class DanMuPerformanceMonitor
{
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly List<Exception> _errors = new List<Exception>();
private int _totalDanMuAdded;
private int _totalDanMuFiltered;
private int _totalDanMuRemoved;
public void Start()
{
_stopwatch.Start();
}
public void Stop()
{
_stopwatch.Stop();
}
public void RecordAddedDanMu()
{
_totalDanMuAdded++;
}
public void RecordFilteredDanMu()
{
_totalDanMuFiltered++;
}
public void RecordRemovedDanMu()
{
_totalDanMuRemoved++;
}
public void RecordClear()
{
_totalDanMuRemoved += _totalDanMuAdded - _totalDanMuRemoved;
}
public void RecordError(Exception ex)
{
_errors.Add(ex);
}
public PerformanceStats GetStats()
{
return new PerformanceStats
{
TotalTime = _stopwatch.Elapsed,
TotalDanMuAdded = _totalDanMuAdded,
TotalDanMuFiltered = _totalDanMuFiltered,
TotalDanMuRemoved = _totalDanMuRemoved,
Errors = _errors.ToArray(),
DanMuPerSecond = _stopwatch.Elapsed.TotalSeconds > 0 ?
_totalDanMuAdded / _stopwatch.Elapsed.TotalSeconds : 0
};
}
public class PerformanceStats
{
public TimeSpan TotalTime { get; set; }
public int TotalDanMuAdded { get; set; }
public int TotalDanMuFiltered { get; set; }
public int TotalDanMuRemoved { get; set; }
public Exception[] Errors { get; set; }
public double DanMuPerSecond { get; set; }
}
}
}