benchmarkdotnet dotnet 基准测试类库试用(一)
使用基准测试对于我们应用的性能优化是比较好的方式,可以快速看出优化的结果同时可以给出报告结果
benchmarkdotnet 是dotnet 版本的一个工具,以下是一个简单的试用
环境准备
我使用的是mac系统
- 安装dotnetcoresdk
这个很简单,选择操作系统安装就可以了 - 创建简单console 项目
mkdir benchmark
cd benchmark
dotnet new console
- 添加benckmarldotnet 类库
dotnet add package BenchmarkDotNet
- 添加简单demo
代码来自官方文档
using System;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
public class Md5VsSha256
{
private const int N = 10000;
private readonly byte[] data;
private readonly SHA256 sha256 = SHA256.Create();
private readonly MD5 md5 = MD5.Create();
public Md5VsSha256()
{
data = new byte[N];
new Random(42).NextBytes(data);
}
[Benchmark]
public byte[] Sha256() => sha256.ComputeHash(data);
[Benchmark]
public byte[] Md5() => md5.ComputeHash(data);
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Md5VsSha256>();
}
}
}
运行&&查看结果
- 运行
注意对于benchmark 需要的是release 版本的测试
编译类库:
dotnet build -c RELEASE
dotnet bin/RELEASE/netcoreapp2.2/benchmark.dll
或者直接:
dotnet run -c release
- 运行效果
// Validating benchmarks:
// ***** BenchmarkRunner: Start *****
// ***** Found 2 benchmark(s) in total *****
// ***** Building 1 exe(s) in Parallel: Start *****
Unable to find .sln file. Will use current directory /Users/dalong/mylearning/aspnetcore-learning/benchmark to search for project file. If you don't use .sln file on purpose it should not be a problem.
// start dotnet restore /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 in /Users/dalong/mylearning/aspnetcore-learning/benchmark/bin/RELEASE/netcoreapp2.2/4fdd42ee-69c6-492c-805f-2ee1777729fb
// command took 1.73s and exited with 0
// start dotnet build -c Release --no-restore /p:UseSharedCompilation=false /p:BuildInParallel=false /m:1 in /Users/dalong/mylearning/aspnetcore-learning/benchmark/bin/RELEASE/netcoreapp2.2/4fdd42ee-69c6-492c-805f-2ee1777729fb
// command took 3.21s and exited with 0
// ***** Done, took 00:00:05 (5.11 sec) *****
// Found 2 benchmarks:
// Md5VsSha256.Sha256: DefaultJob
// Md5VsSha256.Md5: DefaultJob
// **************************
// Benchmark: Md5VsSha256.Sha256: DefaultJob
// *** Execute ***
// Launch: 1 / 1
// Execute: dotnet "4fdd42ee-69c6-492c-805f-2ee1777729fb.dll" --benchmarkName "MyBenchmarks.Md5VsSha256.Sha256" --job "Default" --benchmarkId 0 in /Users/dalong/mylearning/aspnetcore-learning/benchmark/bin/RELEASE/netcoreapp2.2/4fdd42ee-69c6-492c-805f-2ee1777729fb/bin/Release/netcoreapp2.2
Failed to set up high priority. Make sure you have the right permissions. Message: Permission denied
// BeforeAnythingElse
// Benchmark Process Environment Information:
// Runtime=.NET Core 2.2.6 (CoreCLR 4.6.27817.03, CoreFX 4.6.27818.02), 64bit RyuJIT
// GC=Concurrent Workstation
// Job: DefaultJob
OverheadJitting 1: 1 op, 598964.00 ns, 598.9640 us/op
WorkloadJitting 1: 1 op, 1470156.00 ns, 1.4702 ms/op
OverheadJitting 2: 16 op, 1153574.00 ns, 72.0984 us/op
WorkloadJitting 2: 16 op, 1908217.00 ns, 119.2636 us/op
WorkloadPilot 1: 16 op, 498621.00 ns, 31.1638 us/op
WorkloadPilot 2: 32 op, 976046.00 ns, 30.5014 us/op
WorkloadPilot 3: 64 op, 1959744.00 ns, 30.6210 us/op
WorkloadPilot 4: 128 op, 4449342.00 ns, 34.7605 us/op
WorkloadPilot 5: 256 op, 7827198.00 ns, 30.5750 us/op
WorkloadPilot 6: 512 op, 15652304.00 ns, 30.5709 us/op
WorkloadPilot 7: 1024 op, 31633970.00 ns, 30.8925 us/op
WorkloadPilot 8: 2048 op, 60219960.00 ns, 29.4043 us/op
WorkloadPilot 9: 4096 op, 122998260.00 ns, 30.0289 us/op
WorkloadPilot 10: 8192 op, 243277420.00 ns, 29.6970 us/op
WorkloadPilot 11: 16384 op, 490180822.00 ns, 29.9183 us/op
WorkloadPilot 12: 32768 op, 1006114105.00 ns, 30.7042 us/op
OverheadWarmup 1: 32768 op, 148275.00 ns, 4.5250 ns/op
- 生成的报告目录

html 报告结果 
- 其他运行方式
实际上官方文档已经提供了比较全的运行方法
通过全局工具方式
dotnet tool install -g BenchmarkDotNet.Tool
dotnet benchmark MyAssemblyWithBenchmarks.dll --filter *
网络url
string url = "<E.g. direct link to raw content of a gist>";
var summary = BenchmarkRunner.RunUrl(url);
源码
string benchmarkSource = "public class MyBenchmarkClass { ...";
var summary = BenchmarkRunner.RunSource(benchmarkSource);
BenchmarkSwitcher
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
dotnet run -c Release -- --job short --runtimes clr core --filter *BenchmarkClass1*
说明
dotnet 的工具连是越来越丰富了,而且也越来人性化了,实际上基本上各种语言都是类似的实现类库。
参考资料
https://benchmarkdotnet.org/articles/guides/how-to-run.html
https://github.com/dotnet/BenchmarkDotNet
https://github.com/rongfengliang/aspnetcore-webapi-docker-compose-demo
benchmarkdotnet dotnet 基准测试类库试用(一)的更多相关文章
- Asp.net Core dotnet 发布类库文件 带上注释,发布预发行版,带上所有引用
带上注释 效果图 带上所有引用 效果图 预发行版 效果图 由于微软取消了 project.json 这个json 转而用了csproj 用于保存配置 所以懵逼很大一会 资料来源 project.j ...
- dotnet打包类库
打包类库成Nuget包:dotnet pack --configuration Release --include-source --include-symbols --no-build,注意,需要在 ...
- Micro Benchmark Framework java 基准测试类库
Micro Benchmark Framework 框架主要是method 层面上的 benchmark,精度可以精确到微秒级 比较典型的使用场景还有: 想定量地知道某个函数需要执行多长时间,以及执行 ...
- 【基准测试】BenchmarkDotNet介绍
BenchmarkDotNet 概述 BenchmarkDotNet helps you to transform methods into benchmarks, track their perfo ...
- CI框架的引导流程以及基准测试类
一[CI]框架的引导流程了解一下,并掌握如何新增自己的扩展类库 http://www.cnblogs.com/ohmygirl/p/CIRead-4.html // CI框架源码阅读笔记4 引导文 ...
- 程序开发常用第三方类库一览表(VendorLib)
以下是自己开发过程中用到的第三方类库,记录下来方便查阅 ------------------------------------------------------------------------ ...
- 一起了解 .Net Foundation 项目 No.14
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. .NET Core .NE ...
- 一起了解 .Net Foundation 项目 No.19
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Salesforce To ...
- 一系列令人敬畏的.NET核心库,工具,框架和软件
内容 一般 框架,库和工具 API 应用框架 应用模板 身份验证和授权 Blockchain 博特 构建自动化 捆绑和缩小 高速缓存 CMS 代码分析和指标 压缩 编译器,管道工和语言 加密 数据库 ...
随机推荐
- (原创)使用C#开发PLC上位机监控系统客户端应用程序
PLC客户端监控系统的特点: 0.客户端系统软件可部署在 多个管理层的PC机上,或者需要部署在距离服务器较远区域的PC机上,通过网线连接到服务器端的交换机. 1应用范围: (1)所有客户端都只有监视功 ...
- 在.Net Core中使用Swagger制作接口文档
在实际开发过程中后台开发人员与前端(移动端)接口的交流会很频繁.所以需要一个简单的接口文档让双方可以快速定位到问题所在. Swagger可以当接口调试工具也可以作为简单的接口文档使用. 在传统的asp ...
- 关闭Postman 证书的验证
1.问题背景 使用自己生成的SSL证书,用Postman访问失败.需要忽略SSL证书的验证 2.关闭Postman 证书的验证 在Settings-General中 关闭SSL certificate ...
- Java之路---Day18(List集合)
2019-11-05-23:03:28 List集合: java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支,习惯性地会将实现了List 接口的对象称为Lis ...
- spark任务运行完成后在driver端的处理逻辑
回顾 上一篇,我们分析了了任务在executor端的运行流程,任务运行结束后,在Executor.launchTask方法最后,通过调用execBackend.statusUpdate方法将任务结果以 ...
- 【转载】Asp.Net生成图片验证码工具类
在Asp.Net应用程序中,很多时候登陆页面以及其他安全重要操作的页面需要输入验证码,本文提供一个生成验证码图片的工具类,该工具类通过随机数生成验证码文本后,再通过C#中的图片处理类位图类,字体类,一 ...
- 回调、Promise、async-await
第一章 异步:现在与将来 程序中现在运行的部分和将来运行的部分之间的关系就是异步编程的核心. 场景:等待用户输入.从数据库或文件系统中请求数据.通过网络 发送数据并等待响应,或者是在以固定时间间隔执行 ...
- 以服务方式启动tomcat无法访问NFS共享盘
用startup.bat方式启动tomcat,程序的可以访问NFS共享盘的文件.但用 1).以服务的方式启动tomcat 2).或者用windows的任务计划去执行startup.bat的方式启动to ...
- 让window10目录支持大小写
今天用vmware影响文件夹以供linux系统使用,结果宿主机是win10系统,所以用在linux里,大小写变得不敏感. 解决办法也很简单. 管理员模式运行cmd(默认目录是C:\Windows\Sy ...
- aws centos系统磁盘扩容
growpart /dev/xvda 1 展开修改后的分区(注意是:空格 1,而非打错了) # ext3/4 1.resize2fs /dev/xvda1 将分区调整为新的卷容量 # xfs分区 2 ...