Lesson 12 of 60 · Go
Benchmarking & Profiling
Duration: 15 minutes
Benchmarking
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
Benchmarks live in _test.go files and use the *testing.B argument.
func BenchmarkFib(b *testing.B) {
for i := 0; i < b.N; i++ { fib(20) }
}
Run with the -bench flag:
$ go test -bench=.
Profiling CPU & Memory
$ go test -cpuprofile=cpu.out -memprofile=mem.out ./...
$ go tool pprof cpu.out
Inside the pprof REPL you can type top, list Fib, web (requires Graphviz).
Race Detector
Detect data races in concurrent code:
$ go run -race main.go