Skip to content

Commit

Permalink
Add Timings to CommandSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 8, 2024
1 parent 45da359 commit c6c1336
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 57 deletions.
46 changes: 0 additions & 46 deletions MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandResult> CommandResults { get; set; } = [];
public ConcurrentQueue<CommandResult> CommandFailures { get; set; } = new();

public ConcurrentDictionary<string, CommandSummary> 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;
});
}
}
}
29 changes: 18 additions & 11 deletions MyApp.ServiceInterface/IAsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Limit API access to users in role
/// </summary>
Expand Down Expand Up @@ -214,23 +219,22 @@ async Task Exec(object commandArg)
}
}

public const int DefaultCapacity = 250;
public ConcurrentQueue<CommandResult> CommandResults { get; set; } = [];
public ConcurrentQueue<CommandResult> CommandFailures { get; set; } = new();

public ConcurrentDictionary<string, CommandSummary> CommandTotals { get; set; } = new();

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++;
Expand All @@ -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,
Expand Down Expand Up @@ -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<int> Timings { get; set; } = new();
}

[Tag(Tag.Tasks)]
[ExcludeMetadata]
public class ViewCommands : IGet, IReturn<ViewCommandsResponse>
{
Expand Down

0 comments on commit c6c1336

Please sign in to comment.