Skip to content

Commit

Permalink
updated readme and samples
Browse files Browse the repository at this point in the history
  • Loading branch information
migajek committed Jan 5, 2024
1 parent e15171a commit 298da1e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

PicoProfiler is a tiny abstraction layer over built-in `Stopwatch`, leveraging `IDisposable + using` pattern

It's intended use is to measure the time of execution of given block of code and store it somewhere - usually the log entry, hence the built-in integration with `Microsoft.Extensions.Logging`.

Just wrap the code you want to measure in `using (_logger.StartProfiler())` (or use `using declaration` as below):
```c#
private async Task ProcessRules(object[] objects)
{
using var _ = _logger.StartProfiler();

foreach (var o in objects)
{
using (_logger.StartProfiler($"Processing rule {o}", logLevel: LogLevel.Trace))
{
await Task.Delay(15);
}
}
}
```

results in:
```
trce: PicoSampleApp.AlmostRealLifeService[0]
Processing rule 0 finished in 28.01 ms
trce: PicoSampleApp.AlmostRealLifeService[0]
Processing rule 1 finished in 15.26 ms
trce: PicoSampleApp.AlmostRealLifeService[0]
Processing rule 2 finished in 16.06 ms
info: PicoSampleApp.AlmostRealLifeService[0]
ProcessRules finished in 59.74 ms
```

## Getting started

### Microsoft.Extensions.Logging integration
Expand Down
43 changes: 43 additions & 0 deletions samples/PicoSampleApp/AlmostRealLifeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.Extensions.Logging;
using PicoProfiler.Logging;

namespace PicoSampleApp;

internal class AlmostRealLifeService
{
private readonly ILogger<AlmostRealLifeService> _logger;

public AlmostRealLifeService(ILogger<AlmostRealLifeService> logger)
{
_logger = logger;
}

public async Task HandleMessage()
{
var data = await FetchHelperData();
await ProcessRules(data);
}

private async Task<object[]> FetchHelperData()
{
using var _ = _logger.StartProfiler();
await Task.Delay(283); // fair roll dice, as you know it.

var results = Enumerable.Range(0, 3).Cast<object>().ToArray();
_logger.LogInformation("Returning {HelperDataCount} results", results.Length);
return results;
}

private async Task ProcessRules(object[] objects)
{
using var _ = _logger.StartProfiler();

foreach (var o in objects)
{
using (_logger.StartProfiler($"Processing rule {o}", logLevel: LogLevel.Trace))
{
await Task.Delay(15);
}
}
}
}
8 changes: 7 additions & 1 deletion samples/PicoSampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ internal class Program
{
static async Task Main(string[] args)
{
// 1. Plain sample
await PlainSample();

// 2. Console output
await RunConsoleSample();

// 3. Logging output
using var loggerFactory = CreateLoggerFactory();
await RunLoggingSample(loggerFactory.CreateLogger<Program>());

// 4. real life usage scenarios
await new AlmostRealLifeService(loggerFactory.CreateLogger<AlmostRealLifeService>()).HandleMessage();
}

private static async Task PlainSample()
Expand All @@ -37,7 +43,7 @@ private static async Task RunLoggingSample(ILogger logger)
}

private static ILoggerFactory CreateLoggerFactory() => LoggerFactory
.Create(lb => lb.AddConsole(cfg => { }));
.Create(lb => lb.SetMinimumLevel(LogLevel.Trace).AddConsole(cfg => { }));

private static async Task MyTimeConsumingWork() => await Task.Delay(TimeSpan.FromMilliseconds(374));
}

0 comments on commit 298da1e

Please sign in to comment.