Profiler Class

Provides members for performance monitoring.

See the online help for an example with images.

Definition

Namespace: KGySoft.Diagnostics
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0
C#
public static class Profiler
Inheritance
Object    Profiler

Remarks

The Profiler class can be used to place measurement sections into the code. The results can be accessed either directly by the GetMeasurementResults method or when the AutoSaveResults property is , then the results will be dumped into an .XML file into a folder designated by the ProfilerDirectory property.

The profiling can be turned on and off globally by the Enabled property.

  Note

It is recommended to measure performance with Release builds.

Examples

The following example demonstrates how to place measurement sections into the code:
C#
using System;
using System.Threading;
using KGySoft.Diagnostics;

class Example
{
    static void Main(string[] args)
    {
        Profiler.Enabled = true; // set false to turn off profiling
        Profiler.AutoSaveResults = true; // if true a result .XML file is saved on exit

        // put the measurement into a using block. You can specify a category and operation name.
        using (Profiler.Measure(nameof(Example), $"{nameof(Main)} total"))
        {
            for (int i = 0; i < 10; i++)
            {
                using (Profiler.Measure(nameof(Example), $"{nameof(Main)}/1 iteration"))
                {
                    DoSmallTask();
                    DoBigTask();
                }
            }
        }

        // if Profiler.AutoSaveResults is false you might want to check the results here: Profiler.GetMeasurementResults(nameof(Example));
    }

    private static void DoSmallTask()
    {
        using (Profiler.Measure(nameof(Example), nameof(DoSmallTask)))
            Console.WriteLine(nameof(DoSmallTask));
    }

    private static void DoBigTask()
    {
        using (Profiler.Measure(nameof(Example), nameof(DoBigTask)))
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(10);
                DoSmallTask();
            }
        }
    }
}
As a result, in the folder of the application a new Profiler folder has been created with a file named something like [time stamp]_ConsoleApp1.exe.xml with a similar content to the following:
XML
<?xml version="1.0" encoding="utf-8"?>
<ProfilerResult>
  <item Category = "Example" Operation="Main total" NumberOfCalls="1" FirstCall="00:00:00.5500736" TotalTime="00:00:00.5500736" AverageCallTime="00:00:00.5500736" />
  <item Category = "Example" Operation="Main/1 iteration" NumberOfCalls="10" FirstCall="00:00:00.0555439" TotalTime="00:00:00.5500554" AverageCallTime="00:00:00.0550055" />
  <item Category = "Example" Operation="DoSmallTask" NumberOfCalls="60" FirstCall="00:00:00.0005378" TotalTime="00:00:00.0124114" AverageCallTime="00:00:00.0002068" />
  <item Category = "Example" Operation="DoBigTask" NumberOfCalls="10" FirstCall="00:00:00.0546513" TotalTime="00:00:00.5455339" AverageCallTime="00:00:00.0545533" />
</ProfilerResult>
You can open the result even in Microsoft Excel as a table, which allows you to filter and sort the results easily:
The Profiler results opened in Microsoft Excel.

Properties

AutoSaveResults Gets or sets whether results are automatically saved into the directory determined by the ProfilerDirectory property.
Default value:
Enabled Gets or sets whether profiling is enabled.
Default value:
ProfilerDirectory Gets or sets the output folder of the profiler. When or empty value is assigned, sets the Profiler subdirectory of the executing assembly.

Methods

GetMeasurementResult Gets a measurement result as an IMeasureItem instance, or , if the measurement result is not found with the given category and operation.
GetMeasurementResults Gets the measurement results so far.
GetMeasurementResults(String) Gets the measurement results of the given category so far.
Measure If Enabled is , starts a profiling measure. Use in using block.
Reset Resets the profiler results. Every measurement performed earlier will be lost.

See Also