본문 바로가기
C#

C#] 코드 성능 분석 BenchmarkDotNet

by Fastlane 2022. 7. 5.
728x90
반응형

BenchmarkDotNet이란?

오픈소스, NuGet package로 설치할 수 있는 코드 성능 테스트 library 

 

 

사용법

1. Visual Studio 2022, .NET 6 console application project(project name : NET6_Console) 생성

2. Nuget package 관리에서 BenchmarkDotNet 찾아서 설치 

3. MyBenchmarks.cs 파일 추가 

using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NET6_Console
{
    public class MyBenchmarks
    {
        private const int N = 10000;
        private readonly byte[] data;

        private readonly SHA256 sha256 = SHA256.Create();
        private readonly MD5 md5 = MD5.Create();

        public MyBenchmarks()
        {
            data = new byte[N];
            new Random(42).NextBytes(data);
        }

        [Benchmark]
        public byte[] Sha256() => sha256.ComputeHash(data);

        [Benchmark]
        public byte[] Md5() => md5.ComputeHash(data);
    }
}

MyBenchmarks class에는 2개의  methods가 있고, 이 methods의 성능을 측정해보자. 

method에 Benchmark attribyte를 추가한다. 

MyBenchmarks class의 constructor가 radom byte array를 생성한다. 

 

4. Program.cs에 benchmark run trigger 추가하기

아래 3중 하나만 정해서 추가하면 된다. 

// Use any one of below to trigger benchmarking on specific type or assembly

// Run benchmarking on all types in the specified assembly
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);

// Run benchmarking on the specified type
var summary = BenchmarkRunner.Run<MyBenchmarks>();

// Run benchmark on the specified type
var summary = BenchmarkRunner.Run(typeof(MyBenchmarks));

 

5. 실행

실행하는데 2가지 방법이 있다. 

1) 솔루션 구성 Release로 하고, 디버그 -> 디버그하지 않고 시작

2) .NET CLI로 시작, dotnet run -c release

 

결과확인

총 소요 시간은 62초가 걸렸다. 

// ***** BenchmarkRunner: Finish  *****

// * Export *
  BenchmarkDotNet.Artifacts\results\NET6_Console.MyBenchmarks-report.csv
  BenchmarkDotNet.Artifacts\results\NET6_Console.MyBenchmarks-report-github.md
  BenchmarkDotNet.Artifacts\results\NET6_Console.MyBenchmarks-report.html

// * Detailed results *
MyBenchmarks.Sha256: DefaultJob
Runtime = .NET 6.0.4 (6.0.422.16404), X64 RyuJIT; GC = Concurrent Workstation
Mean = 40.216 us, StdErr = 0.064 us (0.16%), N = 14, StdDev = 0.241 us
Min = 39.942 us, Q1 = 40.005 us, Median = 40.136 us, Q3 = 40.443 us, Max = 40.639 us
IQR = 0.437 us, LowerFence = 39.350 us, UpperFence = 41.098 us
ConfidenceInterval = [39.944 us; 40.487 us] (CI 99.9%), Margin = 0.271 us (0.67% of Mean)
Skewness = 0.32, Kurtosis = 1.39, MValue = 2
-------------------- Histogram --------------------
[39.811 us ; 40.770 us) | @@@@@@@@@@@@@@
---------------------------------------------------

MyBenchmarks.Md5: DefaultJob
Runtime = .NET 6.0.4 (6.0.422.16404), X64 RyuJIT; GC = Concurrent Workstation
Mean = 17.270 us, StdErr = 0.016 us (0.09%), N = 15, StdDev = 0.063 us
Min = 17.187 us, Q1 = 17.214 us, Median = 17.254 us, Q3 = 17.325 us, Max = 17.373 us
IQR = 0.111 us, LowerFence = 17.048 us, UpperFence = 17.491 us
ConfidenceInterval = [17.203 us; 17.338 us] (CI 99.9%), Margin = 0.067 us (0.39% of Mean)
Skewness = 0.26, Kurtosis = 1.4, MValue = 2
-------------------- Histogram --------------------
[17.154 us ; 17.407 us) | @@@@@@@@@@@@@@@
---------------------------------------------------

// * Summary *

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19043.1766 (21H1/May2021Update)
Intel Core i5-10400 CPU 2.90GHz, 1 CPU, 12 logical and 6 physical cores
.NET SDK=6.0.202
  [Host]     : .NET 6.0.4 (6.0.422.16404), X64 RyuJIT
  DefaultJob : .NET 6.0.4 (6.0.422.16404), X64 RyuJIT


| Method |     Mean |    Error |   StdDev |
|------- |---------:|---------:|---------:|
| Sha256 | 40.22 us | 0.271 us | 0.241 us |
|    Md5 | 17.27 us | 0.067 us | 0.063 us |

// * Hints *
Outliers
  MyBenchmarks.Sha256: Default -> 1 outlier  was  removed (41.44 us)

// * Legends *
  Mean   : Arithmetic mean of all measurements
  Error  : Half of 99.9% confidence interval
  StdDev : Standard deviation of all measurements
  1 us   : 1 Microsecond (0.000001 sec)

// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:36 (36.35 sec), executed benchmarks: 2

Global total time: 00:01:02 (62.17 sec), executed benchmarks: 2

 

Summary의 Method 컬럼은 측정한 method name이다. 

Mean : 측정값의 평균

Error : 99.9%의 신뢰도 구간의 절반. 

StdDev : 표준편차

 

Md5가 Sha256보다 빠른 것을 알 수 있다. 

 

 

728x90
반응형

댓글