BenchmarkDotNet v0.12x新增功能
起因
在看.Net?官方博客 .Net 5性能優化?中,發現測試性能的BenchmarkDotNet版本已經是v0.12.1,然后去看BenchmarkDotNet文檔,發現還是有不少新的特性.
v0.12.0
支持多個運行時(API改進),增加對.Net 5支持
支持DotNet創建BenchmarkDotNet項目(項目模版)
增加NativeMemoryProfiler(目前僅支持Windows,需要在Nuget管理器中安裝BenchmarkDotNet.Diagnostics.Windows包,才可以,內部使用EtwProfiler)
增加ThreadingDiagnoser
增加MemoryDiagnoser
對LINQPad 6進行支持,可以在LINQPad 6進行代碼性能測試(LINQPad?要收費版才可以,這里也跳過)
文檔快速搜索
v0.12.1
跨平臺生成匯編代碼
基于事件管道跨平臺Profiler
新的API,使用更方便
支持多個運行時,新增.Net 5
<!--新增.Net 5運行時--> <TargetFrameworks>net5.0;netcoreapp3.1;net48</TargetFrameworks>#.netframework 4.8為基準,測試三個版本 .NetFramework 4.8/.Net Core 3.1和.Net 5 dotnet run -c Release -f net48 --runtimes net48 netcoreapp31 netcoreapp50 --filter ** --joinBenchmarkDotNet項目
先查看.Net 5下,有什么項目模版:
dotnet new -l通過命令安裝模版:
#-i 代表install dotnet new -i BenchmarkDotNet.Templates通過命令卸載安裝過的模版:
#-u 代表卸載 u為uninsall dotnet new -u BenchmarkDotNet.Templates新建Benchmark項目:
#新建BenchmarkDotNet項目 dotnet new benchmark --console-appNativeMemoryProfiler使用
在Nuget管理器中安裝BenchmarkDotNet.Diagnostics.Windows包
執行后生成的結果(沒有執行完成,是因為電腦在運行的時候突然藍屏,懷疑是CPU溫度過高造成的,因為筆記本好幾年沒有換過散熱硅脂了):
看BenchmarkDotNet文檔中代碼:
using System; using System.Drawing; using System.Runtime.InteropServices; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Diagnosers; using BenchmarkDotNet.Diagnostics.Windows.Configs;namespace dotnet_perf {[DisassemblyDiagnoser][NativeMemoryProfiler] //在BenchmarkDotNet.Diagnostics.Windows.Configs命名空間下[MemoryDiagnoser]public class IntroNativeMemory{[Benchmark]public void BitmapWithLeaks(){var flag = new Bitmap(200, 100);var graphics = Graphics.FromImage(flag);var blackPen = new Pen(Color.Black, 3);graphics.DrawLine(blackPen, 100, 100, 500, 100);}[Benchmark]public void Bitmap(){using (var flag = new Bitmap(200, 100)){using (var graphics = Graphics.FromImage(flag)){using (var blackPen = new Pen(Color.Black, 3)){graphics.DrawLine(blackPen, 100, 100, 500, 100);}}}}private const int Size = 20; // Greater value could cause System.OutOfMemoryException for test with memory leaks.private int ArraySize = Size * Marshal.SizeOf(typeof(int));[Benchmark]public unsafe void AllocHGlobal(){IntPtr unmanagedHandle = Marshal.AllocHGlobal(ArraySize);Span<byte> unmanaged = new Span<byte>(unmanagedHandle.ToPointer(), ArraySize);Marshal.FreeHGlobal(unmanagedHandle);}[Benchmark]public unsafe void AllocHGlobalWithLeaks(){IntPtr unmanagedHandle = Marshal.AllocHGlobal(ArraySize);Span<byte> unmanaged = new Span<byte>(unmanagedHandle.ToPointer(), ArraySize);}} }ThreadingDiagnoser
using System.Threading; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Diagnosers;namespace dotnet_perf {[ThreadingDiagnoser] //在BenchmarkDotNet.Diagnosers命名空間下public class IntroThreadingDiagnoser{[Benchmark]public void CompleteOneWorkItem(){ManualResetEvent done = new ManualResetEvent(initialState: false);ThreadPool.QueueUserWorkItem(m => (m as ManualResetEvent).Set(), done);done.WaitOne();}} }執行結果(可以看到在.Net 5和.Net?Core 3.1性能相差不大,是因為在.Net 5中并沒有對ThreadPool進行改進,但對異步是有改進):
MemoryDiagnoser使用
這個在筆記本沒法跑出結果.是NativeMemoryProfiler一樣,筆記本散熱達不到.在測試的時候回突然黑屏.這里直接跳過.
v0.12.1 生成跨平臺匯編代碼
using System; using BenchmarkDotNet.Attributes;namespace dotnet_perf {[DisassemblyDiagnoser(printSource:true)][RyuJitX64Job]public class TestJit{private B[] _array = new B[42];[Benchmark]public int Ctor() => new Span<B>(_array).Length;}class A{}sealed class B : A{} }dotnet run -c Release -f net48 --runtimes net48 netcoreapp31 netcoreapp50 --filter ** --joinBenchmarkDotNet?生成匯編代碼,和原先不一樣,原先是要到ObjDump.exe(是需要安裝MingW/Cygwin),現在需要iced(庫,純C#代碼實現,另外有Rust實現).說起這個比較坑.BenchmarkDotNet v0.12.1?是依賴的iced?1.4.0版本,使用新版本,是有異常的.iced庫目前只支持X86架構(32位和64位),看代碼中沒有Arm相關的目錄,應該是不支持的.
總結
以上是生活随笔為你收集整理的BenchmarkDotNet v0.12x新增功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说说 C# 9 新特性的实际运用
- 下一篇: 我画着图,FluentAPI 她自己就生