aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2017-03-07-golang-profiling-simplified.md
diff options
context:
space:
mode:
authorMitja Felicijan <m@mitjafelicijan.com>2023-07-08 23:25:41 +0200
committerMitja Felicijan <m@mitjafelicijan.com>2023-07-08 23:25:41 +0200
commitcd6644ea4ddc78597934ab0ef5ba50e3c3daa927 (patch)
tree03de331a8db6386dfd6fa75155bfbcea6b4feaf3 /content/posts/2017-03-07-golang-profiling-simplified.md
parent84ed124529ffeee1590295b8de3a8faf51848680 (diff)
downloadmitjafelicijan.com-cd6644ea4ddc78597934ab0ef5ba50e3c3daa927.tar.gz
Moved to a simpler SSG
Diffstat (limited to 'content/posts/2017-03-07-golang-profiling-simplified.md')
-rw-r--r--content/posts/2017-03-07-golang-profiling-simplified.md125
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---
2title: Golang profiling simplified
3url: golang-profiling-simplified.html
4date: 2017-03-07T12:00:00+02:00
5draft: false
6---
7
8Many posts have been written regarding profiling in Golang and I haven’t found
9proper tutorial regarding this. Almost all of them are missing some part of
10important information and it gets pretty frustrating when you have a deadline
11and are not finding simple distilled solution.
12
13Nevertheless, after searching and experimenting I have found a solution that
14works for me and probably should also for you.
15
16## Where are my pprof files?
17
18By default pprof files are generated in /tmp/ folder. You can override folder
19where this files are generated programmatically in your golang code as we will
20see below in example.
21
22## Why is my CPU profile empty?
23
24I have found out that sometimes CPU profile is empty because program was not
25executing long enough. Programs, that execute too quickly don’t produce pprof
26file in my cases. Well, file is generated but only contains 4KB of information.
27
28## Profiling
29
30As you can see from examples we are executing dummy_benchmark functions to
31ensure some sort of execution. Memory profiling can be done without such a
32“complex” function. But CPU profiling needs it.
33
34Both memory and CPU profiling examples are almost the same. Only parameters in
35main function when calling profile.Start are different. When we set
36profile.ProfilePath(“.”) we tell profiler to store pprof files in the same
37folder as our program.
38
39### Memory profiling
40
41```go
42package main
43
44import (
45 "fmt"
46 "time"
47 "github.com/pkg/profile"
48)
49
50func 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
67func main() {
68 defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
69 dummy_benchmark()
70}
71```
72
73### CPU profiling
74
75```go
76package main
77
78import (
79 "fmt"
80 "time"
81 "github.com/pkg/profile"
82)
83
84func 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
101func 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
111go build mem.go
112./mem
113go tool pprof -pdf ./mem mem.pprof > mem.pdf
114
115# cpu profiling
116go build cpu.go
117./cpu
118go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf
119```
120
121This 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