BenchmarkDotNet v0.13.2新增功能
前言
在更新BenchmarkDotNet源码时发现已经发布V0.13.2版本了,主要增加了这些特性:- 支持.Net 7
- 支持NativeAOT
- 支持.Net Framework 4.8.1(支持ARM)
- 支持隐藏指定列
- 显示内存分配百分比
- 显示进度和预计完成时间
- 显示硬件指令集信息(如AVX2等)

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
namespace CSharpBenchmarks.MathTest
{
[DisassemblyDiagnoser(printSource: true, maxDepth: 3)]
[MemoryDiagnoser]
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.NativeAot70)] //提示不支持"Currently NativeAOT has no DisassemblyDiagnoser support"
[HideColumns(Column.Gen2)] //隐藏GC2代列
public class RoundTest
{
[Params(1.5)]
public double Value { get; set; }
//[Benchmark]
//public double ToEvenTest() => Math.Round(Value, MidpointRounding.ToEven);
//[Benchmark]
//public double AwayFromZeroTest() => Math.Round(Value, MidpointRounding.AwayFromZero);
[Benchmark(Baseline = true)]
public double RoundDefault() => Math.Round(Value);
}
}
还有对WebAssembly支持的越来越好,输出的信息更多更全,还有显示更多的汇编代码(在测试方法中调用的其他方法显示汇编代码):
; 内联后生成的汇编代码
; CSharpBenchmarks.MathTest.RoundTest.RoundDefault()
; public double RoundDefault() => Math.Round(Value);
; ^^^^^^^^^^^^^^^^^
vzeroupper
vroundsd xmm0,xmm0,qword ptr[rcx + 8],4
ret
; Total bytes of code 11
在看在测试方法中调用其他方法:
; net6perf.LinqTest.TakTest.Take()
; int sum = 0;
; ^^^^^^^^^^^^
; for (int i = 0; i < Times; i++)
; ^^^^^^^^^
; var array = bytes.Take(5).ToArray();
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; sum += array.Length;
; ^^^^^^^^^^^^^^^^^^^^
; return sum;
; ^^^^^^^^^^^push r14
push rdi
push rsi
push rbp
push rbx
sub rsp,20
mov rsi, rcx
xor edi, edi
xor ebx, ebx
cmp dword ptr [rsi+10],0
jle near ptr M00_L03
M00_L00:
mov rbp,[rsi + 8]
test rbp, rbp
je near ptr M00_L04
mov rdx, rbp
mov rcx, offset MT_System.Linq.IPartition`1[[System.Int32, System.Private.CoreLib]]
; ===========调用IsInstanceOfInterface方法===========
call qword ptr [System.Runtime.CompilerServices.CastHelpers.IsInstanceOfInterface(Void *, System.Object)]
test rax, rax
jne short M00_L01
mov rcx, offset MT_System.Linq.Enumerable+ListPartition`1[[System.Int32, System.Private.CoreLib]]
call CORINFO_HELP_NEWSFAST
mov r14, rax
call CORINFO_HELP_GETCURRENTMANAGEDTHREADID
mov [r14+8],eax
lea rcx,[r14 + 18]
mov rdx, rbp
call CORINFO_HELP_ASSIGN_REF
xor ecx, ecx
mov [r14+14],ecx
mov dword ptr [r14+20],4
jmp short M00_L02
M00_L01:
mov rcx, rax
mov r11,7FFE65150440
mov edx,5
call qword ptr [r11]
mov r14, rax
M00_L02:
mov rcx, r14
call qword ptr [System.Linq.Enumerable.ToArray[[System.Int32, System.Private.CoreLib]](System.Collections.Generic.IEnumerable`1 < Int32 >)]
add edi,[rax + 8]
inc ebx
cmp ebx,[rsi + 10]
jl near ptr M00_L00
M00_L03:
mov eax, edi
add rsp,20
pop rbx
pop rbp
pop rsi
pop rdi
pop r14
ret
M00_L04:
mov ecx,10
call qword ptr [7FFE65535780]
int 3
; Total bytes of code 191
; System.Runtime.CompilerServices.CastHelpers.IsInstanceOfInterface(Void *, System.Object)
test rdx, rdx
je short M01_L03
mov rax,[rdx]
movzx r8d, word ptr [rax+0E]
test r8, r8
je short M01_L02
mov r9,[rax + 38]
cmp r8,4
jl short M01_L01
M01_L00:
cmp[r9],rcx
je short M01_L03
cmp [r9+8],rcx
je short M01_L03
cmp [r9+10],rcx
je short M01_L03
cmp [r9+18],rcx
je short M01_L03
add r9,20
add r8,0FFFFFFFFFFFFFFFC
cmp r8,4
jge short M01_L00
test r8, r8
je short M01_L02
M01_L01:
cmp[r9],rcx
je short M01_L03
add r9,8
dec r8
test r8, r8
jg short M01_L01
M01_L02:
test dword ptr [rax],406C0000
jne short M01_L04
xor edx, edx
M01_L03:
mov rax, rdx
ret
M01_L04:
jmp qword ptr [System.Runtime.CompilerServices.CastHelpers.IsInstance_Helper(Void *, System.Object)]
; Total bytes of code 107
秋风
2022-08-29