PerformanceTest Class

Provides a class for performance tests of Action delegate test cases.

Definition

Namespace: KGySoft.Diagnostics
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.1.0
C#
public class PerformanceTest : PerformanceTestBase<Action, Object>
Inheritance
Object    PerformanceTestBase    PerformanceTestBaseAction, Object    PerformanceTest

Example

The following example shows the simplest usage for timed tests.

C#
using System;
using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;

class Example
{
    static void Main(string[] args)
    {
        new PerformanceTest()
            .AddCase(() => ConsoleColor.Black.ToString(), "Enum.ToString")
            .AddCase(() => Enum<ConsoleColor>.ToString(ConsoleColor.Black), "Enum<TEnum>.ToString")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces an output similar to the following one:
// ==[Performance Test Results]================================================
// Test Time: 2,000 ms
// Warming up: Yes
// Test cases: 2
// Calling GC.Collect: Yes
// Forced CPU Affinity: No
// Cases are sorted by fulfilled iterations (the most first)
// --------------------------------------------------
// 1. Enum<TEnum>.ToString: 26,104,501 iterations in 2,000.00 ms. Adjusted for 2,000 ms: 26,104,498.39
// 2. Enum.ToString: 3,956,036 iterations in 2,000.01 ms. Adjusted for 2,000 ms: 3,956,026.31 (-22,148,472.08 / 15.15 %)

Each test case can be repeated multiple times. To see the costs of the first execution the default warming up session can be disabled:

C#
using System;
using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;

class Example
{
    static void Main(string[] args)
    {
        new PerformanceTest
            {
                TestName = "System.Enum vs. KGySoft.CoreLibraries.Enum<TEnum>",
                TestTime = 2000,
                WarmUp = false,
                Repeat = 2
            }
            .AddCase(() => ConsoleColor.Black.ToString(), "Enum.ToString")
            .AddCase(() => Enum<ConsoleColor>.ToString(ConsoleColor.Black), "Enum<TEnum>.ToString")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces an output similar to the following one:
// ==[System.Enum vs. KGySoft.CoreLibraries.Enum<TEnum> Results]================================================
// Test Time: 2,000 ms
// Warming up: No
// Test cases: 2
// Repeats: 2
// Calling GC.Collect: Yes
// Forced CPU Affinity: No
// Cases are sorted by fulfilled iterations (the most first)
// --------------------------------------------------
// 1. Enum<TEnum>.ToString: 57,500,126 iterations in 4,000.00 ms. Adjusted for 2,000 ms: 28,750,060.12
//   #1  28,730,396 iterations in 2,000.00 ms. Adjusted: 28,730,393.13      <---- Worst
//   #2  28,769,730 iterations in 2,000.00 ms. Adjusted: 28,769,727.12      <---- Best
//   Worst-Best difference: 39,334.00 (0.14 %)
// 2. Enum.ToString: 7,618,943 iterations in 4,000.01 ms. Adjusted for 2,000 ms: 3,809,466.01 (-24,940,594.12 / 13.25 %)
//   #1  3,786,163 iterations in 2,000.01 ms. Adjusted: 3,786,152.78        <---- Worst
//   #2  3,832,780 iterations in 2,000.00 ms. Adjusted: 3,832,779.23        <---- Best
//   Worst-Best difference: 46,626.46 (1.23 %)

By specifying Iterations you can constrain to execute the test cases for a fix number of times instead of executing them for the specified time period:

C#
using System;
using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;

class Example
{
    static void Main(string[] args)
    {
        new PerformanceTest { Iterations = 10000 }
            .AddCase(() => ConsoleColor.Black.ToString(), "Enum.ToString")
            .AddCase(() => Enum<ConsoleColor>.ToString(ConsoleColor.Black), "Enum<TEnum>.ToString")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces an output similar to the following one:
// ==[Performance Test Results]================================================
// Iterations: 10,000
// Warming up: Yes
// Test cases: 2
// Calling GC.Collect: Yes
// Forced CPU Affinity: 2
// Cases are sorted by time (quickest first)
// --------------------------------------------------
// 1. Enum<TEnum>.ToString: average time: 0.23 ms
// 2. Enum.ToString: average time: 4.50 ms (+4.27 ms / 1,994.32 %)

Similarly to time-based tests, you can increase number of repetitions:

C#
using System;
using KGySoft.CoreLibraries;
using KGySoft.Diagnostics;

class Example
{
    static void Main(string[] args)
    {
        new PerformanceTest
            {
                WarmUp = false,
                Iterations = 10000,
                Repeat = 5
            }
            .AddCase(() => ConsoleColor.Black.ToString(), "Enum.ToString")
            .AddCase(() => Enum<ConsoleColor>.ToString(ConsoleColor.Black), "Enum<TEnum>.ToString")
            .DoTest()
            .DumpResults(Console.Out);
    }
}

// This code example produces an output similar to the following one:
// ==[Performance Test Results]================================================
// Iterations: 10,000
// Warming up: No
// Test cases: 2
// Repeats: 5
// Calling GC.Collect: Yes
// Forced CPU Affinity: No
// Cases are sorted by time (quickest first)
// --------------------------------------------------
// 1. Enum<TEnum>.ToString: average time: 1.66 ms
//   #1           7.39 ms   <---- Worst
//   #2           0.22 ms   <---- Best
//   #3           0.22 ms
//   #4           0.23 ms
//   #5           0.22 ms
//   Worst-Best difference: 7.17 ms (3,266.23 %)
// 2. Enum.ToString: average time: 5.00 ms (+3.35 ms / 302.10 %)
//   #1           5.25 ms
//   #2           4.40 ms   <---- Best
//   #3           4.54 ms
//   #4           5.97 ms   <---- Worst
//   #5           4.85 ms
//   Worst-Best difference: 1.58 ms (35.86 %)

Constructors

PerformanceTestInitializes a new instance of the PerformanceTest class

Properties

Collect Gets or sets whether GC.Collect should be called before running the test cases.
Default value: .
(Inherited from PerformanceTestBase)
CpuAffinity Gets or sets the CPU affinity to be used for executing tests. If , or is too large for the executing system, then the affinity is not adjusted for the test.
Default value: null.
(Inherited from PerformanceTestBase)
Iterations Gets or sets number of iterations of test cases. If greater than zero, then TestTime is ignored.
Default value: 0.
(Inherited from PerformanceTestBase)
Repeat Gets or sets how many times the test cases should be repeated.
Default value: 1.
(Inherited from PerformanceTestBase)
SortBySize Gets or sets whether the results should be sorted by the size of the produced result instead of iterations count or time results. Makes sense only if the test delegate has a return type and the returned value of a test case is always the same for each run.
Default value: .
(Inherited from PerformanceTestBase)
TestName Gets or sets the name of the test.
(Inherited from PerformanceTestBase)
TestTime Gets or sets the test duration, in milliseconds, for each test case and the warming-up sessions. If Iterations is greater than zero, then this property affects only the warm-up time.
Default value: 2000.
(Inherited from PerformanceTestBase)
WarmUp Gets or sets whether there is an untested warm-up session before each test. Its duration equals to TestTime.
Default value: .
(Inherited from PerformanceTestBase)

Methods

AddCase Adds a test case to the test suit.
See the Examples section of the PerformanceTest class for some examples.
(Inherited from PerformanceTestBaseTDelegate, TResult)
AsString Gets the string representation of the specified result.
(Inherited from PerformanceTestBaseTDelegate, TResult)
DoTest Performs the test and returns the test results.
(Inherited from PerformanceTestBaseTDelegate, TResult)
GetLength Gets the length of the result in any unit specified by the GetUnit method.
The base implementation returns element count if TResult is IEnumerable; otherwise, the size of TResult in bytes (which is 4 or 8 bytes for reference types, depending on the platform target).
(Inherited from PerformanceTestBaseTDelegate, TResult)
GetUnit Gets the length unit name of TResult.
The base implementation returns the element name if TResult is IEnumerableT (C# alias name, if applicable); a localized string for item, if TResult is a non-generic IEnumerable; otherwise, a localized string for byte.
(Inherited from PerformanceTestBaseTDelegate, TResult)
Invoke Invokes the specified delegate.
(Overrides PerformanceTestBaseTDelegate, TResultInvoke(TDelegate))
OnAfterCase Raises the AfterCase event. Called after each repetition of a test case, including the warming-up session.
(Inherited from PerformanceTestBaseTDelegate, TResult)
OnBeforeCase Raises the BeforeCase event. Called before each repetition of a test case, including the warming-up session.
(Inherited from PerformanceTestBaseTDelegate, TResult)
OnInitialize Raises the Initialize event. This method is called before running the test cases.
(Inherited from PerformanceTestBaseTDelegate, TResult)
OnTearDown Raises the TearDown event. Called after running the tests, even after a failure.
(Inherited from PerformanceTestBaseTDelegate, TResult)

Events

AfterCase Occurs after each repetition of a test case, including the warming-up session.
(Inherited from PerformanceTestBaseTDelegate, TResult)
BeforeCase Occurs before each repetition of a test case, including the warming-up session.
(Inherited from PerformanceTestBaseTDelegate, TResult)
Initialize Occurs before running the test cases.
(Inherited from PerformanceTestBaseTDelegate, TResult)
TearDown Occurs after running the tests, even after a failure.
(Inherited from PerformanceTestBaseTDelegate, TResult)

Extension Methods

Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)

See Also