以前一篇博客中,咱们讲解.NET Core中的CSV解析库,在文章的最后,做者使用了性能基准测试工具BenchmarkDotNet测试了2个不一样CSV解析库的性能,本篇咱们来详细介绍一下BenchmarkDotNet。html
原文连接:https://dotnetcoretutorials.com/2017/12/04/benchmarking-net-core-code-benchmarkdotnet/程序员
性能基准测试能够帮助程序员对比2个代码段或者方法的性能,这对于代码重写或者重构来讲,能够提供一种很好的量化标准。若是没有性能基准测试,很难想象将方法A改成B方法时候,仅凭肉眼如何区分性能的变化。app
BenchmarkDotNet是一款强力的.NET性能基准测试库, 官网https://benchmarkdotnet.org/。函数
运行时支持工具
BenchmarkDotnet为每一个被测试的方法提供了孤立的环境, 使用BenchmarkDotnet, 程序员能够很容易的编写各类性能测试方法,并能够避免许多常见的坑。性能
如今咱们但愿来对比一下Linq to object中First和Single方法的性能测试
虽然咱们知道First的性能确定比Single高, First方法会在查询到第一个知足条件的对象以后就中止集合遍历,而Single找到第一个知足条件的对象以后,不会中止查找,它会去继续查找集合中的剩余对象,直到遍历整个集合或者在集合中找到第二个匹配条件的对象。 这里咱们只是为了演示一下如何进行代码基准测试。spa
为了使用BenchmarkDotNet来进行代码基准测试,咱们首先建立一个空的.Net Core控制台程序。调试
而后咱们使用Package Manage Console添加BenchmarkDotNet库code
PM> Install-Package BenchmarkDotNet
而后咱们修改Program.cs文件, 代码以下
public class Program { public class SingleVsFirst { private readonly List<string> _haystack = new List<string>(); private readonly int _haystackSize = 1000000; private readonly string _needle = "needle"; public SingleVsFirst() { //Add a large amount of items to our list. Enumerable.Range(1, _haystackSize).ToList().ForEach(x => _haystack.Add(x.ToString())); //Insert the needle right in the middle. _haystack.Insert(_haystackSize / 2, _needle); } [Benchmark] public string Single() => _haystack.SingleOrDefault(x => x == _needle); [Benchmark] public string First() => _haystack.FirstOrDefault(x => x == _needle); } public static void Main(string[] args) { var summary = BenchmarkRunner.Run<SingleVsFirst>(); Console.ReadLine(); } }
代码解释说明
SingleVsFirst
类是一个测试类。Single
和First
方法,分别调用了Linq to object的SingleOrDefault
和FirstOrDefault
方法来查询字符串集合中的"needle"字符串。Single
和First
方法上,咱们加入[Benchmark]
特性, 拥有该特性的方法会出如今最后的基准检测报告中。注意:
- 测试的方法必须是公开的(public), 若是把public去掉,程序不会产生任何结果
- 在运行程序以前,还有一步关键的操做,测试的程序须要使用Release模式编译,而且不能附加任何调试器(Debugger)
如今咱们运行程序,程序产生的最终报告以下
Method | Mean | Error | StdDev | Median | ------- |---------:|----------:|---------:|---------:| Single | 28.12 ms | 0.9347 ms | 2.697 ms | 28.93 ms | First | 13.30 ms | 0.8394 ms | 2.475 ms | 14.48 ms |
结果中的第一列Mean代表了2个方法处理的平均响应时间,First
比Single
快了一倍(这和咱们测试字符串放置的位置有关系)。
BenchmarkDotNet中咱们还可使用[ParamsSource]
参数来指定测试的用例范围。
在上面的代码中,咱们测试了匹配字符串在集合中间位置时,First
和Single
的效率对比,下面咱们修改上面的代码,咱们但愿分别测试匹配字符串在集合头部,尾部以及中间位置时First
和Single
的效率对比。
using System; using System.Collections.Generic; using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; namespace BenchmarkExample { public class SingleVsFirst { private readonly List<string> _haystack = new List<string>(); private readonly int _haystackSize = 1000000; public List<string> _needles => new List<string> { "StartNeedle", "MiddleNeedle", "EndNeedle" }; public SingleVsFirst() { //Add a large amount of items to our list. Enumerable.Range(1, _haystackSize).ToList().ForEach(x => _haystack.Add(x.ToString())); //One at the start. _haystack.Insert(0, _needles[0]); //One right in the middle. _haystack.Insert(_haystackSize / 2, _needles[1]); //One at the end. _haystack.Insert(_haystack.Count - 1, _needles[2]); } [ParamsSource(nameof(_needles))] public string Needle { get; set; } [Benchmark] public string Single() => _haystack.SingleOrDefault(x => x == Needle); [Benchmark] public string First() => _haystack.FirstOrDefault(x => x == Needle); } class Program { static void Main(string[] args) { var summary = BenchmarkRunner.Run<SingleVsFirst>(); Console.ReadLine(); } } }
代码解释说明
_needles
Needle
, 表示当前测试的用例,在被测试Single
和First
方法中,咱们使用属性Needle
来匹配[ParamsSource]
, 并设置参数来源是_needles
如今咱们运行程序,程序产生的最终报告以下
Method | Needle | Mean | Error | StdDev | Median | ------- |------------- |-----------------:|---------------:|-----------------:|-----------------:| Single | EndNeedle | 23,266,757.53 ns | 432,206.593 ns | 591,609.263 ns | 23,236,343.07 ns | First | EndNeedle | 24,984,621.12 ns | 494,223.345 ns | 783,890.599 ns | 24,936,945.21 ns | Single | MiddleNeedle | 21,379,814.14 ns | 806,253.579 ns | 2,377,256.870 ns | 22,436,101.14 ns | First | MiddleNeedle | 11,984,519.09 ns | 315,184.021 ns | 924,380.173 ns | 12,233,700.94 ns | Single | StartNeedle | 23,650,243.23 ns | 599,968.173 ns | 714,219.431 ns | 23,555,402.19 ns | First | StartNeedle | 89.17 ns | 1.864 ns | 2.732 ns | 89.07 ns
从结果上看
First
性能比Single
高的多First
性能是比Single
的一倍First
和比Single
的性能差很少在.NET Core中的CSV解析库中,咱们使用了如下代码
[MemoryDiagnoser] public class CsvBenchmarking { [Benchmark(Baseline =true)] public IEnumerable<Automobile> CSVHelper() { TextReader reader = new StreamReader("import.txt"); var csvReader = new CsvReader(reader); var records = csvReader.GetRecords<Automobile>(); return records.ToList(); } [Benchmark] public IEnumerable<Automobile> TinyCsvParser() { CsvParserOptions csvParserOptions = new CsvParserOptions(true, ','); var csvParser = new CsvParser<Automobile>(csvParserOptions, new CsvAutomobileMapping()); var records = csvParser.ReadFromFile("import.txt", Encoding.UTF8); return records.Select(x => x.Result).ToList(); } }
其中除了[Benchmark]特性,咱们还在测试类CsvBenchmarking
上添加了[MemoryDiagnoser]
特性,该特性会在测试报告中追加,2个方法执行时的内存使用状况。
Method | Mean | Scaled | Allocated | -------------- |-----------:|-------:|----------:| CSVHelper | 1,404.5 ms | 1.00 | 244.39 MB | TinyCsvParser | 381.6 ms | 0.27 | 32.53 MB |
其中Allocated代表了内存占用状况。
BenchmarkDotNet绝对是.NET开发人员了解代码性能,以及对比代码性能的必备神器。你的项目里用了BenchmarkDotnet了么?