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