Skip to content

Commit

Permalink
New and improved console output
Browse files Browse the repository at this point in the history
  • Loading branch information
WillSoss committed Feb 7, 2024
1 parent 2f37df4 commit d597194
Show file tree
Hide file tree
Showing 12 changed files with 474 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/Diagnostics/DiagnosticJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public override Task<string> GetItemIdAsync(DiagnosticItem item)
return Task.FromResult(item.Id);
}

public override Task<Result> ProcessAsync(DiagnosticItem item)
public override async Task<Result> ProcessAsync(DiagnosticItem item)
{
if (Config.MillisecondDelayPerItem > 0)
Thread.Sleep(Config.MillisecondDelayPerItem);
await Task.Delay(Config.MillisecondDelayPerItem);

if (Config.WaitForSignalInProcessAsync)
signal.WaitOne();
Expand All @@ -104,7 +104,7 @@ public override Task<Result> ProcessAsync(DiagnosticItem item)
if (Config.ThrowExceptionInProcessAsync)
throw new DiagnosticJobException(JobMethod.ProcessAsync, "Exception thrown because Config.ThrowExceptionInProcess is true.");

return Task.FromResult(item.IsSuccessful ? Result.Success(item.Category) : Result.Failure(item.Category));
return item.IsSuccessful ? Result.Success(item.Category) : Result.Failure(item.Category);
}

public override Task<object> FinalizeAsync(Disposition disposition)
Expand Down
2 changes: 1 addition & 1 deletion src/Diagnostics/Diagnostics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>Runly.Diagnostics</AssemblyName>
<RootNamespace>Runly.Diagnostics</RootNamespace>
Expand Down
8 changes: 8 additions & 0 deletions src/Diagnostics/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Diagnostics": {
"commandName": "Project",
"commandLineArgs": "diagnosticjob --milliseconddelayperitem 10 --numberofitems 5000"
}
}
}
64 changes: 58 additions & 6 deletions src/Runly/Execution.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Runly.Testing;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -12,9 +13,44 @@ namespace Runly
public abstract class Execution
{
/// <summary>
/// Event raised when execution of a job starts.
/// Gets the overall and last minute average time for an item to be processed.
/// </summary>
public event Func<string, DateTime, Task> Started;
public Average ItemProcessingTime { get; } = new Average("Item Processing Time");

/// <summary>
/// Gets a list of <see cref="MethodOutcome">MethodOutcomes</see> for each method of a job, excluding methods executed per item.
/// </summary>
public Dictionary<JobMethod, MethodOutcome> Methods { get; } = new Dictionary<JobMethod, MethodOutcome>();

/// <summary>
/// Gets the <see cref="MethodOutcome"/> for InitializeAsync.
/// </summary>
public MethodOutcome InitializeAsync => Methods.ValueOrDefault(JobMethod.InitializeAsync);

/// <summary>
/// Gets the <see cref="MethodOutcome"/> for GetItemsAsync.
/// </summary>
public MethodOutcome GetItemsAsync => Methods.ValueOrDefault(JobMethod.GetItemsAsync);

/// <summary>
/// Gets the <see cref="MethodOutcome"/> for Count.
/// </summary>
public MethodOutcome Count => Methods.ValueOrDefault(JobMethod.Count);

/// <summary>
/// Gets the <see cref="MethodOutcome"/> for GetEnumerator.
/// </summary>
public MethodOutcome GetEnumerator => Methods.ValueOrDefault(JobMethod.GetEnumerator);

/// <summary>
/// Gets the <see cref="MethodOutcome"/> for FinalizeAsync.
/// </summary>
public MethodOutcome FinalizeAsync => Methods.ValueOrDefault(JobMethod.FinalizeAsync);

/// <summary>
/// Event raised when execution of a job starts.
/// </summary>
public event Func<string, DateTime, Task> Started;

/// <summary>
/// Event raised when the state of the job changes.
Expand Down Expand Up @@ -237,7 +273,9 @@ protected void SetState(ExecutionState state)
/// </summary>
protected void CompleteMethod(JobMethod method, TimeSpan duration, Exception exception)
{
events.Enqueue(new MethodOutcome(method, duration, exception));
var mo = new MethodOutcome(method, duration, exception);
Methods.Add(method, mo);
events.Enqueue(mo);
}

/// <summary>
Expand Down Expand Up @@ -273,10 +311,16 @@ private async Task RunEventPump(CancellationToken cancellationToken)
while (!cancellationToken.IsCancellationRequested)
{
await PublishEvents();

ItemProcessingTime.Purge();

await Task.Delay(100);
await Task.Delay(50);
}
}

await PublishEvents();

ItemProcessingTime.Purge();
}

private async Task PublishEvents()
{
Expand All @@ -286,6 +330,14 @@ private async Task PublishEvents()
{
try
{
if (@event is ItemResult r)
{
if (r.IsSuccessful)
ItemProcessingTime.RecordSuccess(r.ProcessAsync.Duration);
else
ItemProcessingTime.RecordError();
}

await (@event switch
{
ItemResult result => ItemCompleted != null ? ItemCompleted(result) : Task.CompletedTask,
Expand Down
Loading

0 comments on commit d597194

Please sign in to comment.