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: 5.0.0-rc.1
Syntax

public abstract class PerformanceTestBase<TDelegate, TResult> : PerformanceTestBase

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: 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.
Protected methodAsString
Gets the string representation of the specified result.
Public methodDoTest
Performs the test and returns the test results.
(Overrides PerformanceTestBaseDoTest.)
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).
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.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodInvoke
Invokes the specified delegate.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnAfterCase
Called after each repetition of a test case.
Protected methodOnBeforeCase
Called before each repetition of a test case.
Protected methodOnInitialize
Called before running the test cases.
Protected methodOnTearDown
Called after running the tests, even after a failure.
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

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