1---
  2title: Golang profiling simplified
  3url: golang-profiling-simplified.html
  4date: 2017-03-07T12:00:00+02:00
  5type: post
  6draft: false
  7---
  8
  9Many posts have been written regarding profiling in Golang and I haven’t found
 10proper tutorial regarding this. Almost all of them are missing some part of
 11important information and it gets pretty frustrating when you have a deadline
 12and are not finding simple distilled solution.
 13
 14Nevertheless, after searching and experimenting I have found a solution that
 15works for me and probably should also for you.
 16
 17## Where are my pprof files?
 18
 19By default pprof files are generated in /tmp/ folder. You can override folder
 20where this files are generated programmatically in your golang code as we will
 21see below in example.
 22
 23## Why is my CPU profile empty?
 24
 25I have found out that sometimes CPU profile is empty because program was not
 26executing long enough. Programs, that execute too quickly don’t produce pprof
 27file in my cases. Well, file is generated but only contains 4KB of information.
 28
 29## Profiling
 30
 31As you can see from examples we are executing dummy_benchmark functions to
 32ensure some sort of execution. Memory profiling can be done without such a
 33“complex” function. But CPU profiling needs it.
 34
 35Both memory and CPU profiling examples are almost the same. Only parameters in
 36main function when calling profile.Start are different. When we set
 37profile.ProfilePath(“.”) we tell profiler to store pprof files in the same
 38folder as our program.
 39
 40### Memory profiling
 41
 42```go
 43package main
 44
 45import (
 46  "fmt"
 47  "time"
 48  "github.com/pkg/profile"
 49)
 50
 51func dummy_benchmark() {
 52
 53  fmt.Println("first set ...")
 54  for i := 0; i < 918231333; i++ {
 55    i *= 2
 56    i /= 2
 57  }
 58
 59  <-time.After(time.Second*3)
 60
 61  fmt.Println("sencond set ...")
 62  for i := 0; i < 9182312232; i++ {
 63    i *= 2
 64    i /= 2
 65  }
 66}
 67
 68func main() {
 69  defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
 70  dummy_benchmark()
 71}
 72```
 73
 74### CPU profiling
 75
 76```go
 77package main
 78
 79import (
 80  "fmt"
 81  "time"
 82  "github.com/pkg/profile"
 83)
 84
 85func dummy_benchmark() {
 86
 87  fmt.Println("first set ...")
 88  for i := 0; i < 918231333; i++ {
 89    i *= 2
 90    i /= 2
 91  }
 92
 93  <-time.After(time.Second*3)
 94
 95  fmt.Println("sencond set ...")
 96  for i := 0; i < 9182312232; i++ {
 97    i *= 2
 98    i /= 2
 99  }
100}
101
102func main() {
103  defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
104  dummy_benchmark()
105}
106```
107
108### Generating profiling reports
109
110```bash
111# memory profiling
112go build mem.go
113./mem
114go tool pprof -pdf ./mem mem.pprof > mem.pdf
115
116# cpu profiling
117go build cpu.go
118./cpu
119go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf
120```
121
122This will generate PDF document with visualized profile.
123
124- [Memory PDF profile example](/assets/posts/go-profiling/golang-profiling-mem.pdf)
125- [CPU PDF profile example](/assets/posts/go-profiling/golang-profiling-cpu.pdf)
126