The GoFrame framework’s Web Server offers a very robust and easy-to-use service performance analysis feature. It is perfectly integrated with the pprof performance analysis tool, which can be activated anytime via the EnablePProf method. You can also customize the route address for the performance analysis tool; if not provided, the default URI is /debug/pprof.

PProf Activation

PProf Performance Analysis - 图1warning

The activation of the PProf feature will have some impact on program performance. The specific degree of influence should be compared before and after the PProf is enabled based on the current business scenario.

EnablePProf

Let’s look at a simple example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "runtime"
  6. )
  7. func main() {
  8. runtime.SetMutexProfileFraction(1) // (optional) Enable lock call tracing
  9. runtime.SetBlockProfileRate(1) // (optional) Enable blocking operation tracing
  10. s := g.Server()
  11. s.EnablePProf()
  12. s.BindHandler("/", func(r *ghttp.Request) {
  13. r.Response.Writeln("Hello World!")
  14. })
  15. s.SetPort(8199)
  16. s.Run()
  17. }

This example uses s.EnablePProf() to enable performance analysis. By default, the following routes are registered:

  1. /debug/pprof/*action
  2. /debug/pprof/cmdline
  3. /debug/pprof/profile
  4. /debug/pprof/symbol
  5. /debug/pprof/trace

Among these, /debug/pprof/*action is used for page access, and the others are prepared for the go tool pprof command.

StartPProfServer

You can also use the StartPProfServer method to quickly start an independent PProf Server, often used in long-running processes without an HTTP Server (such as cron jobs or GRPC services), for program performance analysis. The method is defined as:

  1. func StartPProfServer(port int, pattern ...string)

In general, it is used in an asynchronous goroutine to run the PProf Server, typically like this:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. )
  5. func main() {
  6. go ghttp.StartPProfServer(8199)
  7. // Other services startup and running
  8. // ...
  9. }

The above example can be improved to:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. go ghttp.StartPProfServer(8299)
  8. s := g.Server()
  9. s.EnablePProf()
  10. s.BindHandler("/", func(r *ghttp.Request) {
  11. r.Response.Writeln("Hello World!")
  12. })
  13. s.SetPort(8199)
  14. s.Run()
  15. }

PProf Metrics

  • heap: Reports memory allocation samples for monitoring current and historical memory usage and investigating memory leaks.
  • threadcreate: Reports sections of the program that caused the creation of a new OS thread.
  • goroutine: Reports stack traces of all current goroutines.
  • block: Shows where goroutines block waiting on synchronization primitives (including timer channels). Not enabled by default; needs runtime.SetBlockProfileRate to enable.
  • mutex: Reports lock contention. Not enabled by default; needs runtime.SetMutexProfileFraction to enable.

PProf Pages

For simple performance analysis, directly accessing the /debug/pprof address is sufficient, and the content is as follows:

  1. pprof page

PProf Performance Analysis - 图2

  1. Heap Usage

PProf Performance Analysis - 图3

  1. Details of goroutines in the current process

PProf Performance Analysis - 图4

Performance Data Collection and Analysis🔥

PProf Performance Analysis - 图5tip

The screenshots in the examples below are sourced from sample projects and are for reference only.

For detailed performance analysis, the go tool pprof command-line tool is indispensable. After enabling performance analysis support, you can perform performance data collection and analysis with the following command:

  1. go tool pprof -http :8080 "http://127.0.0.1:8199/debug/pprof/profile"

You can also export the pprof file and then open it with the go tool pprof command:

  1. curl http://127.0.0.1:8199/debug/pprof/profile > pprof.profile
  2. go tool pprof -http :8080 pprof.profile

After approximately 30 seconds of API data collection by the pprof tool (during which time the WebServer should have incoming traffic), a performance analysis report is generated. You can then view the report results using top10/web and other pprof commands. For more commands, use go tool pprof. For detailed usage of pprof, please refer to Golang’s official documentation: blog.golang.org/profiling-go-programs

CPU Performance Analysis

The command line performance analysis result in this example is as follows:

  1. $ go tool pprof -http :8080 "http://127.0.0.1:8199/debug/pprof/profile"
  2. Serving web UI on http://localhost:8080

PProf Performance Analysis - 图6tip

To display pprof graphically, the Graphviz graphical tool needs to be installed. For my current system, Ubuntu, install by executing sudo apt-get install graphviz (for MacOS, use brew install Graphviz).

After running, it will open the following graphical API using the default browser, displaying the CPU cost path captured during this period:

PProf Performance Analysis - 图7

You can also view the flame graph, which might be more illustrative:

PProf Performance Analysis - 图8

Memory Usage Analysis

Similar to CPU performance analysis, memory usage analysis also utilizes the go tool pprof command:

  1. $ go tool pprof -http :8080 "http://127.0.0.1:8199/debug/pprof/heap"
  2. Serving web UI on http://localhost:8080

The graphical display is similar to this:

PProf Performance Analysis - 图9

Again, you can view the flame graph, which might be more illustrative:

PProf Performance Analysis - 图10

Goroutine Usage Analysis

Similar to the above analysis, goroutine usage analysis also uses the go tool pprof command:

  1. $ go tool pprof -http :8080 "http://127.0.0.1:8199/debug/pprof/goroutine"
  2. Serving web UI on http://localhost:8080

The graphical display is similar to this, and usually, when looking at goroutines, this graph is sufficient, although there is also a flame graph available.

PProf Performance Analysis - 图11