BenchmarkDotNet的使用
生活随笔
收集整理的這篇文章主要介紹了
BenchmarkDotNet的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我的高級架構師告訴我,檢驗程序性能時不要用DateTime.Now相減或者StopWatch,最好用BenchmarkDotNet,于是我就試了一下。
上手體驗后感覺BenchmarkDotNet大致的特點如下:
使用前要在NuGet管理器中,或者在包管理器的PM終端中安裝BenchmarkDotNet。
最后上代碼(以測試SeriLog和原生StreamWriter的效率對比為例):
// See https://aka.ms/new-console-template for more information using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using Serilog; using Serilog.Core;SerilogTest.Program.main();namespace SerilogTest {public class Program{Logger _log2Console = new LoggerConfiguration().WriteTo.Console().CreateLogger();Logger _log2File = new LoggerConfiguration().WriteTo.File("seri.log").CreateLogger();private StreamWriter _sw2File = new StreamWriter("sq.log");private int _loopMaxTime = 100;[Benchmark]public double Seri2ConsoleI(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2Console.Information($"Serilog info {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2ConsoleE(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2Console.Error($"Serilog error {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2FileI(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2File.Information($"Serilog info {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Seri2FileE(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_log2File.Error($"Serilog error {i}");}Log.CloseAndFlush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Stream2File(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){_sw2File.WriteLine($"{tmpStartTime.ToString()}.077 +08:00 [ERR] Stream Writer {i}");}_sw2File.Flush();DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}[Benchmark]public double Stream2Console(){DateTime tmpStartTime = System.DateTime.Now;for (int i = 1; i < _loopMaxTime; i++){Console.WriteLine($"{tmpStartTime.ToString()}.077 +08:00 [ERR] Console Write {i}");}DateTime tmpEndTime = System.DateTime.Now;TimeSpan tmpTimeCost = tmpEndTime.Subtract(tmpStartTime);return Math.Round(tmpTimeCost.TotalMilliseconds, 2, MidpointRounding.AwayFromZero);}public static void main(){var summary = BenchmarkRunner.Run<Program>();/*double SeriConcolsErrorCost = Seri2ConsoleE();double SeriConcolsInfoCost = Seri2ConsoleI();double SeriFileErrorCost = Seri2FileE();double SeriFileInfoCost = Seri2FileI();double SystConsoleInfoCost = Stream2Console();double SystFileInfoCost = Stream2File();Console.WriteLine($"[To Console]:\n" +$" Seri info: { SeriConcolsInfoCost }ms\n" +$" Seri err: { SeriConcolsErrorCost }ms\n" +$" Sys output: { SystConsoleInfoCost }ms\n" +$"[To file]:\n" +$" Seri info: { SeriFileInfoCost }ms\n" +$" Seri error: { SeriFileErrorCost }ms\n" +$" Sys output: { SystFileInfoCost }ms\n");*/}} }運行結果如下:
結果日志也會保存在新生成的BenchmarkDotNet.Artifacts目錄中。
總結
以上是生活随笔為你收集整理的BenchmarkDotNet的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一晃居然已经停更半年了
- 下一篇: 什么副业最赚钱 尤其是上班族要看一看