. NET Core performance testing component BenchmarkDotNet supports. NET Framework Mono and benchmarkdotnet
BenchmarkDotNet, a super-powerful performance testing component of. NET Core, supports Full. NET Framework,. NET Core (RTM), and Mono.
BenchmarkDotNet supports C #, F #, and Visual Basic, and can be run across platforms.
It also supports export of various reports, which is quite convenient.
GitHub: https://github.com/PerfDotNet/BenchmarkDotNet
Next we will discuss the actual use and experience.
This article describes how to use BenchmarkDotNet in. NET Core applications.
Create an application
First, create a. NET Core console application NETCoreTest.
Install BenchmarkDotNet
Use the NuGet command line to install:
Install-Package BenchmarkDotNet
You can also search for installation in NuGet Manager
Write code
After the installation, We can compile the test code.
Create an Md5VsSha256 class and add the Benchmark feature to the method.
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() { return sha256.ComputeHash(data); } [Benchmark] public byte[] Md5() { return md5.ComputeHash(data); } }
Run the test
Next we will execute
Add the following code to Program. cs Main:
var summary = BenchmarkRunner.Run<Md5VsSha256>();
Then run the program. You can use dotnet run or vs DEBUG.
The following figure shows the output result of the console.
After execution, a BenchmarkDotNet. Artifacts folder will be created in the program directory.
There will also be a corresponding test result file.
For more powerful functions, see the official documentation: https://perfdotnet.github.io/BenchmarkDotNet/
If you think this article is helpful to you, click"Recommendation", Thank you.