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