BenchmarkDotNet v0.13.5新增功能

前言

在更新BenchmarkDotNet源码的时候,发现已经更新了两个小版本,让我们看看带来那些功能.

BenchmarkDotNet v0.13.4更新功能:
  1. 修复BenchmarkDotNet在LinqPad中使用
  2. 增加新的JIT诊断工具JitStatsDiagnoser(目前仅支持在Windows使用)功能,
  3. 对文档带来微小的改善

BenchmarkDotNet v0.13.5更新功能:
  1. 改善JitStatsDiagnoser,显示在基准测试期间从JIT编译器收集的统计数据,如(执行方法的次数,分层后执行的次数和方法分配的总内存),并增加了JitStatsDiagnoser这个特性
  2. 在Nuget中生成启用强签名程序集
  3. 避免将基准测试返回的引用值保存在内存中
  4. 在使用MsBuild生成生成日志时,保留生成的日志
  5. 为UnresolvedDiagnoser增加Id
  6. 支持Windows 22H2和Macos 13
  7. 移除不再使用的InProcessToolchain 

JitStatsDiagnoser使用

再次说一下,JitStatsDiagnoser需要使用BenchmarkDotNet.Diagnostics.Windows包,版本要大于等于0.13.5
[MemoryDiagnoser]
[DisassemblyDiagnoser(printSource: true)]
[JitStatsDiagnoser]  // JitStatsDiagnoser这个需要依赖BenchmarkDotNet.Diagnostics.Windows,目前只支持在Windows使用
public class JitInfoTest
{
    [Benchmark]
    public void Sleep() => Thread.Sleep(10);
}

在BenchmarkDotNet使用JitStatsDiagnoser

当使用JitStatsDiagnoser时,我们主要关注3个字段,分别是:

Methods JITted: 测试期间Jit执行方法的总次数(包含预热)

Methods Tiered: JIT分层编译后,执行的次数

JIT allocated memory:在测试期间,该方法在JIT分配的总内存

TailCallDiagnoser使用

[TailCallDiagnoser]
public class TailCallTest
{
    [Benchmark]
    public long Calc()
    => FactorialWithoutTailing(7) - FactorialWithTailing(7);

    private static long FactorialWithoutTailing(int depth)
        => depth == 0 ? 1 : depth * FactorialWithoutTailing(depth - 1);

    private static long FactorialWithTailing(int pos, int depth)
        => pos == 0 ? depth : FactorialWithTailing(pos - 1, depth * pos);

    private static long FactorialWithTailing(int depth)
        => FactorialWithTailing(depth - 1, depth);
}

在BenchmarkDotNet使用TailCallDiagnoser

秋风 2023-02-26