From c6c13367a8b57a1125f8665834d72b25450aaf46 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Mon, 8 Apr 2024 09:21:16 +0800 Subject: [PATCH] Add Timings to CommandSummary --- MyApp.ServiceInterface/Data/AppConfig.cs | 46 ------------------------ MyApp.ServiceInterface/IAsyncCommand.cs | 29 +++++++++------ 2 files changed, 18 insertions(+), 57 deletions(-) diff --git a/MyApp.ServiceInterface/Data/AppConfig.cs b/MyApp.ServiceInterface/Data/AppConfig.cs index cac8f8e..051720f 100644 --- a/MyApp.ServiceInterface/Data/AppConfig.cs +++ b/MyApp.ServiceInterface/Data/AppConfig.cs @@ -216,50 +216,4 @@ public bool HasUnreadAchievements(string? userName) } public bool IsHuman(string? userName) => userName != null && GetModelUser(userName) == null; - - public const int DefaultCapacity = 250; - public ConcurrentQueue CommandResults { get; set; } = []; - public ConcurrentQueue CommandFailures { get; set; } = new(); - - public ConcurrentDictionary CommandTotals { get; set; } = new(); - - public void AddCommandResult(CommandResult result) - { - var ms = result.Ms ?? 0; - if (result.Error == null) - { - CommandResults.Enqueue(result); - while (CommandResults.Count > DefaultCapacity) - CommandResults.TryDequeue(out _); - - CommandTotals.AddOrUpdate(result.Name, - _ => new CommandSummary { Name = result.Name, Count = 1, TotalMs = ms, MinMs = ms > 0 ? ms : int.MinValue }, - (_, summary) => - { - summary.Count++; - summary.TotalMs += ms; - summary.MaxMs = Math.Max(summary.MaxMs, ms); - if (ms > 0) - { - summary.MinMs = Math.Min(summary.MinMs, ms); - } - return summary; - }); - } - else - { - CommandFailures.Enqueue(result); - while (CommandFailures.Count > DefaultCapacity) - CommandFailures.TryDequeue(out _); - - CommandTotals.AddOrUpdate(result.Name, - _ => new CommandSummary { Name = result.Name, Failed = 1, Count = 0, TotalMs = 0, MinMs = int.MinValue, LastError = result.Error }, - (_, summary) => - { - summary.Failed++; - summary.LastError = result.Error; - return summary; - }); - } - } } diff --git a/MyApp.ServiceInterface/IAsyncCommand.cs b/MyApp.ServiceInterface/IAsyncCommand.cs index 788aab8..70da645 100644 --- a/MyApp.ServiceInterface/IAsyncCommand.cs +++ b/MyApp.ServiceInterface/IAsyncCommand.cs @@ -71,6 +71,11 @@ public class CommandsFeature : IPlugin, IConfigureServices, ServiceStack.Model.I { public string Id => "commands"; + public const int DefaultCapacity = 250; + public int ResultsCapacity { get; set; } = DefaultCapacity; + public int FailuresCapacity { get; set; } = DefaultCapacity; + public int TimingsCapacity { get; set; } = 1000; + /// /// Limit API access to users in role /// @@ -214,7 +219,6 @@ async Task Exec(object commandArg) } } - public const int DefaultCapacity = 250; public ConcurrentQueue CommandResults { get; set; } = []; public ConcurrentQueue CommandFailures { get; set; } = new(); @@ -222,15 +226,15 @@ async Task Exec(object commandArg) public void AddCommandResult(CommandResult result) { - var ms = result.Ms ?? 0; + var ms = (int)(result.Ms ?? 0); if (result.Error == null) { CommandResults.Enqueue(result); - while (CommandResults.Count > DefaultCapacity) + while (CommandResults.Count > ResultsCapacity) CommandResults.TryDequeue(out _); CommandTotals.AddOrUpdate(result.Name, - _ => new CommandSummary { Name = result.Name, Count = 1, TotalMs = ms, MinMs = ms > 0 ? ms : int.MinValue }, + _ => new CommandSummary { Name = result.Name, Count = 1, TotalMs = ms, MinMs = ms > 0 ? ms : int.MinValue, Timings = new([ms]) }, (_, summary) => { summary.Count++; @@ -240,13 +244,16 @@ public void AddCommandResult(CommandResult result) { summary.MinMs = Math.Min(summary.MinMs, ms); } + summary.Timings.Enqueue(ms); + while (summary.Timings.Count > TimingsCapacity) + summary.Timings.TryDequeue(out var _); return summary; }); } else { CommandFailures.Enqueue(result); - while (CommandFailures.Count > DefaultCapacity) + while (CommandFailures.Count > FailuresCapacity) CommandFailures.TryDequeue(out _); CommandTotals.AddOrUpdate(result.Name, @@ -314,16 +321,16 @@ public class CommandResult public class CommandSummary { public string Name { get; set; } - public long Count { get; set; } - public long Failed { get; set; } - public long TotalMs { get; set; } - public long MinMs { get; set; } - public long MaxMs { get; set; } + public int Count { get; set; } + public int Failed { get; set; } + public int TotalMs { get; set; } + public int MinMs { get; set; } + public int MaxMs { get; set; } public int AverageMs => (int) Math.Floor(TotalMs / (double)Count); public string? LastError { get; set; } + public ConcurrentQueue Timings { get; set; } = new(); } -[Tag(Tag.Tasks)] [ExcludeMetadata] public class ViewCommands : IGet, IReturn {