BenchmarkDotNet v0.12x新增功能
起因
在看.Net?官方博客 .Net 5性能優(yōu)化?中,發(fā)現(xiàn)測試性能的BenchmarkDotNet版本已經(jīng)是v0.12.1,然后去看BenchmarkDotNet文檔,發(fā)現(xiàn)還是有不少新的特性.
v0.12.0
支持多個(gè)運(yùn)行時(shí)(API改進(jìn)),增加對.Net 5支持
支持DotNet創(chuàng)建BenchmarkDotNet項(xiàng)目(項(xiàng)目模版)
增加NativeMemoryProfiler(目前僅支持Windows,需要在Nuget管理器中安裝BenchmarkDotNet.Diagnostics.Windows包,才可以,內(nèi)部使用EtwProfiler)
增加ThreadingDiagnoser
增加MemoryDiagnoser
對LINQPad 6進(jìn)行支持,可以在LINQPad 6進(jìn)行代碼性能測試(LINQPad?要收費(fèi)版才可以,這里也跳過)
文檔快速搜索
v0.12.1
跨平臺生成匯編代碼
基于事件管道跨平臺Profiler
新的API,使用更方便
支持多個(gè)運(yùn)行時(shí),新增.Net 5
<!--新增.Net 5運(yùn)行時(shí)--> <TargetFrameworks>net5.0;netcoreapp3.1;net48</TargetFrameworks>#.netframework 4.8為基準(zhǔn),測試三個(gè)版本 .NetFramework 4.8/.Net Core 3.1和.Net 5 dotnet run -c Release -f net48 --runtimes net48 netcoreapp31 netcoreapp50 --filter ** --joinBenchmarkDotNet項(xiàng)目
先查看.Net 5下,有什么項(xiàng)目模版:
dotnet new -l通過命令安裝模版:
#-i 代表install dotnet new -i BenchmarkDotNet.Templates通過命令卸載安裝過的模版:
#-u 代表卸載 u為uninsall dotnet new -u BenchmarkDotNet.Templates新建Benchmark項(xiàng)目:
#新建BenchmarkDotNet項(xiàng)目 dotnet new benchmark --console-appNativeMemoryProfiler使用
在Nuget管理器中安裝BenchmarkDotNet.Diagnostics.Windows包
執(zhí)行后生成的結(jié)果(沒有執(zhí)行完成,是因?yàn)殡娔X在運(yùn)行的時(shí)候突然藍(lán)屏,懷疑是CPU溫度過高造成的,因?yàn)楣P記本好幾年沒有換過散熱硅脂了):
看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();}} }執(zhí)行結(jié)果(可以看到在.Net 5和.Net?Core 3.1性能相差不大,是因?yàn)樵?Net 5中并沒有對ThreadPool進(jìn)行改進(jìn),但對異步是有改進(jìn)):
MemoryDiagnoser使用
這個(gè)在筆記本沒法跑出結(jié)果.是NativeMemoryProfiler一樣,筆記本散熱達(dá)不到.在測試的時(shí)候回突然黑屏.這里直接跳過.
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),現(xiàn)在需要iced(庫,純C#代碼實(shí)現(xiàn),另外有Rust實(shí)現(xiàn)).說起這個(gè)比較坑.BenchmarkDotNet v0.12.1?是依賴的iced?1.4.0版本,使用新版本,是有異常的.iced庫目前只支持X86架構(gòu)(32位和64位),看代碼中沒有Arm相關(guān)的目錄,應(yīng)該是不支持的.
總結(jié)
以上是生活随笔為你收集整理的BenchmarkDotNet v0.12x新增功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说说 C# 9 新特性的实际运用
- 下一篇: 我画着图,FluentAPI 她自己就生