Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pprof, a performance analysis tool for Go language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to use pprof, the performance analysis tool of Go language, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

First, an overview of pprof:

Pprof: is the performance analysis tool of Go. During the running of the program, you can record the running information of the program, such as CPU usage, memory usage, goroutine operation, etc. Go language has encapsulated pprof in the package net/http/pprof.

For pprof, it is mainly used for: CPU analysis, memory analysis, blocking analysis, mutex analysis.

There are two ways to view these metrics, one is the browser, and the other is the command line.

Browser mode:

Access through http://pprofIPAddress:port/debug/pprof/, and the interface after access is as follows:

Command line mode:

Basic commands:

# download cpu profile. By default, collect 30 seconds of cpu usage from the current start. Need to wait for 30sgo tool pprof http://localhost:6060/debug/pprof/profile# 30-second CPU profile go tool pprof http://localhost:6060/debug/pprof/profile?seconds=120# wait 120s# download heap profile go tool pprof http://localhost:6060/debug/pprof/heap# heap profile# download goroutine profile go tool pprof http://localhost:6060/debug/pprof/goroutine# goroutine profile# download block profile go tool pprof http://localhost:6060/debug/pprof/block# Goroutine blocking profile# downloads mutex profile go tool pprof http://localhost:6060/debug/pprof/mutex

This article is an introduction to pprof, focusing on the use of CPU analysis and memory analysis.

2. Memory analysis

The memory here refers to the heap data in Go, as shown in the following example:

Package mainimport ("fmt", "time", "sync", "net/http" _ "net/http/pprof")

Var buf [] bytefunc Add () {tick: = time.Tick (time.Second / 200) for range tick {buf = append (buf, make ([] byte, 2 "1024" 1024)...)} func main () {/ / enable pprof Listen to the request var wg sync.WaitGroup wg.Add (1) go func () {defer wg.Done () ip: = "0.0.0.0 defer wg.Done 6060" if err: = http.ListenAndServe (ip, nil) Err! = nil {fmt.Printf ("start pprof failed on% s\ n", ip)} () fmt.Println ("continue~") Add () wg.Wait ()}

Usually, to analyze memory information, you need to use go tool pprof http://localhost:6060/debug/pprof/heap. Generally, samples are compared many times to see the changes in memory. To view memory information, the three commands top, list, and traces in pprof are mainly used, as follows:

Step 1: generate two memory analysis files, depending on your needs. In this case, the interval is about 1 minute and 30 seconds.

Step 2: compare the differences between two memory files.

$go tool pprof-base ~ / pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz ~ / pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz

-base: indicates that the first memory file is used as a comparison sample.

Step 3: analyze memory information.

View memory information through top, list, and traces. From the execution result below, we can see that the mian.Add function uses the most memory, and the 14-line buf = append (buf,make ([] byte), 2 '1024' 1024) in Add is the source of the new memory.

For traces, Represents the call relationship of a stack.

Note: the introduction of float and cum is as follows:

Cum Sort entries based on cumulative weight

Flat Sort entries based on own weight

3. CPU analysis:

The usage analysis of CPU is simpler than memory. After all, CPU does not need to be divided into several parts for comparison. The analysis steps are as follows:

Step 1: collect cpu data information.

Command: $go tool pprof http://localhost:6060/debug/pprof/profile

Step 2: analyze CPU information.

Through top, list, traces to further analyze the hot spots of CPU.

Top and list to view the usage time of cpu:

Traces Analysis:

After reading the above, do you have any further understanding of how to use pprof, the performance analysis tool of Go language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report