• Golang Performance Optimization Analysis Tool Pprof

Four,.net/HTTP/pprof

4.1 Code Example 1

Go version go1.13.9

Change the above program example slightly and call it demohttp.go:

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"sync"
)

func main(a) {
	http.HandleFunc("/pprof-test", handler)

	fmt.Println("http server start")
	err := http.ListenAndServe(": 8090".nil)
	iferr ! =nil {
		log.Fatal(err)
	}
}

func handler(resp http.ResponseWriter, req *http.Request) {
	var wg sync.WaitGroup
	wg.Add(200)

	for i := 0; i < 200; i++ {
		go cyclenum(30000, &wg)
	}

	wg.Wait()

	wb := writeBytes()
	b, err := ioutil.ReadAll(wb)
	iferr ! =nil {
		resp.Write([]byte(err.Error()))
		return
	}
	resp.Write(b)
}

func cyclenum(num int, wg *sync.WaitGroup) {
	slice := make([]int.0)
	for i := 0; i < num; i++ {
		for j := 0; j < num; j++ {
			j = i + j
			slice = append(slice, j)
		}
	}
	wg.Done()
}

func writeBytes(a) *bytes.Buffer {
	var buff bytes.Buffer

	for i := 0; i < 30000; i++ {
		buff.Write([]byte{'a' + byte(rand.Intn(10))})}return &buff
}
Copy the code

4.2 Starting Analysis

4.2.1 Analysis on the Web UI

To start, run the demohttp.go program above and execute the command:

go run demohttp.go

In the browser then enter: http://localhost:8090/debug/pprof/, view the service operation, the following figure:

The name of the url instructions
allocs $host/debug/pprof/allocs? debug=1 All past memory sampling cases
block $host/debug/pprof/block? debug=1 Some cases of stack tracing when synchronization blocks
heap $host/debug/pprof/heap? debug=1 Memory allocation of live objects
mutex $host/debug/pprof/mutex? debug=1 Stack frames for mutex holders
profile $host/debug/pprof/profile CPU profile, when clicked, gets a file that can then be analyzed using the Go Tool pprof command
threadcreate $host/debug/pprof/threadcreate? debug=1 Create a stack trace for a new OS thread
trace $host/debug/pprof/trace When clicked, you will get a file that can be analyzed using the Go Tool Trace command

Click the link above to see the analysis. As you refresh the page, you can see that the data is changing.

4.2.2 Command Line Interaction Analysis

Run the demohttp.go program on the command line and execute the following command:

go run demohttp.go

A. Analyze CPU profiles

To start another command line terminal, run the following command:

go tool pprof http://localhost:8090/debug/pprof/profile? seconds=70

Parameter seconds = 70: Data sample collection 70s. This parameter can be adjusted according to the actual situation.

After executing the above command, wait 70s before entering the command interface, as shown in the figure above

Enter the top command:

The runtime/pprof command line interaction is the same as the runtime/pprof command line interaction.

You can also use the command: list to find the parts of the code that take time.

Did you find any problems after executing the top command? The top command displays all system call information, not user-defined functions. Why is that? Let’s analyze it.

B. Analyze the memory profile

Execute command:

go tool pprof http://localhost:8090/debug/pprof/heap

Then enter the top command again to view the function usage, as shown below:

The rest of the trace analysis commands are similar and will not be analyzed.

Visual analysis of the above interactive analysis data in the terminal command line.

4.2.3 Graphic visualization analysis

A. Pprof graphic visualization

In the previous visualization analysis, we learned that there are two most important steps in visualization: 1. Data collection 2. Graphically collected data.

In the third section runtime/pprof above, enter the terminal command line interaction and then enter the Web command to generate an IMAGE in SVG format that can be viewed directly in a browser. Let’s try it the same way.

  1. Enter the command:

go tool pprof http://localhost:8090/debug/pprof/profile? seconds=30

  1. Wait 30 seconds and enterwebThe command

The diagram below:

Sure enough, an SVG file is generated. When you look at the image file in your browser, there is no useful information, as shown below:

Why is there no useful information? As mentioned earlier, no user is accessing the HTTP server, and the required program is not running and is blocked waiting for the client to access the connection, so go Tool Pprof can only collect information about part of the code running, and this part of the code does not consume much CPU.

So what?

One approach is to simulate user access with HTTP testing tools. Here we use github.com/rakyll/hey. Hey installation:

go get -u github.com/rakyll/hey

After the installation is complete, perform HTTP tests:

hey -n 1000 http://localhost:8090/pprof-test

Run the following command on another terminal:

go tool pprof http://localhost:8090/debug/pprof/profile? seconds=120

After 120s, information collection is complete, as shown in the following figure:

The inputtopCommand to view statistics:

You can see that one of the most time-consuming user-defined functions is main.cyclenum. To see the most time-consuming part of the code for this function, use the list cyclenum command.

We’re going to generate a picture here, so typewebCommand to generate images:

View an SVG image in a browser:

(The image is larger and only partially captured)

This figure shows the complete information of the top command.

B. Web visualization

Execute command:

go tool pprof -http=”:8080″ http://localhost:8090/debug/pprof/profile

At the same time, open another terminal to execute the test command:

hey -n 200 -q 5 http://localhost:8090/pprof-test

Go above the tool pprof execution is completed, will automatically open an HTTP address to the browser, http://localhost:8080/ui/, the diagram below:

(Part of the picture taken)

This allows you to view the analysis data in a Web browser.

C. the flame

Use HTTP to test framework Hey access with the following command:

hey -n 200 -q 5 http://localhost:8090/pprof-test

Start another terminal at the same time of pressure measurement and execute the command:

go-torch -u http://localhost:8090

To generate the flame pattern.

The terminal outputs information when running the command:

Run pprof command: go tool pprof -raw -seconds 30 http://localhost:8090/debug/pprof/profile

You can see that the go-Torch’s original command also uses the Go Tool pprof

By default, this command generates a torch. SVG flame map file, as follows:

(Part of the picture is captured and displayed)

Click on the box to see more details:

reference

  • pprof
    • README
  • Profiling Go Programs
  • runtime/pprof
  • net/http/pprof
  • go-torch
  • Flame Graph
  • HTTP pressure tool hey