75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
|
|
namespace YwxAppWpfBarrage.Services
|
|
{
|
|
public class BarragePerformanceMonitor
|
|
{
|
|
private readonly Stopwatch _stopwatch = new Stopwatch();
|
|
private readonly List<Exception> _errors = new List<Exception>();
|
|
|
|
private int _totalBarrageAdded;
|
|
private int _totalBarrageFiltered;
|
|
private int _totalBarrageRemoved;
|
|
|
|
public void Start()
|
|
{
|
|
_stopwatch.Start();
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
_stopwatch.Stop();
|
|
}
|
|
|
|
public void RecordAddedBarrage()
|
|
{
|
|
_totalBarrageAdded++;
|
|
}
|
|
|
|
public void RecordFilteredBarrage()
|
|
{
|
|
_totalBarrageFiltered++;
|
|
}
|
|
|
|
public void RecordRemovedBarrage()
|
|
{
|
|
_totalBarrageRemoved++;
|
|
}
|
|
|
|
public void RecordClear()
|
|
{
|
|
_totalBarrageRemoved += _totalBarrageAdded - _totalBarrageRemoved;
|
|
}
|
|
|
|
public void RecordError(Exception ex)
|
|
{
|
|
_errors.Add(ex);
|
|
}
|
|
|
|
public PerformanceStats GetStats()
|
|
{
|
|
return new PerformanceStats
|
|
{
|
|
TotalTime = _stopwatch.Elapsed,
|
|
TotalBarrageAdded = _totalBarrageAdded,
|
|
TotalBarrageFiltered = _totalBarrageFiltered,
|
|
TotalBarrageRemoved = _totalBarrageRemoved,
|
|
Errors = _errors.ToArray(),
|
|
BarragePerSecond = _stopwatch.Elapsed.TotalSeconds > 0 ?
|
|
_totalBarrageAdded / _stopwatch.Elapsed.TotalSeconds : 0
|
|
};
|
|
}
|
|
|
|
public class PerformanceStats
|
|
{
|
|
public TimeSpan TotalTime { get; set; }
|
|
public int TotalBarrageAdded { get; set; }
|
|
public int TotalBarrageFiltered { get; set; }
|
|
public int TotalBarrageRemoved { get; set; }
|
|
public Exception[] Errors { get; set; }
|
|
public double BarragePerSecond { get; set; }
|
|
}
|
|
}
|
|
} |