Daily code optimisation using benchmarks and profiling in Golang ...

32
Daily code optimisation using benchmarks and profiling in Golang Karthic Rao @hackintoshrao medium.com/@hackintoshrao

Transcript of Daily code optimisation using benchmarks and profiling in Golang ...

Page 1: Daily code optimisation using benchmarks and profiling in Golang ...

Daily code optimisation using benchmarks and profiling in Golang

Karthic Rao @hackintoshrao

medium.com/@hackintoshrao

Page 2: Daily code optimisation using benchmarks and profiling in Golang ...

Recipe for code optimisation• Write benchmark

• CPU Profile

• Memory Profile

• Blocking profile

• Other tricks

Page 3: Daily code optimisation using benchmarks and profiling in Golang ...

Writing benchmarks• Comes bundled with Golang testing package

• And its easy ( overcoming the psychological barrier)

• Compare performance easily

Page 4: Daily code optimisation using benchmarks and profiling in Golang ...
Page 5: Daily code optimisation using benchmarks and profiling in Golang ...

$ go test -bench=. BenchmarkGoMapAdd 5000000 286 ns/op BenchmarkGoStructAdd 2000000000 0.56 ns/op

Page 6: Daily code optimisation using benchmarks and profiling in Golang ...
Page 7: Daily code optimisation using benchmarks and profiling in Golang ...
Page 8: Daily code optimisation using benchmarks and profiling in Golang ...

$ go test run=xxx -bench=.

BenchmarkHandleStructAdd-4 30000 40219 ns/op

Page 9: Daily code optimisation using benchmarks and profiling in Golang ...

Profiling from the benchmarks

go test -run=^$ -bench=. -cpuprofile=profile.cpu

2 new files are created.

A binary ending with .test and the profile info in profile.cpu

go tool pprof <binary> <profile file>

go tool pprof simple-http-benchmark.test profile.cpu

$ go test -run=xxx -bench=. | tee bench0

Page 10: Daily code optimisation using benchmarks and profiling in Golang ...

The interactive profiler topN

top —cum

Page 11: Daily code optimisation using benchmarks and profiling in Golang ...

go tool pprof —pdf bench.test cpu.out > cpu0.pdf

Page 12: Daily code optimisation using benchmarks and profiling in Golang ...
Page 13: Daily code optimisation using benchmarks and profiling in Golang ...

Now compare the performance

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

• Use benchcmp for performance comparison

Page 14: Daily code optimisation using benchmarks and profiling in Golang ...

Build your own tools • Want to build your own tools around Golang

benchmark data? • Use benchmark parse

Page 15: Daily code optimisation using benchmarks and profiling in Golang ...

go tool pprof --pdf bench.test cpu.out > cpu1.pdf

Page 16: Daily code optimisation using benchmarks and profiling in Golang ...

$go tool pprof bench.test cpu.out

list handleStructAdd

Page 17: Daily code optimisation using benchmarks and profiling in Golang ...

$go test -run=xxx -bench=. -cpuprofile=cpu.out

$go tool pprof bench.test cpu.out

Top10

Page 18: Daily code optimisation using benchmarks and profiling in Golang ...

Solving the Mallocgc challenge

• Mallocgc is Golang garbage collector

• GC sweeps the heap allocations once it starts spiking up

• But how to identify the reason behind the high CPU usage of some these runtime functions ?

• Let’s say I want to know about the functions which are contributing highly for the mallogc invocation?

Page 19: Daily code optimisation using benchmarks and profiling in Golang ...

Removing the noise web mallocgc

Page 20: Daily code optimisation using benchmarks and profiling in Golang ...

Again, reduce the noise in the profiling graph

• go tool pprof --nodefraction=0.2 bench.test pro.cpu

Page 21: Daily code optimisation using benchmarks and profiling in Golang ...

Tools in Testing.B

Page 22: Daily code optimisation using benchmarks and profiling in Golang ...

Memory profiling

• Use *testing.B.ReportAlloc()

Page 23: Daily code optimisation using benchmarks and profiling in Golang ...

Running the benchmark

Page 24: Daily code optimisation using benchmarks and profiling in Golang ...

Memory profiler

• $go test -run=^$ -bench=. -memprofile=mem0.out

• — inuse_objects (show count by number of allocations)

• — alloc_space (shows the total allocation size0

• $go tool pprof --alloc_space bench.test mem0.out

Page 25: Daily code optimisation using benchmarks and profiling in Golang ...

Find the top cumulative memory consumers

Page 26: Daily code optimisation using benchmarks and profiling in Golang ...
Page 27: Daily code optimisation using benchmarks and profiling in Golang ...
Page 28: Daily code optimisation using benchmarks and profiling in Golang ...
Page 29: Daily code optimisation using benchmarks and profiling in Golang ...

The modification • func (t *Template) Execute(wr io.Writer, data interface{}) (err

error)

Page 30: Daily code optimisation using benchmarks and profiling in Golang ...

Benchmark and compare

Page 31: Daily code optimisation using benchmarks and profiling in Golang ...

Other tools

Page 32: Daily code optimisation using benchmarks and profiling in Golang ...

Thank you @hackintoshrao

medium.com/@hackintoshrao