KGy SOFT

PerformanceTestBaseTDelegate, TResult Class

KGy SOFT Core Libraries Help
Provides a base class for performance tests.
See the Examples section for an example.
Inheritance Hierarchy

SystemObject
  KGySoft.DiagnosticsPerformanceTestBase
    KGySoft.DiagnosticsPerformanceTestBaseTDelegate, TResult
      KGySoft.DiagnosticsPerformanceTest
      KGySoft.DiagnosticsPerformanceTestTResult

Namespace:  KGySoft.Diagnostics
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 6.0.0-rc.2
Syntax

public abstract class PerformanceTestBase<TDelegate, TResult> : PerformanceTestBase
where TDelegate : Delegate

Type Parameters

TDelegate
The delegate type of the test cases.
TResult
The type of the result.

The PerformanceTestBaseTDelegate, TResult type exposes the following members.

Constructors

  NameDescription
Protected methodPerformanceTestBaseTDelegate, TResult
Initializes a new instance of the PerformanceTestBaseTDelegate, TResult 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: null.
(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 affects only the warm-up time.
Default value: 2000.
(Inherited from PerformanceTestBase.)
Public propertyWarmUp
Gets or sets whether there is an untested warm-up session before each test. Its duration equals to TestTime.
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.
Protected methodAsString
Gets the string representation of the specified result.
Public methodDoTest
Performs the test and returns the test results.
(Overrides PerformanceTestBaseDoTest.)
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).
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.
Protected methodInvoke
Invokes the specified delegate.
Protected methodOnAfterCase
Raises the AfterCase event. Called after each repetition of a test case, including the warming-up session.
Protected methodOnBeforeCase
Raises the BeforeCase event. Called before each repetition of a test case, including the warming-up session.
Protected methodOnInitialize
Raises the Initialize event. This method is called before running the test cases.
Protected methodOnTearDown
Raises the TearDown event. Called after running the tests, even after a failure.
Top
Events

  NameDescription
Public eventAfterCase
Occurs after each repetition of a test case, including the warming-up session.
Public eventBeforeCase
Occurs before each repetition of a test case, including the warming-up session.
Public eventInitialize
Occurs before running the test cases.
Public eventTearDown
Occurs after running the tests, even after a failure.
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

Tip Tip
Try also online.
The following example demonstrates how to derive this class to create parameterized performance tests.
C#
public class RandomizedPerformanceTest<T> : PerformanceTestBase<Func<Random, T>, T>
{
    private Random random;

    // a fix seed can be specified when initializing the test
    public int Seed { get; set; }

    protected override T Invoke(Func<Random, T> del) => del.Invoke(random);

    // resetting the random instance with the specified seed before executing each case
    protected override void OnBeforeCase() => random = new Random(Seed);
}
And now the delegate of the AddCase method will have a Random parameter can be used in the test cases:
C#
new RandomizedPerformanceTest<string> { Seed = 0, Iterations = 1_000_000 }
    .AddCase(rnd => rnd.NextEnum<ConsoleColor>().ToString(), "Enum.ToString")
    .AddCase(rnd => Enum<ConsoleColor>.ToString(rnd.NextEnum<ConsoleColor>()), "Enum<TEnum>.ToString")
    .DoTest()
    .DumpResults(Console.Out);
See Also

Reference