BenchmarkDotNet是一款开源的性能测试工具,使用方式非常简单,特别是对实现同一功能的两种方式,犹豫不决时,可以使用它进行个对比。

比如我们比较ADO.NET方式查询数据库表,和用Dapper方式获取数据为表,只需要在两个方法上增加 [Benchmark]就可以了,然后调用var summary = BenchmarkRunner.Run<Cycle>();就实现了性能测试。

using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Dapper;namespace MyBenchmarks
{public class Product{public string Name { get; set; }public string ProductNumber { get; set; }public bool MakeFlag { get; set; }public bool FinishedGoodsFlag { get; set; }public string Color { get; set; }public short SafetyStockLevel { get; set; }public short ReorderPoint { get; set; }public decimal StandardCost { get; set; }public decimal ListPrice { get; set; }public string Size { get; set; }public string SizeUnitMeasureCode { get; set; }public string WeightUnitMeasureCode { get; set; }public double Weight { get; set; }public int DaysToManufacture { get; set; }public string ProductLine { get; set; }public string Class { get; set; }public string Style { get; set; }public int ProductSubcategoryID { get; set; }public int ProductModelID { get; set; }public DateTime SellStartDate { get; set; }public DateTime SellEndDate { get; set; }public DateTime DiscontinuedDate { get; set; }public Guid rowguid { get; set; }public DateTime ModifiedDate { get; set; }}public class Cycle{[Benchmark]public void DapperTest(){var conn = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks2016;Persist Security Info=True;User ID=sa;password=sa;");var table = conn.Query<Product>("select top 100 * from production.product");}[Benchmark]public void CommandTest(){var conn = "Data Source=.;Initial Catalog=AdventureWorks2016;Persist Security Info=True;User ID=sa;password=sa;";var sql = "select top 100 * from production.product";var dapp = new SqlDataAdapter(sql, conn);var table = new DataTable();dapp.Fill(table);}}public class Program{public static void Main(string[] args){var summary = BenchmarkRunner.Run<Cycle>();}}
}

运行一下看一下结果,为了减少篇幅,删除了一些结果,从结果看出,为了得到较为客观的性能测试结果,BenchmarkDotNet还是做了很多工作的:

OverheadJitting,WorkloadJitting,WorkloadPilot,OverheadWarmup,OverheadActual,WorkloadWarmup,WorkloadActual,WorkloadResult。

并且通过最后的一个表格,清楚的给出了结果。同时还给出了Warnings ,以供参考。

// Validating benchmarks:
// ***** BenchmarkRunner: Start   *****
// ***** Found 2 benchmark(s) in total *****
// ***** Building 1 exe(s) in Parallel: Start   *****
// start dotnet restore  /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 /p:Deterministic=true /p:Optimize=true in C:\MyFile\Source\Repos\Asp.NetCoreExperiment\Asp.NetCoreExperiment\BenchmarkDotNet\Demo01\bin\Release\net6.0\03bf0209-ac26-4de1-b763-4ba03e46cf4d
// command took 2.83s and exited with 0
// start dotnet build -c Release  --no-restore /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 /p:Deterministic=true /p:Optimize=true in C:\MyFile\Source\Repos\Asp.NetCoreExperiment\Asp.NetCoreExperiment\BenchmarkDotNet\Demo01\bin\Release\net6.0\03bf0209-ac26-4de1-b763-4ba03e46cf4d
// command took 4.29s and exited with 0
// ***** Done, took 00:00:07 (7.52 sec)   *****
// Found 2 benchmarks:
//   Cycle.DapperTest: DefaultJob
//   Cycle.CommandTest: DefaultJob// **************************
// Benchmark: Cycle.DapperTest: DefaultJob
// *** Execute ***
// Launch: 1 / 1
// Execute: dotnet "03bf0209-ac26-4de1-b763-4ba03e46cf4d.dll" --benchmarkName "MyBenchmarks.Cycle.DapperTest" --job "Default" --benchmarkId 0 in C:\MyFile\Source\Repos\Asp.NetCoreExperiment\Asp.NetCoreExperiment\BenchmarkDotNet\Demo01\bin\Release\net6.0\03bf0209-ac26-4de1-b763-4ba03e46cf4d\bin\Release\net6.0
// BeforeAnythingElse// Benchmark Process Environment Information:
// Runtime=.NET 6.0.0 (6.0.21.25307), X64 RyuJIT
// GC=Concurrent Workstation
// Job: DefaultJobOverheadJitting  1: 1 op, 229700.00 ns, 229.7000 us/op
WorkloadJitting  1: 1 op, 410021000.00 ns, 410.0210 ms/opWorkloadWarmup   1: 1 op, 4581600.00 ns, 4.5816 ms/op
WorkloadWarmup   2: 1 op, 746500.00 ns, 746.5000 us/op
WorkloadWarmup   3: 1 op, 830000.00 ns, 830.0000 us/op
WorkloadWarmup   4: 1 op, 822900.00 ns, 822.9000 us/op
WorkloadWarmup   5: 1 op, 741800.00 ns, 741.8000 us/op
WorkloadWarmup   6: 1 op, 746900.00 ns, 746.9000 us/op
WorkloadWarmup   7: 1 op, 826300.00 ns, 826.3000 us/op
WorkloadWarmup   8: 1 op, 698300.00 ns, 698.3000 us/op// BeforeActualRun
WorkloadActual   1: 1 op, 755100.00 ns, 755.1000 us/op
……
WorkloadActual  100: 1 op, 712700.00 ns, 712.7000 us/op// AfterActualRun
WorkloadResult   1: 1 op, 755100.00 ns, 755.1000 us/op
……
WorkloadResult  95: 1 op, 712700.00 ns, 712.7000 us/op// AfterAll
// Benchmark Process 26724 has exited with code 0Mean = 753.054 us, StdErr = 11.687 us (1.55%), N = 95, StdDev = 113.909 us
Min = 578.400 us, Q1 = 668.600 us, Median = 734.900 us, Q3 = 798.800 us, Max = 1,052.600 us
IQR = 130.200 us, LowerFence = 473.300 us, UpperFence = 994.100 us
ConfidenceInterval = [713.353 us; 792.754 us] (CI 99.9%), Margin = 39.701 us (5.27% of Mean)
Skewness = 0.87, Kurtosis = 2.93, MValue = 3.63// **************************
// Benchmark: Cycle.CommandTest: DefaultJob
// *** Execute ***
// Launch: 1 / 1
// Execute: dotnet "03bf0209-ac26-4de1-b763-4ba03e46cf4d.dll" --benchmarkName "MyBenchmarks.Cycle.CommandTest" --job "Default" --benchmarkId 1 in C:\MyFile\Source\Repos\Asp.NetCoreExperiment\Asp.NetCoreExperiment\BenchmarkDotNet\Demo01\bin\Release\net6.0\03bf0209-ac26-4de1-b763-4ba03e46cf4d\bin\Release\net6.0
// BeforeAnythingElse// Benchmark Process Environment Information:
// Runtime=.NET 6.0.0 (6.0.21.25307), X64 RyuJIT
// GC=Concurrent Workstation
// Job: DefaultJobOverheadJitting  1: 1 op, 215400.00 ns, 215.4000 us/op
WorkloadJitting  1: 1 op, 339247500.00 ns, 339.2475 ms/opWorkloadWarmup   1: 1 op, 3680100.00 ns, 3.6801 ms/op
WorkloadWarmup   2: 1 op, 943000.00 ns, 943.0000 us/op
WorkloadWarmup   3: 1 op, 875200.00 ns, 875.2000 us/op
WorkloadWarmup   4: 1 op, 982500.00 ns, 982.5000 us/op
WorkloadWarmup   5: 1 op, 956900.00 ns, 956.9000 us/op
WorkloadWarmup   6: 1 op, 1030800.00 ns, 1.0308 ms/op
WorkloadWarmup   7: 1 op, 1170900.00 ns, 1.1709 ms/op
WorkloadWarmup   8: 1 op, 1169400.00 ns, 1.1694 ms/op// BeforeActualRun
WorkloadActual   1: 1 op, 975400.00 ns, 975.4000 us/op
……
WorkloadActual  100: 1 op, 897400.00 ns, 897.4000 us/op// AfterActualRun
WorkloadResult   1: 1 op, 975400.00 ns, 975.4000 us/op
……
WorkloadResult  91: 1 op, 897400.00 ns, 897.4000 us/op// AfterAll
// Benchmark Process 17300 has exited with code 0Mean = 951.263 us, StdErr = 13.676 us (1.44%), N = 91, StdDev = 130.465 us
Min = 759.300 us, Q1 = 864.000 us, Median = 912.900 us, Q3 = 1,004.600 us, Max = 1,316.000 us
IQR = 140.600 us, LowerFence = 653.100 us, UpperFence = 1,215.500 us
ConfidenceInterval = [904.736 us; 997.789 us] (CI 99.9%), Margin = 46.526 us (4.89% of Mean)
Skewness = 0.95, Kurtosis = 3.05, MValue = 2.44// ***** BenchmarkRunner: Finish  *****// * Export *BenchmarkDotNet.Artifacts\results\MyBenchmarks.Cycle-report.csvBenchmarkDotNet.Artifacts\results\MyBenchmarks.Cycle-report-github.mdBenchmarkDotNet.Artifacts\results\MyBenchmarks.Cycle-report.html// * Detailed results *
Cycle.DapperTest: DefaultJob
Runtime = .NET 6.0.0 (6.0.21.25307), X64 RyuJIT; GC = Concurrent Workstation
Mean = 753.054 us, StdErr = 11.687 us (1.55%), N = 95, StdDev = 113.909 us
Min = 578.400 us, Q1 = 668.600 us, Median = 734.900 us, Q3 = 798.800 us, Max = 1,052.600 us
IQR = 130.200 us, LowerFence = 473.300 us, UpperFence = 994.100 us
ConfidenceInterval = [713.353 us; 792.754 us] (CI 99.9%), Margin = 39.701 us (5.27% of Mean)
Skewness = 0.87, Kurtosis = 2.93, MValue = 3.63
-------------------- Histogram --------------------
[  545.635 us ;   582.035 us) | @
[  582.035 us ;   650.935 us) | @@@@@@@@@@@@@@@
[  650.935 us ;   726.135 us) | @@@@@@@@@@@@@@@@@@@@@@@@@@@
[  726.135 us ;   791.665 us) | @@@@@@@@@@@@@@@@@@@@@@@@@@@@
[  791.665 us ;   852.365 us) | @@@@@@@@
[  852.365 us ;   924.365 us) | @@@
[  924.365 us ; 1,009.685 us) | @@@@@@@@@@@
[1,009.685 us ; 1,085.365 us) | @@
---------------------------------------------------Cycle.CommandTest: DefaultJob
Runtime = .NET 6.0.0 (6.0.21.25307), X64 RyuJIT; GC = Concurrent Workstation
Mean = 951.263 us, StdErr = 13.676 us (1.44%), N = 91, StdDev = 130.465 us
Min = 759.300 us, Q1 = 864.000 us, Median = 912.900 us, Q3 = 1,004.600 us, Max = 1,316.000 us
IQR = 140.600 us, LowerFence = 653.100 us, UpperFence = 1,215.500 us
ConfidenceInterval = [904.736 us; 997.789 us] (CI 99.9%), Margin = 46.526 us (4.89% of Mean)
Skewness = 0.95, Kurtosis = 3.05, MValue = 2.44
-------------------- Histogram --------------------
[  721.230 us ;   780.280 us) | @@
[  780.280 us ;   860.730 us) | @@@@@@@@@@@@@@@@@@@@
[  860.730 us ;   936.870 us) | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[  936.870 us ; 1,020.120 us) | @@@@@@@@@@@@@@@@
[1,020.120 us ; 1,080.630 us) | @@@@@
[1,080.630 us ; 1,170.980 us) | @@@@@@@
[1,170.980 us ; 1,247.120 us) | @@@@@@@@
[1,247.120 us ; 1,277.930 us) |
[1,277.930 us ; 1,354.070 us) | @
---------------------------------------------------// * Summary *BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19043.1052 (21H1/May2021Update)
Intel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.4.21255.9[Host]     : .NET 6.0.0 (6.0.21.25307), X64 RyuJIT  [AttachedDebugger]DefaultJob : .NET 6.0.0 (6.0.21.25307), X64 RyuJIT|      Method |     Mean |    Error |   StdDev |   Median |
|------------ |---------:|---------:|---------:|---------:|
|  DapperTest | 753.1 us | 39.70 us | 113.9 us | 734.9 us |
| CommandTest | 951.3 us | 46.53 us | 130.5 us | 912.9 us |// * Warnings *
MultimodalDistributionCycle.DapperTest: Default -> It seems that the distribution is bimodal (mValue = 3.63)
EnvironmentSummary -> Benchmark was executed with attached debugger
MinIterationTimeCycle.DapperTest: Default  -> The minimum observed iteration time is 578.4000 us which is very small. It's recommended to increase it to at least 100.0000 ms using more operations.Cycle.CommandTest: Default -> The minimum observed iteration time is 759.3000 us which is very small. It's recommended to increase it to at least 100.0000 ms using more operations.// * Hints *
OutliersCycle.DapperTest: Default  -> 5 outliers were removed (1.06 ms..2.17 ms)Cycle.CommandTest: Default -> 9 outliers were removed (1.37 ms..2.51 ms)// * Legends *Mean   : Arithmetic mean of all measurementsError  : Half of 99.9% confidence intervalStdDev : Standard deviation of all measurementsMedian : Value separating the higher half of all measurements (50th percentile)1 us   : 1 Microsecond (0.000001 sec)// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:03 (3.12 sec), executed benchmarks: 2Global total time: 00:00:10 (10.65 sec), executed benchmarks: 2
// * Artifacts cleanup *

BenchmarkDotNet性能测试相关推荐

  1. 用BenchmarkDotNet给C#程序做性能测试

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用BenchmarkDotNet给C#程序做性能测试. 转载于:https://www.cnblogs. ...

  2. .NET Core中的性能测试工具BenchmarkDotnet

    背景介绍 之前一篇博客中,我们讲解.NET Core中的CSV解析库,在文章的最后,作者使用了性能基准测试工具BenchmarkDotNet测试了2个不同CSV解析库的性能,本篇我们来详细介绍一下Be ...

  3. .NET Core性能测试组件BenchmarkDotNet 支持.NET Framework Mono

    .NET Core 超强性能测试组件BenchmarkDotNet 支持Full .NET Framework, .NET Core (RTM), Mono. BenchmarkDotNet支持 C# ...

  4. .Net性能测试工具BenchmarkDotnet

    性能基准测试可以帮助程序员对比2个代码段或者方法的性能,这对于代码重写或者重构来说,可以提供一种很好的量化标准.如果没有性能基准测试,很难想象将方法A改为B方法时候,仅凭肉眼如何区分性能的变化. Be ...

  5. BenchmarkDotNet v0.12x新增功能

    起因 在看.Net 官方博客 .Net 5性能优化 中,发现测试性能的BenchmarkDotNet版本已经是v0.12.1,然后去看BenchmarkDotNet文档,发现还是有不少新的特性. v0 ...

  6. Magicodes.IE 在100万数据量下导入导出性能测试

    原文作者:HueiFeng 前言 目前Magicodes.IE更新到了2.2.3,感谢大家的支持,同时建议大家在使用过程中如果遇到一些问题或者说需要一些额外的功能可以直接提issues,当然更建议大家 ...

  7. NPOI 导出 excel 性能测试

    NPOI 导出 excel 性能测试 Intro 网上看到很多人说 NPOI 的性能不行,自己写了一个 NPOI 的扩展库,于是想尝试看看 NPOI 的性能究竟怎么样,道听途说始终不如自己动手一试. ...

  8. 利用BenchmarkDotNet 测试 .Net Core API 同步和异步方法性能

    事由: 这两天mentor给我布置了个任务让我用BenchmarkDotNet工具去测试一下同一个API 用同步和异步方法写性能上有什么差别. 顺带提一下: 啊啊啊啊 等我仔细看文档的时候文档 发现它 ...

  9. 使用 BenchmarkDotnet 测试代码性能

    先来点题外话,清明节前把工作辞了(去 tm 的垃圾团队,各种拉帮结派.勾心斗角).这次找工作就得慢慢找了,不能急了,希望能找到个好团队,好岗位吧.顺便这段时间也算是比较闲,也能学习一下和填掉手上的坑. ...

最新文章

  1. Asp.net的加密解密技巧--[转载]
  2. mysql模糊查询 汉字为何不起作用_mysql中文模糊查询遇到的有关问题,各位救急...
  3. android studio 加载ffmpeg.so,Android studio使用已经编译好的ffmpeg .so库
  4. 插入排序(java版)
  5. mfc切换office样式_干货搬运工|计算机二级office必读,裸考也能通关的宝典
  6. 利用python进行tf-idf算法绘制词云图_利用python实现通过TF-IDF和BM25提取文章关键词...
  7. C语言能够被替换吗?
  8. JavaMail实践--实现邮件发送
  9. SQLite升级数据库:
  10. gps信号用什么软件测试,gps信号检测软件
  11. 电子元器件选型——电阻
  12. 让你浏览器飞起来的电脑插件合集
  13. 哪个表示经度?哪个表示纬度?是X是经度?还是Y表示经度?
  14. WORD中设置“选择性粘贴“—“无格式文本”的快捷键
  15. 指纹识别-传感器原理
  16. 【Matlab】MATLAB编辑器主题颜色更改(MATLAB Schemer)
  17. 三月不开单,开单吃三月说的是你吗?
  18. CAN工具 - PCAN - 半自动化
  19. http://wenku.baidu.com/view/981f99d376eeaeaad1f330e7.html
  20. jsp简易的图书管理系统

热门文章

  1. Plsql运行mysql脚本_oracle中PLSQL语句
  2. Yii2 的快速配置 api 服务 yii2-fast-api
  3. c#中的奇异递归模式
  4. 数码管流动显示(自己的单片机)
  5. 导入shape文件到SDE数据库
  6. 计算机英语课程背景,专家讲座第十五讲:信息化背景下高质量大学英语课程建设与教学设计...
  7. 移动硬盘改台式机硬盘_如何在台式机或移动设备上离线使用Google云端硬盘
  8. 「读懂源码系列2」我从 lodash 源码中学到的几个知识点
  9. 实现CSS在线美化(格式化)、压缩、加密、解密、混淆工具-toolfk程序员工具网
  10. Java8新的异步编程方式 CompletableFuture(三)