[Go]

[Go] Benchmark 사용법 (How to Use Benchmarks)

dogfootman 2024. 6. 25. 23:18

개요

Benchmark의 사용법

 

 

1. 기본 사용법

func BenchmarkXxx(*testing.B)

 

의 형태를 가지고 있으면 벤치마크로 간주되며

-bench 의 플러그가 있으면 go test 의 명령에 의해서 실행된다

 

 

샘플코드

package main

...

func BenchmarkDeepCopyManual(b *testing.B) {
	original := Student{
		Name:    "Alice",
		Age:     21,
		Hobbies: []string{"Reading", "Cycling", "Swimming"},
		Address: Address{
			City:  "San Francisco",
			State: "CA",
		},
	}

	for i := 0; i < b.N; i++ {
		_ = deepCopyManual(original)
	}
}

func BenchmarkDeepCopyJSON(b *testing.B) {
	original := Student{
		Name:    "Alice",
		Age:     21,
		Hobbies: []string{"Reading", "Cycling", "Swimming"},
		Address: Address{
			City:  "San Francisco",
			State: "CA",
		},
	}

	for i := 0; i < b.N; i++ {
		_, err := deepCopyJSON(original)
		if err != nil {
			b.Fatalf("Error: %v", err)
		}
	}
}

func BenchmarkDeepCopyGob(b *testing.B) {
	original := Student{
		Name:    "Alice",
		Age:     21,
		Hobbies: []string{"Reading", "Cycling", "Swimming"},
		Address: Address{
			City:  "San Francisco",
			State: "CA",
		},
	}

	for i := 0; i < b.N; i++ {
		_, err := deepCopyGob(original)
		if err != nil {
			b.Fatalf("Error: %v", err)
		}
	}
}

func BenchmarkDeepCopyReflect(b *testing.B) {
	original := Student{
		Name:    "Alice",
		Age:     21,
		Hobbies: []string{"Reading", "Cycling", "Swimming"},
		Address: Address{
			City:  "San Francisco",
			State: "CA",
		},
	}

	for i := 0; i < b.N; i++ {
		var copyStudent Student
		deepCopyReflect(&copyStudent, original)
	}
}

와 같이 작성해 xxx_test.go 의 형태로 저장

  • b.N: 테스트가 반복되는 횟수이며, 자동적으로 계산 된다

 

실행 커맨드 예)

-bench regexp

'-bench .' or '-bench=.'

 

ex) 모든 벤치마크 함수를 실행한다

go test -bench .

 

참고 윈도우? 환경에선 -bench=. 의 형태로 사용 할 땐 ""로 감싸주어야 실행이 되는듯 하다

go test -bench="."

 

ex) *JSON형태의 함수를 실행한다

 go test -bench ".*JSON"
 go test -bench=".*JSON"

 

 

2. 결과 값

PS D:\Study\golang\project\deepcopy\deepcopybench> go test -bench .                      
goos: windows
goarch: amd64
pkg: project/deepcopy/deepcopybench
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkDeepCopyManual-16      32283057                33.78 ns/op
BenchmarkDeepCopyJSON-16          561468              2260 ns/op
BenchmarkDeepCopyGob-16            69405             17542 ns/op
BenchmarkDeepCopyReflect-16      4331769               273.4 ns/op
PASS
ok      project/deepcopy/deepcopybench  5.565s

 

결과 값 표시 내용

  • BenchmarkDeepCopyManual-16
    • BenchmarkDeepCopyManual: 실행된 메서드
    • -16: 병렬로 실행 된 고루틴의 수 (= CPU 수)
  • 32283057: 실행 수
  • 33.78 ns/op: 평균 실행 속도 ns 단위

 

 

3. 실행 시간 지정 (-benchtime t)

ex) 10초 동안 실행

go test -bench . -benchtime 10s

기본값은 1s

PS D:\Study\golang\project\deepcopy\deepcopybench> go test -bench . -benchtime 10s
goos: windows
goarch: amd64
pkg: project/deepcopy/deepcopybench
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkDeepCopyManual-16      365221957               33.65 ns/op
BenchmarkDeepCopyJSON-16         5229326              2264 ns/op
BenchmarkDeepCopyGob-16           674312             17697 ns/op
BenchmarkDeepCopyReflect-16     44499788               276.3 ns/op
PASS
ok      project/deepcopy/deepcopybench  54.713s

대략 10배의 시간이 걸린 것을 알수있다

 

 

4. 반복 횟수 지정 (-count n)

ex) 3회 반복

go test -bench . -count 3

기본값은 1

 

PS D:\Study\golang\project\deepcopy\deepcopybench> go test -bench . -count 3     
goos: windows
goarch: amd64
pkg: project/deepcopy/deepcopybench
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkDeepCopyManual-16      36163094                33.08 ns/op
BenchmarkDeepCopyManual-16      36837273                33.15 ns/op
BenchmarkDeepCopyManual-16      36041664                34.14 ns/op
BenchmarkDeepCopyJSON-16          541074              2209 ns/op
BenchmarkDeepCopyJSON-16          509419              2241 ns/op
BenchmarkDeepCopyJSON-16          526886              2216 ns/op
BenchmarkDeepCopyGob-16            68505             17692 ns/op
BenchmarkDeepCopyGob-16            68971             17452 ns/op
BenchmarkDeepCopyGob-16            67466             17293 ns/op
BenchmarkDeepCopyReflect-16      4377800               276.3 ns/op
BenchmarkDeepCopyReflect-16      4317730               273.9 ns/op
BenchmarkDeepCopyReflect-16      4263261               270.8 ns/op
PASS
ok      project/deepcopy/deepcopybench  16.097s

1초씩 3회 반복 된다

 

 

5. 메모리 할당량 측정 (-benchmem)

go test -bench . -benchmem

 

 

PS D:\Study\golang\project\deepcopy\deepcopybench> go test -bench . -benchmem
goos: windows
goarch: amd64
pkg: project/deepcopy/deepcopybench
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkDeepCopyManual-16      34451178                33.11 ns/op           48 B/op          1 allocs/op
BenchmarkDeepCopyJSON-16          485587              2297 ns/op             720 B/op         18 allocs/op
BenchmarkDeepCopyGob-16            65107             17625 ns/op           10284 B/op        261 allocs/op
BenchmarkDeepCopyReflect-16      4331397               270.4 ns/op           232 B/op          4 allocs/op
PASS
ok      project/deepcopy/deepcopybench  5.385s

 

 

추가 된 결과 값의 의미

  • 48 B/op: 각 실행에서 평균적으로 48바이트의 메모리가 할당
  • 1 allocs/op: 각 실행에서 평균적으로 1번의 메모리가 할당

 

6. CPU 코어 수 지정  (-cpu n,n,n...)

go test -bench . -cpu 1,4

각각 CPU의 코어를 1개 4개씩 사용하여 실행한다

 

PS D:\Study\golang\project\deepcopy\deepcopybench> go test -bench . -cpu 1,4 
goos: windows
goarch: amd64
pkg: project/deepcopy/deepcopybench
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
BenchmarkDeepCopyManual         35368495                32.29 ns/op
BenchmarkDeepCopyManual-4       36784426                33.96 ns/op
BenchmarkDeepCopyJSON             520654              2265 ns/op
BenchmarkDeepCopyJSON-4           556598              2231 ns/op
BenchmarkDeepCopyGob               68617             17370 ns/op
BenchmarkDeepCopyGob-4             67000             17880 ns/op
BenchmarkDeepCopyReflect         4287910               270.8 ns/op
BenchmarkDeepCopyReflect-4       4495464               277.1 ns/op
PASS
ok      project/deepcopy/deepcopybench  10.904s

 

 

7. 기타 옵션의 확인

go help testflag 명령어나 

아래의 참조 사이트를 통해서 확인 가능

 

참조: https://pkg.go.dev/testing#hdr-Benchmarks

 

testing package - testing - Go Packages

Discover Packages Standard library testing Version: go1.22.4 Opens a new window with list of versions in this module. Published: Jun 4, 2024 License: BSD-3-Clause Opens a new window with license information. Imports: 24 Opens a new window with list of impo

pkg.go.dev

참조: https://pkg.go.dev/cmd/go#hdr-Testing_flags

 

go command - cmd/go - Go Packages

Discover Packages Standard library cmd go Version: go1.22.4 Opens a new window with list of versions in this module. Published: Jun 4, 2024 License: BSD-3-Clause Opens a new window with license information. Imports: 34 Opens a new window with list of impor

pkg.go.dev