Performance einer Anwendung testen

Nachfolgend eine kleine Helper-Klasse, mit der man ganz einfach die Performance einer Applikation testen kann.

[sourcecode language=“csharp“]
using System;
using System.Runtime.InteropServices;

/// <summary>
/// Basic PerformanceTimer by using Kernel32.dll.
/// </summary>
/// <example>
/// PerformanceTiming.Start();
/// double seconds = PerformanceTiming.Stop();
/// </example>
public class PerformanceTiming
{
/// <summary>
/// Queries the performance counter.
/// </summary>
/// <param name="nPfCt">The n pf ct.</param>
/// <returns></returns>
[DllImport("KERNEL32")]
public static extern bool QueryPerformanceCounter(ref Int64 nPfCt);

/// <summary>
/// Queries the performance frequency.
/// </summary>
/// <param name="nPfFreq">The n pf freq.</param>
/// <returns></returns>
[DllImport("KERNEL32")]
public static extern bool QueryPerformanceFrequency(ref Int64 nPfFreq);

protected Int64 Mi64Frequency;
protected Int64 Mi64Start;

public PerformanceTiming()
{
QueryPerformanceFrequency(ref Mi64Frequency);
Mi64Start = 0;
}

/// <summary>
/// Starts the performance counter.
/// </summary>
public void Start()
{
QueryPerformanceCounter(ref Mi64Start);
}

/// <summary>
/// Ends the performance counter and returns the number of seconds the counter has measured.
/// </summary>
/// <returns></returns>
public double End()
{
Int64 i64End = 0;
QueryPerformanceCounter(ref i64End);
return ((i64End – Mi64Start) / (double)Mi64Frequency);
}
}
[/sourcecode]

Viel Spaß!

Leave a reply