You are on page 1of 14

code optimisation in Golang

Test & Benchmark

using benchmarks and profiling


Recipe for code optimisation

Write benchmark
– Đi kèm với “testing package” của Golang
– Dễ dàng so sánh performance

CPU Profile

Memory Profile

Blocking profile

Other tricks
Package “testing”

Package testing provides support for automated testing &
benchmark of Go packages.
– Được sử dụng phối hợp với lệnh “go test”
– Tính auto cần phải dựa trên các form và syntax:

Syntax file:

Form func:
– File test: “go test” tìm kiếm các file test này và compile

Syntax: <filename>_test.go

Rule: Put the file in the same package as the one being tested

Các file “xxx_test.go” sẽ được loại bỏ (exclude) khi “build” package.
– Tự động tìm kiếm các hàm được viết theo chuẩn (form):

Form: <Prefix><Upper-Char><any alphanumeric string>

Ex:
– func TestXxx(*testing.T)
– func BenchmarkXxx(*testing.B)
Package “testing”

The “go test” command expects to find test, benchmark, and example
functions in the "*_test.go" files corresponding to the package under test.
– A test function is one named TestXXX and should have the signature

func TestXXX(t *testing.T) { ... }
– A benchmark function is one named BenchmarkXXX and should have the signature

func BenchmarkXXX(b *testing.B) { ... }
– An example function is similar to a test function

Not use *testing.T

prints output to os.Stdout.

Method:
– type B is a type passed to Benchmark functions to manage benchmark timing and
to specify the number of iterations to run.
– type T is a type passed to Test functions to manage test state and support
formatted test logs.
Write Benchmark
“-bench=.” : run
func BenchmarkHello(b *testing.B) { go test -bench=. benchmark all func in
for i := 0; i < b.N; i++ { directory
fmt.Sprintf("hello")
}
} BenchmarkHello-4 20000000 103 ns/op

Number of loops
The benchmark function must run the
target code b.N times.

During benchmark execution, b.N is go test -bench=Hello


adjusted until the benchmark function
lasts long enough to be timed reliably. “-bench=Hello” : run benchmark
only BenchmarkHello func

go test -bench=PIndexPath -benchtime=20s

-benchtime flag: fix benchmark


time
Write Benchmark
Compare the performance

Benchcmp makes it easier to compare between various benchmark
results and benchviz make it even more easier by providing simple
visualization.
– go get github.com/ajstarks/svgo/benchviz
– go get golang.org/x/tools/cmd/benchcmp

go test -run=xxx -bench=. | tee bench1

Use benchcmp for performance comparison
– $ benchcmp bench1 bench2

Use benchviz

Use benchcmp & benchviz
– $GOPATH/bin/benchcmp bench1 bench2 | $GOPATH/bin/benchviz > out.svg
CPU Profile

Using Profiling
– Cpuprofile in “go test”

go test -run=^$ -bench=. -cpuprofile=profile.cpu
– 2 new files are created:

A binary ending with .test

The profile info in profile.cpu
– Pprof tool in “go tool pprof”

go tool pprof <binary> <profile file>
– go tool pprof simple-benchmark.test profile.cpu
Memory Profile

Mallocgc is Golang garbage collector
– GC sweeps the heap allocations once it starts spiking up
– Question:

How to identify the reason behind the high CPU usage of some these runtime functions?

What is the functions which are contributing highly for the mallogc invocation?

Use: *testing.B.ReportAlloc()

Use Profiling
– Memprofile in “go test”

go test -run=^$ -bench=. -memprofile=mem0.out
– Pprof tool in “go tool pprof”

$go tool pprof --alloc_space bench.test mem0.out
– inuse_objects (show count by number of allocations)
– alloc_space (shows the total allocation size)
Other tools

Golang blocking profiler

sync.Pool
– To pool and re-use resources

Garbage collector tracer

Memory Allocator tracer

Scheduler tracer

runtime.ReadMemstats
Các yếu tố ảnh hưởng tới
benchmark

RAM:
– Ảnh hưởng 1 chút đối với benchmark
– Ảnh hưởng tới số lượng vòng lặp kiểm thử

CPU
– thermal management: can add noise to benchmark results.

Mallocgc


Compiler optimisations
– Phần: “A note on compiler optimisations”
Các yếu tố ảnh hưởng tới
benchmark
Stress test Go packages

https://dave.cheney.net/2013/06/19/stress-test-
your-go-packages

Ref

Go Performance Tales
Ref

https://golang.org/pkg/testing/

https://dave.cheney.net/2013/06/30/how-to-write
-benchmarks-in-go

http://www.soroushjp.com/2015/01/27/beautifull
y-simple-benchmarking-with-go/

http://www.gophercon.in/pdf/gcon1.pdf

https://medium.com/@hackintoshrao/analyzing-
benchmarks-with-ease-using-benchcmp-and-be
nchviz-golang-add607fc46d6

https://medium.com/@hackintoshrao/daily-code
-optimization-using-benchmarks-and-profiling-in
-golang-gophercon-india-2016-talk-874c8b4dc3

You might also like