2024-01-08 12:00:55 +08:00
|
|
|
|
using Microsoft.Extensions.ObjectPool;
|
|
|
|
|
using System;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2024-01-08 12:00:55 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
using System.Text;
|
2024-01-08 12:00:55 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Xml.Linq;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
|
|
|
|
|
namespace CodeWalker.Core.Utils
|
|
|
|
|
{
|
2024-01-08 12:00:55 +08:00
|
|
|
|
public class DisposableTimer : Stopwatch, IDisposable, IResettable
|
2023-10-28 03:31:09 +08:00
|
|
|
|
{
|
2024-01-08 12:00:55 +08:00
|
|
|
|
public static event Action<TimeSpan, string> OnDispose;
|
|
|
|
|
public Stopwatch Stopwatch => this;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
|
|
|
|
|
static DisposableTimer()
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
TimerStopped += (timeSpan, name) => Debug.WriteLine($"{name} took {timeSpan.TotalMilliseconds} ms");
|
|
|
|
|
#endif
|
2024-01-08 12:00:55 +08:00
|
|
|
|
OnDispose += (timeSpan, name) => Console.WriteLine($"{name} took {timeSpan.TotalMilliseconds} ms");
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
|
2024-01-08 12:00:55 +08:00
|
|
|
|
public DisposableTimer([CallerMemberName] string name = "") : base()
|
2023-10-28 03:31:09 +08:00
|
|
|
|
{
|
2024-01-08 12:00:55 +08:00
|
|
|
|
Start();
|
2023-10-28 03:31:09 +08:00
|
|
|
|
Name = name;
|
|
|
|
|
}
|
2024-01-08 12:00:55 +08:00
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public DisposableTimer(string name, bool start)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
if (start)
|
|
|
|
|
{
|
2024-01-08 12:00:55 +08:00
|
|
|
|
Start();
|
2024-01-07 02:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-28 03:31:09 +08:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2024-01-08 12:00:55 +08:00
|
|
|
|
Stop();
|
|
|
|
|
OnDispose?.Invoke(Elapsed, Name ?? string.Empty);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryReset()
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SummableDisposableTimer : DisposableTimer
|
|
|
|
|
{
|
|
|
|
|
public event Action<TimeSpan, string> OnDispose;
|
|
|
|
|
|
|
|
|
|
public SummableDisposableTimer([CallerMemberName] string name = "") : base(name)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DisposableTimerSummed : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private long _elapsed;
|
|
|
|
|
public TimeSpan TimeSpan => new TimeSpan(_elapsed);
|
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
|
|
|
|
public DisposableTimerSummed([CallerMemberName] string name = "")
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DisposableTimer GetTimer([CallerMemberName] string name = "")
|
|
|
|
|
{
|
|
|
|
|
var timer = new SummableDisposableTimer(name);
|
|
|
|
|
timer.OnDispose += (time, _) =>
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Add(ref _elapsed, time.Ticks);
|
|
|
|
|
};
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{Name} took {TimeSpan.TotalMilliseconds} ms");
|
|
|
|
|
GC.SuppressFinalize(this);
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|