aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2017-03-07-golang-profiling-simplified.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2022-08-27 14:05:48 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2022-08-27 14:05:48 +0200
commit9f5454bda6299db43a4e9de5b3716471388b81d9 (patch)
tree1ceedf64a4517a372d70efc2b6f4bbd9478ce792 /content/posts/2017-03-07-golang-profiling-simplified.md
parente728c3a2cbd06d95cd1226d3b23473816bd0d67e (diff)
downloadmitjafelicijan.com-9f5454bda6299db43a4e9de5b3716471388b81d9.tar.gz
Move blog to Hugo
Diffstat (limited to 'content/posts/2017-03-07-golang-profiling-simplified.md')
-rw-r--r--content/posts/2017-03-07-golang-profiling-simplified.md111
1 files changed, 111 insertions, 0 deletions
diff --git a/content/posts/2017-03-07-golang-profiling-simplified.md b/content/posts/2017-03-07-golang-profiling-simplified.md
new file mode 100644
index 0000000..d784dcc
--- /dev/null
+++ b/content/posts/2017-03-07-golang-profiling-simplified.md
@@ -0,0 +1,111 @@
1---
2title: Golang profiling simplified
3url: golang-profiling-simplified.html
4date: 2017-03-07
5draft: false
6---
7
8Many 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.
9
10Nevertheless, after searching and experimenting I have found a solution that works for me and probably should also for you.
11
12## Where are my pprof files?
13
14By 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.
15
16## Why is my CPU profile empty?
17
18I 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.
19
20## Profiling
21
22As 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.
23
24Both 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.
25
26### Memory profiling
27
28```go
29package main
30
31import (
32 "fmt"
33 "time"
34 "github.com/pkg/profile"
35)
36
37func dummy_benchmark() {
38
39 fmt.Println("first set ...")
40 for i := 0; i < 918231333; i++ {
41 i *= 2
42 i /= 2
43 }
44
45 <-time.After(time.Second*3)
46
47 fmt.Println("sencond set ...")
48 for i := 0; i < 9182312232; i++ {
49 i *= 2
50 i /= 2
51 }
52}
53
54func main() {
55 defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
56 dummy_benchmark()
57}
58```
59
60### CPU profiling
61
62```go
63package main
64
65import (
66 "fmt"
67 "time"
68 "github.com/pkg/profile"
69)
70
71func dummy_benchmark() {
72
73 fmt.Println("first set ...")
74 for i := 0; i < 918231333; i++ {
75 i *= 2
76 i /= 2
77 }
78
79 <-time.After(time.Second*3)
80
81 fmt.Println("sencond set ...")
82 for i := 0; i < 9182312232; i++ {
83 i *= 2
84 i /= 2
85 }
86}
87
88func main() {
89 defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
90 dummy_benchmark()
91}
92```
93
94### Generating profiling reports
95
96```bash
97# memory profiling
98go build mem.go
99./mem
100go tool pprof -pdf ./mem mem.pprof > mem.pdf
101
102# cpu profiling
103go build cpu.go
104./cpu
105go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf
106```
107
108This will generate PDF document with visualized profile.
109
110- [Memory PDF profile example](/go-profiling/golang-profiling-mem.pdf)
111- [CPU PDF profile example](/go-profiling/golang-profiling-cpu.pdf)