| | | 1 | | using System.Threading; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal; |
| | | 4 | | |
| | | 5 | | internal sealed class TelegramHandlerRequestTimingScope : IDisposable |
| | | 6 | | { |
| | 1 | 7 | | private static readonly AsyncLocal<TelegramHandlerRequestTimingScope?> CurrentSlot = new(); |
| | | 8 | | |
| | | 9 | | private readonly TelegramHandlerRequestTimingScope? _previous; |
| | 10 | 10 | | private readonly List<RequestInterval> _intervals = []; |
| | | 11 | | private bool _disposed; |
| | | 12 | | |
| | | 13 | | private TelegramHandlerRequestTimingScope(TelegramHandlerRequestTimingScope? previous) |
| | | 14 | | { |
| | 10 | 15 | | _previous = previous; |
| | 10 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static TelegramHandlerRequestTimingScope Begin() |
| | | 19 | | { |
| | 10 | 20 | | var scope = new TelegramHandlerRequestTimingScope(CurrentSlot.Value); |
| | 10 | 21 | | CurrentSlot.Value = scope; |
| | 10 | 22 | | return scope; |
| | | 23 | | } |
| | | 24 | | |
| | 83 | 25 | | public static bool HasCurrent => CurrentSlot.Value is not null; |
| | | 26 | | |
| | | 27 | | public static void Record(long startTimestamp, long endTimestamp) |
| | | 28 | | { |
| | 4 | 29 | | if (endTimestamp < startTimestamp) |
| | | 30 | | { |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 4 | 34 | | CurrentSlot.Value?._intervals.Add(new RequestInterval(startTimestamp, endTimestamp)); |
| | 4 | 35 | | } |
| | | 36 | | |
| | | 37 | | public TelegramHandlerRequestTimingSummary CreateSummary( |
| | | 38 | | TimeProvider timeProvider, |
| | | 39 | | TimeSpan handlerElapsed) |
| | | 40 | | { |
| | 10 | 41 | | ArgumentNullException.ThrowIfNull(timeProvider); |
| | | 42 | | |
| | 10 | 43 | | var requestWait = CalculateMergedRequestWait(timeProvider); |
| | 10 | 44 | | var logic = handlerElapsed - requestWait; |
| | | 45 | | |
| | 10 | 46 | | if (logic < TimeSpan.Zero) |
| | | 47 | | { |
| | 0 | 48 | | logic = TimeSpan.Zero; |
| | | 49 | | } |
| | | 50 | | |
| | 10 | 51 | | return new TelegramHandlerRequestTimingSummary( |
| | 10 | 52 | | _intervals.Count, |
| | 10 | 53 | | requestWait.TotalMilliseconds, |
| | 10 | 54 | | logic.TotalMilliseconds); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public void Dispose() |
| | | 58 | | { |
| | 10 | 59 | | if (_disposed) |
| | | 60 | | { |
| | 0 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 10 | 64 | | _disposed = true; |
| | 10 | 65 | | CurrentSlot.Value = _previous; |
| | 10 | 66 | | } |
| | | 67 | | |
| | | 68 | | private TimeSpan CalculateMergedRequestWait(TimeProvider timeProvider) |
| | | 69 | | { |
| | 10 | 70 | | if (_intervals.Count == 0) |
| | | 71 | | { |
| | 7 | 72 | | return TimeSpan.Zero; |
| | | 73 | | } |
| | | 74 | | |
| | 3 | 75 | | var ordered = _intervals |
| | 2 | 76 | | .OrderBy(static interval => interval.StartTimestamp) |
| | 3 | 77 | | .ToArray(); |
| | 3 | 78 | | var currentStart = ordered[0].StartTimestamp; |
| | 3 | 79 | | var currentEnd = ordered[0].EndTimestamp; |
| | 3 | 80 | | var total = TimeSpan.Zero; |
| | | 81 | | |
| | 8 | 82 | | for (var index = 1; index < ordered.Length; index++) |
| | | 83 | | { |
| | 1 | 84 | | var interval = ordered[index]; |
| | | 85 | | |
| | 1 | 86 | | if (interval.StartTimestamp <= currentEnd) |
| | | 87 | | { |
| | 0 | 88 | | if (interval.EndTimestamp > currentEnd) |
| | | 89 | | { |
| | 0 | 90 | | currentEnd = interval.EndTimestamp; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | continue; |
| | | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | total += timeProvider.GetElapsedTime(currentStart, currentEnd); |
| | 1 | 97 | | currentStart = interval.StartTimestamp; |
| | 1 | 98 | | currentEnd = interval.EndTimestamp; |
| | | 99 | | } |
| | | 100 | | |
| | 3 | 101 | | total += timeProvider.GetElapsedTime(currentStart, currentEnd); |
| | 3 | 102 | | return total; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private readonly record struct RequestInterval(long StartTimestamp, long EndTimestamp); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal readonly record struct TelegramHandlerRequestTimingSummary( |
| | | 109 | | int RequestCount, |
| | | 110 | | double RequestElapsedMilliseconds, |
| | | 111 | | double HandlerLogicElapsedMilliseconds); |