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