├── .gitignore ├── Makefile ├── README.md ├── animal ├── animal.go ├── canidae │ ├── canidae.go │ ├── dog │ │ └── dog.go │ └── wolf │ │ └── wolf.go ├── felidae │ ├── cat │ │ └── cat.go │ ├── felidae.go │ └── tiger │ │ └── tiger.go └── muridae │ ├── mouse │ └── mouse.go │ └── muridae.go ├── constant └── size.go ├── go.mod └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *pprof-* 3 | go-pprof-practice 4 | /testdata/ 5 | # Created by https://www.gitignore.io/api/macos,windows,linux,intellij,go 6 | # Edit at https://www.gitignore.io/?templates=macos,windows,linux,intellij,go 7 | 8 | ### Go ### 9 | # Binaries for programs and plugins 10 | *.exe 11 | *.exe~ 12 | *.dll 13 | *.so 14 | *.dylib 15 | 16 | # Test binary, built with `go test -c` 17 | *.test 18 | 19 | # Output of the go coverage tool, specifically when used with LiteIDE 20 | *.out 21 | 22 | ### Go Patch ### 23 | /vendor/ 24 | /Godeps/ 25 | 26 | ### Intellij ### 27 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 28 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 29 | 30 | # User-specific stuff 31 | .idea/**/workspace.xml 32 | .idea/**/tasks.xml 33 | .idea/**/usage.statistics.xml 34 | .idea/**/dictionaries 35 | .idea/**/shelf 36 | 37 | # Generated files 38 | .idea/**/contentModel.xml 39 | 40 | # Sensitive or high-churn files 41 | .idea/**/dataSources/ 42 | .idea/**/dataSources.ids 43 | .idea/**/dataSources.local.xml 44 | .idea/**/sqlDataSources.xml 45 | .idea/**/dynamic.xml 46 | .idea/**/uiDesigner.xml 47 | .idea/**/dbnavigator.xml 48 | 49 | # Gradle 50 | .idea/**/gradle.xml 51 | .idea/**/libraries 52 | 53 | # Gradle and Maven with auto-import 54 | # When using Gradle or Maven with auto-import, you should exclude module files, 55 | # since they will be recreated, and may cause churn. Uncomment if using 56 | # auto-import. 57 | # .idea/modules.xml 58 | # .idea/*.iml 59 | # .idea/modules 60 | 61 | # CMake 62 | cmake-build-*/ 63 | 64 | # Mongo Explorer plugin 65 | .idea/**/mongoSettings.xml 66 | 67 | # File-based project format 68 | *.iws 69 | 70 | # IntelliJ 71 | out/ 72 | 73 | # mpeltonen/sbt-idea plugin 74 | .idea_modules/ 75 | 76 | # JIRA plugin 77 | atlassian-ide-plugin.xml 78 | 79 | # Cursive Clojure plugin 80 | .idea/replstate.xml 81 | 82 | # Crashlytics plugin (for Android Studio and IntelliJ) 83 | com_crashlytics_export_strings.xml 84 | crashlytics.properties 85 | crashlytics-build.properties 86 | fabric.properties 87 | 88 | # Editor-based Rest Client 89 | .idea/httpRequests 90 | 91 | # Android studio 3.1+ serialized cache file 92 | .idea/caches/build_file_checksums.ser 93 | 94 | ### Intellij Patch ### 95 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 96 | 97 | # *.iml 98 | # modules.xml 99 | # .idea/misc.xml 100 | # *.ipr 101 | 102 | # Sonarlint plugin 103 | .idea/sonarlint 104 | 105 | ### Linux ### 106 | *~ 107 | 108 | # temporary files which can be created if a process still has a handle open of a deleted file 109 | .fuse_hidden* 110 | 111 | # KDE directory preferences 112 | .directory 113 | 114 | # Linux trash folder which might appear on any partition or disk 115 | .Trash-* 116 | 117 | # .nfs files are created when an open file is removed but is still being accessed 118 | .nfs* 119 | 120 | ### macOS ### 121 | # General 122 | .DS_Store 123 | .AppleDouble 124 | .LSOverride 125 | 126 | # Icon must end with two \r 127 | Icon 128 | 129 | # Thumbnails 130 | ._* 131 | 132 | # Files that might appear in the root of a volume 133 | .DocumentRevisions-V100 134 | .fseventsd 135 | .Spotlight-V100 136 | .TemporaryItems 137 | .Trashes 138 | .VolumeIcon.icns 139 | .com.apple.timemachine.donotpresent 140 | 141 | # Directories potentially created on remote AFP share 142 | .AppleDB 143 | .AppleDesktop 144 | Network Trash Folder 145 | Temporary Items 146 | .apdisk 147 | 148 | ### Windows ### 149 | # Windows thumbnail cache files 150 | Thumbs.db 151 | ehthumbs.db 152 | ehthumbs_vista.db 153 | 154 | # Dump file 155 | *.stackdump 156 | 157 | # Folder config file 158 | [Dd]esktop.ini 159 | 160 | # Recycle Bin used on file shares 161 | $RECYCLE.BIN/ 162 | 163 | # Windows Installer files 164 | *.cab 165 | *.msi 166 | *.msix 167 | *.msm 168 | *.msp 169 | 170 | # Windows shortcuts 171 | *.lnk 172 | 173 | # End of https://www.gitignore.io/api/macos,windows,linux,intellij,go 174 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: dep test check build run clean 2 | 3 | BINARY_NAME="pprof" 4 | 5 | MAIN_GO=main.go 6 | 7 | dep: 8 | go mod download 9 | 10 | test: 11 | go test ./... 12 | 13 | test_coverage: 14 | go test ./... -coverprofile=coverage.out 15 | 16 | check: 17 | go fmt ./... 18 | go vet ./... 19 | 20 | build: dep check test 21 | GOARCH=arm64 GOOS=darwin go build -o ${BINARY_NAME}-arm64-darwin ${MAIN_GO} 22 | GOARCH=amd64 GOOS=darwin go build -o ${BINARY_NAME}-amd64-darwin ${MAIN_GO} 23 | GOARCH=amd64 GOOS=linux go build -o ${BINARY_NAME}-amd64-linux ${MAIN_GO} 24 | GOARCH=amd64 GOOS=windows go build -o ${BINARY_NAME}-amd64-windows ${MAIN_GO} 25 | 26 | run: 27 | ./${BINARY_NAME}-linux 28 | 29 | clean: 30 | go clean 31 | rm -f ${BINARY_NAME}-arm64-darwin 32 | rm -f ${BINARY_NAME}-amd64-darwin 33 | rm -f ${BINARY_NAME}-amd64-linux 34 | rm -f ${BINARY_NAME}-amd64-windows 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-pprof-practice 2 | 3 | - [《golang pprof 实战》](https://blog.wolfogre.com/posts/go-ppof-practice/)代码实验用例。 4 | - [《Go性能分析工具》](https://farmerchillax.github.io/2023/07/04/Go%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7/)代码实验用例。 5 | -------------------------------------------------------------------------------- /animal/animal.go: -------------------------------------------------------------------------------- 1 | package animal 2 | 3 | import ( 4 | "github.com/wolfogre/go-pprof-practice/animal/canidae/dog" 5 | "github.com/wolfogre/go-pprof-practice/animal/canidae/wolf" 6 | "github.com/wolfogre/go-pprof-practice/animal/felidae/cat" 7 | "github.com/wolfogre/go-pprof-practice/animal/felidae/tiger" 8 | "github.com/wolfogre/go-pprof-practice/animal/muridae/mouse" 9 | ) 10 | 11 | var ( 12 | AllAnimals = []Animal{ 13 | &dog.Dog{}, 14 | &wolf.Wolf{}, 15 | 16 | &cat.Cat{}, 17 | &tiger.Tiger{}, 18 | 19 | &mouse.Mouse{}, 20 | } 21 | ) 22 | 23 | type Animal interface { 24 | Name() string 25 | Live() 26 | 27 | Eat() 28 | Drink() 29 | Shit() 30 | Pee() 31 | } 32 | -------------------------------------------------------------------------------- /animal/canidae/canidae.go: -------------------------------------------------------------------------------- 1 | package canidae 2 | 3 | import "github.com/wolfogre/go-pprof-practice/animal" 4 | 5 | type Canidae interface { 6 | animal.Animal 7 | Run() 8 | Howl() 9 | } 10 | -------------------------------------------------------------------------------- /animal/canidae/dog/dog.go: -------------------------------------------------------------------------------- 1 | package dog 2 | 3 | import ( 4 | "log" 5 | 6 | "github.com/wolfogre/go-pprof-practice/constant" 7 | ) 8 | 9 | type Dog struct { 10 | } 11 | 12 | func (d *Dog) Name() string { 13 | return "dog" 14 | } 15 | 16 | func (d *Dog) Live() { 17 | d.Eat() 18 | d.Drink() 19 | d.Shit() 20 | d.Pee() 21 | d.Run() 22 | d.Howl() 23 | } 24 | 25 | func (d *Dog) Eat() { 26 | log.Println(d.Name(), "eat") 27 | } 28 | 29 | func (d *Dog) Drink() { 30 | log.Println(d.Name(), "drink") 31 | } 32 | 33 | func (d *Dog) Shit() { 34 | log.Println(d.Name(), "shit") 35 | } 36 | 37 | func (d *Dog) Pee() { 38 | log.Println(d.Name(), "pee") 39 | } 40 | 41 | func (d *Dog) Run() { 42 | log.Println(d.Name(), "run") 43 | _ = make([]byte, 16*constant.Mi) 44 | } 45 | 46 | func (d *Dog) Howl() { 47 | log.Println(d.Name(), "howl") 48 | } 49 | -------------------------------------------------------------------------------- /animal/canidae/wolf/wolf.go: -------------------------------------------------------------------------------- 1 | package wolf 2 | 3 | import ( 4 | "log" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | type Wolf struct { 10 | buffer []byte 11 | } 12 | 13 | func (w *Wolf) Name() string { 14 | return "wolf" 15 | } 16 | 17 | func (w *Wolf) Live() { 18 | w.Eat() 19 | w.Drink() 20 | w.Shit() 21 | w.Pee() 22 | w.Run() 23 | w.Howl() 24 | } 25 | 26 | func (w *Wolf) Eat() { 27 | log.Println(w.Name(), "eat") 28 | } 29 | 30 | func (w *Wolf) Drink() { 31 | log.Println(w.Name(), "drink") 32 | for i := 0; i < 10; i++ { 33 | go func() { 34 | time.Sleep(30 * time.Second) 35 | }() 36 | } 37 | } 38 | 39 | func (w *Wolf) Shit() { 40 | log.Println(w.Name(), "shit") 41 | } 42 | 43 | func (w *Wolf) Pee() { 44 | log.Println(w.Name(), "pee") 45 | } 46 | 47 | func (w *Wolf) Run() { 48 | log.Println(w.Name(), "run") 49 | } 50 | 51 | func (w *Wolf) Howl() { 52 | log.Println(w.Name(), "howl") 53 | 54 | m := &sync.Mutex{} 55 | m.Lock() 56 | go func() { 57 | time.Sleep(time.Second) 58 | m.Unlock() 59 | }() 60 | m.Lock() 61 | } 62 | -------------------------------------------------------------------------------- /animal/felidae/cat/cat.go: -------------------------------------------------------------------------------- 1 | package cat 2 | 3 | import ( 4 | "log" 5 | "time" 6 | ) 7 | 8 | type Cat struct { 9 | } 10 | 11 | func (c *Cat) Name() string { 12 | return "cat" 13 | } 14 | 15 | func (c *Cat) Live() { 16 | c.Eat() 17 | c.Drink() 18 | c.Shit() 19 | c.Pee() 20 | c.Climb() 21 | c.Sneak() 22 | } 23 | 24 | func (c *Cat) Eat() { 25 | log.Println(c.Name(), "eat") 26 | } 27 | 28 | func (c *Cat) Drink() { 29 | log.Println(c.Name(), "drink") 30 | } 31 | 32 | func (c *Cat) Shit() { 33 | log.Println(c.Name(), "shit") 34 | } 35 | 36 | func (c *Cat) Pee() { 37 | log.Println(c.Name(), "pee") 38 | 39 | <-time.After(time.Second) 40 | } 41 | 42 | func (c *Cat) Climb() { 43 | log.Println(c.Name(), "climb") 44 | } 45 | 46 | func (c *Cat) Sneak() { 47 | log.Println(c.Name(), "sneak") 48 | } 49 | -------------------------------------------------------------------------------- /animal/felidae/felidae.go: -------------------------------------------------------------------------------- 1 | package felidae 2 | 3 | import "github.com/wolfogre/go-pprof-practice/animal" 4 | 5 | type Felidae interface { 6 | animal.Animal 7 | Climb() 8 | Sneak() 9 | } 10 | -------------------------------------------------------------------------------- /animal/felidae/tiger/tiger.go: -------------------------------------------------------------------------------- 1 | package tiger 2 | 3 | import "log" 4 | 5 | type Tiger struct { 6 | } 7 | 8 | func (t *Tiger) Name() string { 9 | return "tiger" 10 | } 11 | 12 | func (t *Tiger) Live() { 13 | t.Eat() 14 | t.Drink() 15 | t.Shit() 16 | t.Pee() 17 | t.Climb() 18 | t.Sneak() 19 | } 20 | 21 | func (t *Tiger) Eat() { 22 | log.Println(t.Name(), "eat") 23 | loop := 10000000000 24 | for i := 0; i < loop; i++ { 25 | // do nothing 26 | } 27 | } 28 | 29 | func (t *Tiger) Drink() { 30 | log.Println(t.Name(), "drink") 31 | } 32 | 33 | func (t *Tiger) Shit() { 34 | log.Println(t.Name(), "shit") 35 | } 36 | 37 | func (t *Tiger) Pee() { 38 | log.Println(t.Name(), "pee") 39 | } 40 | 41 | func (t *Tiger) Climb() { 42 | log.Println(t.Name(), "climb") 43 | } 44 | 45 | func (t *Tiger) Sneak() { 46 | log.Println(t.Name(), "sneak") 47 | } 48 | -------------------------------------------------------------------------------- /animal/muridae/mouse/mouse.go: -------------------------------------------------------------------------------- 1 | package mouse 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | "github.com/wolfogre/go-pprof-practice/constant" 8 | ) 9 | 10 | type Mouse struct { 11 | buffer [][constant.Mi]byte 12 | slowBuffer [][constant.Mi]byte 13 | } 14 | 15 | func (*Mouse) Name() string { 16 | return "mouse" 17 | } 18 | 19 | func (m *Mouse) Live() { 20 | m.Eat() 21 | m.Drink() 22 | m.Shit() 23 | m.Pee() 24 | m.Hole() 25 | m.Steal() 26 | } 27 | 28 | func (m *Mouse) Eat() { 29 | log.Println(m.Name(), "eat") 30 | } 31 | 32 | func (m *Mouse) Drink() { 33 | log.Println(m.Name(), "drink") 34 | } 35 | 36 | func (m *Mouse) Shit() { 37 | log.Println(m.Name(), "shit") 38 | } 39 | 40 | func (m *Mouse) Pee() { 41 | log.Println(m.Name(), "pee") 42 | go func() { 43 | time.Sleep(time.Second * 30) 44 | max := constant.Gi 45 | for len(m.slowBuffer)*constant.Mi < max { 46 | m.slowBuffer = append(m.slowBuffer, [constant.Mi]byte{}) 47 | time.Sleep(time.Millisecond * 500) 48 | } 49 | }() 50 | } 51 | 52 | func (m *Mouse) Hole() { 53 | log.Println(m.Name(), "hole") 54 | } 55 | 56 | func (m *Mouse) Steal() { 57 | log.Println(m.Name(), "steal") 58 | max := constant.Gi 59 | for len(m.buffer)*constant.Mi < max { 60 | m.buffer = append(m.buffer, [constant.Mi]byte{}) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /animal/muridae/muridae.go: -------------------------------------------------------------------------------- 1 | package muridae 2 | 3 | import "github.com/wolfogre/go-pprof-practice/animal" 4 | 5 | type Muridae interface { 6 | animal.Animal 7 | Hole() 8 | Steal() 9 | } 10 | -------------------------------------------------------------------------------- /constant/size.go: -------------------------------------------------------------------------------- 1 | package constant 2 | 3 | const ( 4 | Ki = 1024 5 | Mi = Ki * Ki 6 | Gi = Ki * Mi 7 | Ti = Ki * Gi 8 | Pi = Ki * Ti 9 | ) 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/wolfogre/go-pprof-practice 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | _ "net/http/pprof" 7 | "os" 8 | "runtime" 9 | "time" 10 | 11 | "github.com/wolfogre/go-pprof-practice/animal" 12 | ) 13 | 14 | func main() { 15 | log.SetFlags(log.Lshortfile | log.LstdFlags) 16 | log.SetOutput(os.Stdout) 17 | 18 | runtime.GOMAXPROCS(1) 19 | runtime.SetMutexProfileFraction(1) 20 | runtime.SetBlockProfileRate(1) 21 | 22 | go func() { 23 | if err := http.ListenAndServe(":6060", nil); err != nil { 24 | log.Fatal(err) 25 | } 26 | os.Exit(0) 27 | }() 28 | 29 | for { 30 | for _, v := range animal.AllAnimals { 31 | v.Live() 32 | } 33 | time.Sleep(time.Second) 34 | } 35 | } 36 | --------------------------------------------------------------------------------