diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
| commit | 23a56bd50b04211da3cab45f72c3390711b2416b (patch) | |
| tree | ab9a4a0136b4cce06dba7d853e296f682f807dbb /content/posts/2017-03-07-golang-profiling-simplified.md | |
| parent | cecb4b48a39a3558979b9c4b50e45bf605a3684e (diff) | |
| download | mitjafelicijan.com-23a56bd50b04211da3cab45f72c3390711b2416b.tar.gz | |
Moved notes and posts into subfolders
Diffstat (limited to 'content/posts/2017-03-07-golang-profiling-simplified.md')
| -rw-r--r-- | content/posts/2017-03-07-golang-profiling-simplified.md | 126 |
1 files changed, 126 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..ee3a210 --- /dev/null +++ b/content/posts/2017-03-07-golang-profiling-simplified.md | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | --- | ||
| 2 | title: Golang profiling simplified | ||
| 3 | url: golang-profiling-simplified.html | ||
| 4 | date: 2017-03-07T12:00:00+02:00 | ||
| 5 | type: post | ||
| 6 | draft: false | ||
| 7 | --- | ||
| 8 | |||
| 9 | Many posts have been written regarding profiling in Golang and I haven’t found | ||
| 10 | proper tutorial regarding this. Almost all of them are missing some part of | ||
| 11 | important information and it gets pretty frustrating when you have a deadline | ||
| 12 | and are not finding simple distilled solution. | ||
| 13 | |||
| 14 | Nevertheless, after searching and experimenting I have found a solution that | ||
| 15 | works for me and probably should also for you. | ||
| 16 | |||
| 17 | ## Where are my pprof files? | ||
| 18 | |||
| 19 | By default pprof files are generated in /tmp/ folder. You can override folder | ||
| 20 | where this files are generated programmatically in your golang code as we will | ||
| 21 | see below in example. | ||
| 22 | |||
| 23 | ## Why is my CPU profile empty? | ||
| 24 | |||
| 25 | I have found out that sometimes CPU profile is empty because program was not | ||
| 26 | executing long enough. Programs, that execute too quickly don’t produce pprof | ||
| 27 | file in my cases. Well, file is generated but only contains 4KB of information. | ||
| 28 | |||
| 29 | ## Profiling | ||
| 30 | |||
| 31 | As you can see from examples we are executing dummy_benchmark functions to | ||
| 32 | ensure some sort of execution. Memory profiling can be done without such a | ||
| 33 | “complex” function. But CPU profiling needs it. | ||
| 34 | |||
| 35 | Both memory and CPU profiling examples are almost the same. Only parameters in | ||
| 36 | main function when calling profile.Start are different. When we set | ||
| 37 | profile.ProfilePath(“.”) we tell profiler to store pprof files in the same | ||
| 38 | folder as our program. | ||
| 39 | |||
| 40 | ### Memory profiling | ||
| 41 | |||
| 42 | ```go | ||
| 43 | package main | ||
| 44 | |||
| 45 | import ( | ||
| 46 | "fmt" | ||
| 47 | "time" | ||
| 48 | "github.com/pkg/profile" | ||
| 49 | ) | ||
| 50 | |||
| 51 | func dummy_benchmark() { | ||
| 52 | |||
| 53 | fmt.Println("first set ...") | ||
| 54 | for i := 0; i < 918231333; i++ { | ||
| 55 | i *= 2 | ||
| 56 | i /= 2 | ||
| 57 | } | ||
| 58 | |||
| 59 | <-time.After(time.Second*3) | ||
| 60 | |||
| 61 | fmt.Println("sencond set ...") | ||
| 62 | for i := 0; i < 9182312232; i++ { | ||
| 63 | i *= 2 | ||
| 64 | i /= 2 | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | func main() { | ||
| 69 | defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop() | ||
| 70 | dummy_benchmark() | ||
| 71 | } | ||
| 72 | ``` | ||
| 73 | |||
| 74 | ### CPU profiling | ||
| 75 | |||
| 76 | ```go | ||
| 77 | package main | ||
| 78 | |||
| 79 | import ( | ||
| 80 | "fmt" | ||
| 81 | "time" | ||
| 82 | "github.com/pkg/profile" | ||
| 83 | ) | ||
| 84 | |||
| 85 | func dummy_benchmark() { | ||
| 86 | |||
| 87 | fmt.Println("first set ...") | ||
| 88 | for i := 0; i < 918231333; i++ { | ||
| 89 | i *= 2 | ||
| 90 | i /= 2 | ||
| 91 | } | ||
| 92 | |||
| 93 | <-time.After(time.Second*3) | ||
| 94 | |||
| 95 | fmt.Println("sencond set ...") | ||
| 96 | for i := 0; i < 9182312232; i++ { | ||
| 97 | i *= 2 | ||
| 98 | i /= 2 | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | func main() { | ||
| 103 | defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop() | ||
| 104 | dummy_benchmark() | ||
| 105 | } | ||
| 106 | ``` | ||
| 107 | |||
| 108 | ### Generating profiling reports | ||
| 109 | |||
| 110 | ```bash | ||
| 111 | # memory profiling | ||
| 112 | go build mem.go | ||
| 113 | ./mem | ||
| 114 | go tool pprof -pdf ./mem mem.pprof > mem.pdf | ||
| 115 | |||
| 116 | # cpu profiling | ||
| 117 | go build cpu.go | ||
| 118 | ./cpu | ||
| 119 | go tool pprof -pdf ./cpu cpu.pprof > cpu.pdf | ||
| 120 | ``` | ||
| 121 | |||
| 122 | This will generate PDF document with visualized profile. | ||
| 123 | |||
| 124 | - [Memory PDF profile example](/assets/go-profiling/golang-profiling-mem.pdf) | ||
| 125 | - [CPU PDF profile example](/assets/go-profiling/golang-profiling-cpu.pdf) | ||
| 126 | |||
