Search Results for

    Show / Hide Table of Contents

    Class Profiler

    Helper methods to measure code execution time.

    Inheritance
    Object
    Profiler
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: BionicCode.Utilities.Net
    Assembly: BionicCode.Utilities.Net.Common.dll
    Syntax
    public static class Profiler

    Methods

    | Improve this Doc View Source

    LogTime(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 action should be executed.

    Returns
    Type Description
    ProfilerBatchResult

    The average execution time of all runCount number of iterations as TimeSpan.

    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 ofrunCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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 action should be executed.

    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    Returns
    Type Description
    ProfilerBatchResult

    The average execution time of all runCount number of iterations as TimeSpan.

    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 ofrunCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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 action should be executed.

    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    Returns
    Type Description
    Task<ProfilerBatchResult>

    The average execution time of all runCount number of iterations as TimeSpan.

    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 ofrunCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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 asyncAction should be executed.

    Returns
    Type Description
    Task<ProfilerBatchResult>

    A Task holding the average execution time of all runCount number of iterations as TimeSpan.

    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.

    Use the LogTimeAsync(Func<Task>, 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 runCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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 asyncAction should be executed.

    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    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 runCount number of iterations.

    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.

    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 ofrunCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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 asyncAction should be executed.

    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    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 runCount number of iterations.

    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.

    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 runCount is not between '0' and 'ulong.MaxValue'.

    | Improve this Doc View Source

    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    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:


              ╭───────────────┬────────────────────────────┬────────────────╮
              | 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 │                │
    ╰─────────────────────────┴────────────────────────────┴────────────────╯
    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
    In This Article
    Back to top Generated by DocFX