如何为 .Net 应用程序编写性能测试? nUnit 或任何其他测试框架是否为此提供了框架?
编辑 :我必须衡量 WCF 服务的性能。
最佳答案
如果您对方法和算法的相对性能感兴趣,可以在 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");
}
关于.net - 如何为 .Net 应用程序编写性能测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2449870/