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