Class Profiler
Helper methods to measure code execution time.
Inherited Members
Namespace: BionicCode.Utilities.Net
Assembly: BionicCode.Utilities.Net.Common.dll
Syntax
public static class Profiler
Methods
| Improve this Doc View SourceLogTime(Action, Int32)
Measures the execution time of a method.
Declaration
public static ProfilerBatchResult LogTime(Action action, int runCount)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The code to measure execution time. |
| Int32 | runCount | Number of iterations the |
Returns
| Type | Description |
|---|---|
| ProfilerBatchResult | The average execution time of all |
Remarks
Use the LogTime(Action, Int32, ProfilerLoggerDelegate) overload and provide an instance of the ProfilerLoggerDelegate delegate for the logger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTime(Action, Int32, ProfilerLoggerDelegate)
Measures the execution time of a method.
Declaration
public static ProfilerBatchResult LogTime(Action action, int runCount, ProfilerLoggerDelegate logger)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The code to measure execution time. |
| Int32 | runCount | Number of iterations the |
| ProfilerLoggerDelegate | logger | A delegate of type ProfilerLoggerDelegate which can be used to automatically let the LogTime(Action, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
Returns
| Type | Description |
|---|---|
| ProfilerBatchResult | The average execution time of all |
Remarks
Provide an instance of the ProfilerLoggerDelegate delegate for the logger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTimeAsync(Action, Int32, ProfilerLoggerAsyncDelegate)
Measures the execution time of a method.
Declaration
public static async Task<ProfilerBatchResult> LogTimeAsync(Action action, int runCount, ProfilerLoggerAsyncDelegate asyncLogger)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The code to measure execution time. |
| Int32 | runCount | Number of iterations the |
| ProfilerLoggerAsyncDelegate | asyncLogger | A delegate of type ProfilerLoggerAsyncDelegate which can be used to automatically let the LogTime(Action, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
Returns
| Type | Description |
|---|---|
| Task<ProfilerBatchResult> | The average execution time of all |
Remarks
Provide an instance of the ProfilerLoggerAsyncDelegate delegate for the asyncLogger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTimeAsync(Func<Task>, Int32)
Measures the execution time of a method.
Declaration
public static async Task<ProfilerBatchResult> LogTimeAsync(Func<Task> asyncAction, int runCount)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | asyncAction | A delegate that executes the asynchronous code to measure execution time. |
| Int32 | runCount | Number of iterations the |
Returns
| Type | Description |
|---|---|
| Task<ProfilerBatchResult> | A Task holding the average execution time of all |
Remarks
Cancelled tasks are ignored when calculating the result (although the cancelled runs are listed in the Summary, but marked as cancelled).
A cancelled task is a Task where the Status returns either Canceled or Faulted or an OperationCanceledException exception was thrown.
logger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTimeAsync(Func<Task>, Int32, ProfilerLoggerAsyncDelegate)
Measures the execution time of a method.
Declaration
public static async Task<ProfilerBatchResult> LogTimeAsync(Func<Task> asyncAction, int runCount, ProfilerLoggerAsyncDelegate asyncLogger)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | asyncAction | A delegate that executes the asynchronous code to measure execution time. |
| Int32 | runCount | Number of iterations the |
| ProfilerLoggerAsyncDelegate | asyncLogger | An asynchronous delegate of type ProfilerLoggerAsyncDelegate which can be used to automatically let the LogTimeAsync(Func<Task>, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
Returns
| Type | Description |
|---|---|
| Task<ProfilerBatchResult> | A Task holding the ProfilerBatchResult result which contains neta data like average execution time or a formatted report (Summary) of all |
Remarks
Cancelled tasks are ignored when calculating the result (although the cancelled runs are listed in the Summary, but marked as cancelled).
A cancelled task is a Task where the Status returns either Canceled or Faulted or an OperationCanceledException exception was thrown.
asyncLogger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTimeAsync(Func<Task>, Int32, ProfilerLoggerDelegate)
Measures the execution time of a method.
Declaration
public static async Task<ProfilerBatchResult> LogTimeAsync(Func<Task> asyncAction, int runCount, ProfilerLoggerDelegate logger)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | asyncAction | A delegate that executes the asynchronous code to measure execution time. |
| Int32 | runCount | Number of iterations the |
| ProfilerLoggerDelegate | logger | A delegate of type ProfilerLoggerDelegate which can be used to automatically let the LogTimeAsync(Func<Task>, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
Returns
| Type | Description |
|---|---|
| Task<ProfilerBatchResult> | A Task holding the ProfilerBatchResult result which contains neta data like average execution time or a formatted report (Summary) of all |
Remarks
Cancelled tasks are ignored when calculating the result (although the cancelled runs are listed in the Summary, but marked as cancelled).
A cancelled task is a Task where the Status returns either Canceled or Faulted or an OperationCanceledException exception was thrown.
logger parameter to control the output target or customize the formatting.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when the value of |
LogTimeScoped(out ProfilerBatchResult)
Measures the execution time of a using block i.e. the scope of the IDisposable's lifetime.
Declaration
public static IDisposable LogTimeScoped(out ProfilerBatchResult result)
Parameters
| Type | Name | Description |
|---|---|---|
| ProfilerBatchResult | result | A Task holding the ProfilerBatchResult result which contains meta data like average execution time or a formatted report (Summary). |
Returns
| Type | Description |
|---|---|
| IDisposable | An IDisposable to control the scope of the profiling. |
Remarks
Time is measured during the lifetime of the IDisposable instance: from time of creation to the final Dispose() call.
It's recommended to use the IDisposable managed by a using-statement or using-expression.
Examples
public static void Main(string[] args)
{
ProfilerBatchResult result;
using (Profiler.LogTimeScoped(out result))
{
FibonacciSeries(0, 1, 1, 100);
}
// Print a preformatted summary
Console.Write(result.Summary);
// Print the average duration
Console.Write(result.AverageDuration);
}
public static void FibonacciSeries(int firstNumber, int secondNumber, int counter, int number)
{
Console.Write(firstNumber + " ");
if (counter < number)
{
FibonacciSeries(secondNumber, firstNumber + secondNumber, counter + 1, number);
}
}
Output:
╭───────────────┬────────────────────────────┬────────────────╮
| Iteration # │ Duration [ms] │ Is cancelled |
┝━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━┥
│ 1 │ 1.0512 │ False |
│ 2 │ 1.0020 │ False │
│ 3 │ 0.8732 │ False │
│ 4 │ 0.0258 │ True (ignored) │
│ 5 │ 0.9943 │ False │
╭═════════╧═══════════════╪════════════════════════════┼────────────────┤
│ Total: - │ 3.9207 │ │
│ Min: 3 │ 0.8732 │ │
│ Max: 1 │ 1.0512 │ │
│ Average: - │ 0.9802 │ │
╰─────────────────────────┴────────────────────────────┴────────────────╯
|
Improve this Doc
View Source
LogTimeScoped(ProfilerLoggerAsyncDelegate, out ProfilerBatchResult)
Measures the execution time of using block i.e. the scope of the IDisposable's lifetime.
Declaration
public static IDisposable LogTimeScoped(ProfilerLoggerAsyncDelegate logger, out ProfilerBatchResult result)
Parameters
| Type | Name | Description |
|---|---|---|
| ProfilerLoggerAsyncDelegate | logger | An asynchronous delegate of type ProfilerLoggerAsyncDelegate which can be used to automatically let the LogTimeAsync(Func<Task>, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
| ProfilerBatchResult | result | A Task holding the ProfilerBatchResult result which contains meta data like average execution time or a formatted report (Summary). |
Returns
| Type | Description |
|---|---|
| IDisposable | An IDisposable to control the scope of the profiling. |
Remarks
Time is measured during the lifetime of the IDisposable instance: from time of creation to the final Dispose() call.
It's recommended to use the IDisposable managed by a using-statement or using-expression.
LogTimeScoped(ProfilerLoggerAsyncDelegate, out ProfilerBatchResult)
Examples
public static void Main(string[] args)
{
ProfilerBatchResult result;
using (Profiler.LogTimeScoped(LogToFileAsync, out result))
{
FibonacciSeries(0, 1, 1, 100);
}
// Print a preformatted summary
Console.Write(result.Summary);
// Print the average duration
Console.Write(result.AverageDuration);
}
private static async Task LogToFileAsync(ProfilerBatchResult result, string summary)
{
await File.WriteAllTextAsync(summary, @"c:\profiler_log.txt");
}
public static void FibonacciSeries(int firstNumber, int secondNumber, int counter, int number)
{
Console.Write(firstNumber + " ");
if (counter < number)
{
FibonacciSeries(secondNumber, firstNumber + secondNumber, counter + 1, number);
}
}
Output:
╭───────────────┬────────────────────────────┬────────────────╮
| Iteration # │ Duration [ms] │ Is cancelled |
┝━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━┥
│ 1 │ 1.0512 │ False |
│ 2 │ 1.0020 │ False │
│ 3 │ 0.8732 │ False │
│ 4 │ 0.0258 │ True (ignored) │
│ 5 │ 0.9943 │ False │
╭═════════╧═══════════════╪════════════════════════════┼────────────────┤
│ Total: - │ 3.9207 │ │
│ Min: 3 │ 0.8732 │ │
│ Max: 1 │ 1.0512 │ │
│ Average: - │ 0.9802 │ │
╰─────────────────────────┴────────────────────────────┴────────────────╯
|
Improve this Doc
View Source
LogTimeScoped(ProfilerLoggerDelegate, out ProfilerBatchResult)
Measures the execution time of using block i.e. the scope of the IDisposable's lifetime.
Declaration
public static IDisposable LogTimeScoped(ProfilerLoggerDelegate logger, out ProfilerBatchResult result)
Parameters
| Type | Name | Description |
|---|---|---|
| ProfilerLoggerDelegate | logger | A delegate of type ProfilerLoggerDelegate which can be used to automatically let the LogTimeAsync(Func<Task>, Int32, ProfilerLoggerDelegate) print the Summary to a destination (e.g. file) in the following formatting:
|
| ProfilerBatchResult | result | A Task holding the ProfilerBatchResult result which contains meta data like average execution time or a formatted report (Summary). |
Returns
| Type | Description |
|---|---|
| IDisposable | An IDisposable to control the scope of the profiling. |
Remarks
Time is measured during the lifetime of the IDisposable instance: from time of creation to the final Dispose() call.
It's recommended to use the IDisposable managed by a using-statement or using-expression.
Examples
public static void Main(string[] args)
{
ProfilerBatchResult result;
using (Profiler.LogTimeScoped(out result))
{
FibonacciSeries(0, 1, 1, 100);
}
// Print a preformatted summary
Console.Write(result.Summary);
// Print the average duration
Console.Write(result.AverageDuration);
}
public static void FibonacciSeries(int firstNumber, int secondNumber, int counter, int number)
{
Console.Write(firstNumber + " ");
if (counter < number)
{
FibonacciSeries(secondNumber, firstNumber + secondNumber, counter + 1, number);
}
}
Output:
╭───────────────┬────────────────────────────┬────────────────╮
| Iteration # │ Duration [ms] │ Is cancelled |
┝━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━┥
│ 1 │ 1.0512 │ False |
│ 2 │ 1.0020 │ False │
│ 3 │ 0.8732 │ False │
│ 4 │ 0.0258 │ True (ignored) │
│ 5 │ 0.9943 │ False │
╭═════════╧═══════════════╪════════════════════════════┼────────────────┤
│ Total: - │ 3.9207 │ │
│ Min: 3 │ 0.8732 │ │
│ Max: 1 │ 1.0512 │ │
│ Average: - │ 0.9802 │ │
╰─────────────────────────┴────────────────────────────┴────────────────╯