KGy SOFT

PerformanceTest Class

KGy SOFT Core Libraries Help
Provides a class for performance tests of Action delegate test cases.
See the Examples section for some examples.
Inheritance Hierarchy

SystemObject
  KGySoft.DiagnosticsPerformanceTestBase
    KGySoft.DiagnosticsPerformanceTestBaseAction, Object
      KGySoft.DiagnosticsPerformanceTest

Namespace:  KGySoft.Diagnostics
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public class PerformanceTest : PerformanceTestBase<Action, Object>

The PerformanceTest type exposes the following members.

Constructors

  NameDescription
Public methodPerformanceTest
Initializes a new instance of the PerformanceTest class
Top
Properties

  NameDescription
Public propertyCollect
Gets or sets whether GC.Collect should be called before running the test cases.
Default value: .
(Inherited from PerformanceTestBase.)
Public propertyCpuAffinity
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: 2.
(Inherited from PerformanceTestBase.)
Public propertyIterations
Gets or sets number of iterations of test cases. If greater than zero, then TestTime is ignored.
Default value: 0.
(Inherited from PerformanceTestBase.)
Public propertyRepeat
Gets or sets how many times the test cases should be repeated.
Default value: 1.
(Inherited from PerformanceTestBase.)
Public propertySortBySize
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.)
Public propertyTestName
Gets or sets the name of the test.
(Inherited from PerformanceTestBase.)
Public propertyTestTime
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 is ignored.
Default value: 2000.
(Inherited from PerformanceTestBase.)
Public propertyWarmUp
Gets or sets whether there is an untested warm-up session before each test. Its duration or iteration count equals to TestTime or Iterations, respectively.
Default value: .
(Inherited from PerformanceTestBase.)
Top
Methods

  NameDescription
Public methodAddCase
Adds a test case to the test suit.
See the Examples section of the PerformanceTest class for some examples.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Protected methodAsString
Gets the string representation of the specified result.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Public methodDoTest
Performs the test and returns the test results.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Protected methodGetLength
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.)
Protected methodGetUnit
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.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodInvoke
Invokes the specified delegate.
(Overrides PerformanceTestBaseTDelegate, TResultInvoke(TDelegate).)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnAfterCase
Called after each repetition of a test case.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Protected methodOnBeforeCase
Called before each repetition of a test case.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Protected methodOnInitialize
Called before running the test cases.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Protected methodOnTearDown
Called after running the tests, even after a failure.
(Inherited from PerformanceTestBaseTDelegate, TResult.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
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.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
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.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
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.)
Top
Examples

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: 2
// Cases are sorted by fulfilled iterations (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: 2
// Cases are sorted by fulfilled iterations (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:

Tip Tip
Try also online.
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: 2
// 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 %)

See Also

Reference