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