1. What is pprof? Why use pprof?
1.1 What is PPROF?
- A tool used to do performance analysis
1.2 What angles can Pprof analyze from
- CPU Profiling: CPU Profiling, which collects monitored application CPU usage (including registers) at a given frequency, can determine where an application is spending time while actively consuming CPU cycles
- Memory Profiling: Memory Profiling, which records stack traces as an application makes heap allocation, is used to monitor current and historical Memory usage, and to check for Memory leaks
- Block Profiling: Block Profiling records where goroutine blocks waiting for synchronization, including timer channels
- Mutex Profiling: Mutex Profiling, reporting on Mutex contention
2. How to use pprof?
package main
import (
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
netpprof "net/http/pprof"
"os"
"runtime/pprof"
"time"
)
func main(a) {
//pprofSingerProgram() // Analyze a single program example
//pprofHttp() // Analyze an HTTP service example
pprofDiyHttp() // Analyze a custom HTTP service example
}
func pprofSingerProgram(a) {
f, _ := os.Create("./test.pprof")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for i := 0; i < 100000; i++ {
LocalTz()
DoSomething([]byte(`{"a": 1, "b": 2, "c": 3}`))}}func pprofHttp(a) {
go func(a) {
for {
LocalTz()
DoSomething([]byte(`{"a": 1, "b": 2, "c": 3}`))
}
}()
time.Sleep(1)
fmt.Println("start api server...")
panic(http.ListenAndServe(": 8081".nil))}func pprofDiyHttp(a) {
var httpSvc *http.Server
httpAddr := ": 8081"
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", netpprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", netpprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", netpprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", netpprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", netpprof.Trace)
httpSvc = &http.Server{
Addr: httpAddr,
Handler: mux,
}
iferr := httpSvc.ListenAndServe(); err ! =nil&& err ! = http.ErrServerClosed { fmt.Println(err) } }func DoSomething(s []byte) {
var m map[string]interface{}
err := json.Unmarshal(s, &m)
iferr ! =nil {
panic(err)
}
s1 := make([]string.0)
s2 := ""
for i := 0; i < 100; i++ {
s1 = append(s1, string(s))
s2 += string(s)
}
}
func LocalTz(a) *time.Location {
tz, _ := time.LoadLocation("Asia/Shanghai")
return tz
}
Copy the code
2.1 Used in common programs
- Example code above: pprofSingerProgram
2.2 Used on the HTTP Server
- Example code above: pprofHttp
- MAC install graphviz
- brew install graphviz
- If you encounter an error, you can switch to the CUST source and execute brew Update
- Use in gin framework: go get “github.com/gin-contrib/pprof”
2.3 Used in the SELF-defined HTTP Server
- You need to register routes
2.3 Obtaining addresses of Various types of data
- For all data: http://127.0.0.1:6060/debug/pprof/, listed below are other relevant data, if you are new on the corresponding access path? With debug=1, you can access it directly from the browser. Otherwise, you just download the file
- Allocs: View a sample of all past memory allocations at $HOST/debug/pprof/allocs.
- Block: View the stack trace that causes the blocking synchronization at $HOST/debug/pprof/block.
- Cmdline: the full call path to the command line of the current program.
- Goroutine: look at all the currently running goroutines stack trace, the access path for $HOST/debug/pprof/goroutine.
- Heap: To view the memory allocation of active objects, access the path $HOST/debug/pprof/heap.
- Mutex: View the stack trace of the competing owner that caused the mutex, with access to $HOST/debug/pprof/mutex.
- Profile: CPU Profiling is performed for 30 seconds by default, resulting in a profile for analysis, accessed at $HOST/debug/pprof/profile.
- Threadcreate: view to create a new OS thread stack trace, access path for $HOST/debug/pprof/threadcreate.
2.4 Obtaining flame chart:
- go tool pprof -http=:803 http://localhost:802/debug/pprof/profile -seconds 10
- – HTTP =803: listens on a port for viewing on the web page
- -seconds 10: collects information in the last 10 seconds
3. What about the flame chart?
- The first is root, which represents the starting point of all functions
- The functions in a row of data are parallel, that is, between row elements
- Each function in a column of data is the child function of the previous row, which is responsible upward, that is, there is a parent-child relationship between the columns
- There exists a list of functions divided into multiple functions, the length of which represents their time
4. Reference links
- Jvns.ca /blog/2017/0…
- Pprof official support flame figure: zhuanlan.zhihu.com/p/71529062
- Fish detail pprof:segmentfault.com/a/119000001…
- Fish book (journey to the go programming language) about: golang2.eddycjy.com/posts/ch6/0…