【问题标题】:What is a good way to create non-flaky performance tests什么是创建非不稳定性能测试的好方法
【发布时间】:2013-09-18 20:25:11
【问题描述】:

我写了一些常用的方法,我发现性能非常重要。在进行了一些更改以修复明显的性能错误之后,我喜欢进行一些测试以验证性能不会由于未来的更改而降低。但是,我发现这些测试通常非常不稳定(可能是由于垃圾收集、我们的自动化测试服务器上的其他活动等)。我想知道的是,编写和维护此类测试是否有公认的最佳实践?

到目前为止,我的大部分测试如下所示:

runMyCode(); // for assembly loading/jitting
var sw = Stopwatch.StartNew();
for (iterations) { runMyCode(); }
var time = sw.Elapsed;

// then either
Assert.Less(time, TimeSpan.FromSeconds(some reasonably generous amount of time));
// or
// time some other piece of benchmark code (e. g. a framework method 
// which runMyCode() layers on top of). And do:
Assert.Less(time.TotalSeconds, someMultiplier * benchmarkTime.TotalSeconds);

我认为我必须提高稳定性的一种方法是让测试存储在数据库中记录时间,并且只有在最后 N 次未通过基准测试时才会失败。

【问题讨论】:

标签: c# benchmarking performance-testing


【解决方案1】:

看这篇文章:Learn how to create correct C# benchmarks。为您提供的主要提示:

  • 您应该使用处理器缓存的预热来获得正确的结果

  • 您应该在单个处理器上运行代码 (Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1))

  • 你应该为线程和进程设置高优先级

  • 你应该在基准测试之前运行GC.Collect()

  • 您应该多次运行基准测试并获取结果的中位数

    您还可以使用这个免费的开源 .NET 框架来创建足够的基准:BenchmarkDotNet

【讨论】:

    【解决方案2】:

    我不知道您使用的是 Visual Studio 还是 Mono Develop,但您可以使用以下速度分析器工具:

    红门:http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/?utm_source=google&utm_medium=cpc&utm_content=unmet_need&utm_campaign=antsperformanceprofiler&gclid=CPfS1Z_k1bkCFYuk4Aoda3oAVg

    或使用默认的 Visual Studio 性能分析器。请参阅本教程:http://msdn.microsoft.com/en-us/library/ms182372.aspx(更多内容在互联网上)。

    【讨论】:

    • 正如我试图在我的问题中陈述的那样,我不希望进行一次分析运行,而是通过自动化测试来防止未来的性能下降。这些工具能做到吗?
    • 对不起,我的误解。红门可以做到这一点。对于视觉工作室性能分析器,我从未尝试过这种测试。您可以尝试这两种方法(vsp 集成在 Visual Studio 中,redgate 为您提供了一个完整的试用版)
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2021-08-18
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多