如何为.Net应用程序编写性能测试?

Ami*_*abh 4 .net performance wcf nunit

如何为.Net应用程序编写性能测试?nUnit或任何其他测试框架是否为此提供了框架?

编辑:我必须衡量WCF服务的性能.

Pad*_*ker 10

如果您对方法和算法的相对性能感兴趣,可以在NUnit测试中使用System.Diagnostic.StopWatch类来编写有关某些方法需要多长时间的断言.

在下面的简单示例中,primes类使用[SetUp]方法(未显示)进行实例化,因为我对generatePrimes方法所花费的时间感兴趣,而不是我的类的实例化,并且我正在编写一个断言这种方法应该少于5秒.这不是一个非常具有挑战性的断言,但希望可以作为你如何做到这一点的一个例子.

    [Test]
    public void checkGeneratePrimesUpToTenMillion()
    {
        System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
        timer.Start();
        long[] primeArray = primes.generatePrimes(10000000);
        timer.Stop();
        Assert.AreEqual(664579, primeArray.Length, "Should be 664,579 primes below ten million");
        int elapsedSeconds = timer.Elapsed.Seconds;
        Console.Write("Time in seconds to generate primes up to ten million: " + elapsedSeconds);
        bool ExecutionTimeLessThanFiveSeconds = (elapsedSeconds < 5);
        Assert.IsTrue(ExecutionTimeLessThanFiveSeconds, "Should take less than five seconds");
    }
Run Code Online (Sandbox Code Playgroud)


Ami*_*abh 2

我发现 NTime 看起来很适合编写性能测试。

http://www.codeproject.com/kb/dotnet/NTime.aspx