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