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