当前位置:网站首页>Go language core 36 lectures (go language practice and application 26) -- learning notes
Go language core 36 lectures (go language practice and application 26) -- learning notes
2022-06-23 22:06:00 【Zhengziming】
48 | Fundamentals of program performance analysis ( On )
As part of the collection , Today we are going to talk about Go Basic knowledge about program performance analysis .
Go Language provides rich performance analysis for program developers API, And very easy to use standard tools . these API Mainly in :
1、runtime/pprof;
2、net/http/pprof;
3、runtime/trace;
In these three code packages .
in addition ,runtime The code package also contains some lower level API. They can be used to collect or output Go Some key indicators in the process of program operation , And help us generate corresponding profiles for subsequent analysis .
As for standard tools , There are mainly go tool pprof and go tool trace these two items. . They can parse the information in the profile , And display this information in a human readable way .
Besides ,go test The command can also generate a profile after the program test is completed . In this way , We can easily use the previous two tools to read the profile , The performance of the tested program is analyzed . This will undoubtedly enrich the first-hand data of program performance testing , The results are more accurate and credible .
stay Go In language , There are three types of profiles used to analyze program performance , Namely :CPU Profile (CPU Profile)、 Memory profile (Mem Profile) And blocking profiles (Block Profile).
These profiles contain : In a certain period of time , Yes Go Summary information obtained after multiple sampling of relevant indicators of the program .
about CPU For profiles , Each of these separate profiles records , At the moment when a sample is taken ,CPU What's going on on Go Code .
For memory profiles , Each paragraph of the summary information records , At a certain sampling time , Ongoing Go Code and heap memory usage , This includes the number of bytes allocated and released and the number of objects . As for blocking profiles , Summary of each paragraph , All represent Go One of the procedures goroutine Blocking events .
Be careful , By default , The information in these profiles is not ordinary text , They are presented in binary form . If you use a regular text editor to view them , Then I'm sure I'll see a pile of “ The statement ”.
Then you can show go tool pprof The function of this tool . We can use it to enter a command-line based interactive interface , And consult the specified profile . It looks like this :
$ go tool pprof cpuprofile.out Type: cpu Time: Nov 9, 2018 at 4:31pm (CST) Duration: 7.96s, Total samples = 6.88s (86.38%) Entering interactive mode (type "help" for commands, "o" for options) (pprof)
About the specific usage of this tool , I won't go over it here . After entering the interactive interface of the tool , We just need to enter instructions help And press enter , You can see a very detailed help document .
Now let's talk about how to generate a profile .
You may ask , Since the information in the profile is not ordinary text , So what format are they ? For a large number of program developers , The less important fact is , They are through protocol buffers Generated binary data stream , Or byte stream .
A nutshell ,protocol buffers Is a data serialization protocol , It is also a serialization tool . It can put a value , For example, a structure or a dictionary , Convert to a byte stream .
Or vice versa , Invert the byte stream generated by it into a value in the program . The former is called serialization , The latter is called deserialization .
let me put it another way ,protocol buffers Defines and implements a “ Data can be converted between structural form and flat form ” The way .
Protocol buffers There are many advantages . such as , It can compress data while serializing data , So the byte stream it generates , It is usually better than other formats of the same data ( for example XML and JSON) The space occupied is obviously much smaller .
And such as , It allows us to define data serialization and structured format , It also allows us to update this format on the premise of ensuring backward compatibility .
Because of these advantages ,Go Language from 1.8 Version start , Put all the profile The relevant information generation work is entrusted to protocol buffers Here we go . This is also what we in the above profile , The root cause of not seeing ordinary text .
Protocol buffers It's very versatile , And in areas such as data storage 、 Data transmission and other tasks have a high utilization rate . however , About it , I'll stop here for the moment . That's enough for you to know now . You don't have to care runtime/pprof Packages and runtime How the program in the package serializes these profiles .
Continue to the topic of how to generate profiles , We still talk about it through specific questions .
Our question today is : How to make the program right CPU Sample profiles ?
The typical answer to this question is .
It needs to use runtime/pprof In bag API. More specifically , Before we want the program to start on CPU When sampling profiles , You need to call... In this code package StartCPUProfile function , When you stop sampling, you need to call the StopCPUProfile function .
Problem analysis
runtime/pprof.StartCPUProfile function ( hereinafter referred to as StartCPUProfile function ) When called , I'll set it first CPU Sampling frequency of profile information , And will be in a separate goroutine In the middle of CPU Collection and output of profile information .
Be careful ,StartCPUProfile The sampling frequency set by the function is always fixed , namely :100 Hertz . in other words , Samples per second 100 Time , Or every 10 One millisecond sample .
Hertz , Also known as Hz, From English words “Hertz”( An English surname ) A transliterated Chinese word . It is CPU The basic unit of dominant frequency .
CPU The dominant frequency of refers to ,CPU The clock frequency at which the kernel works , It's often called CPU clock speed. The reciprocal of this clock frequency is the clock cycle (clock cycle), That's one CPU The time required for the kernel to execute an operation instruction , The unit is seconds .
for example , The main frequency is 1000Hz Of CPU, The time required for a single kernel to execute an operation instruction is 0.001 second , namely 1 millisecond . For example , What we use now 3.2GHz The multicore CPU, Its single core is 1 At least three operation instructions can be executed in a nanosecond time .
StartCPUProfile Function set CPU Profile sampling frequency , Compared with modern CPU The dominant frequency is very low . There are two main reasons for this .
One side , Too high sampling frequency will affect Go The running efficiency of the program has a significant negative impact . therefore ,runtime In bag SetCPUProfileRate When the function is called , It will ensure that the sampling frequency does not exceed 1MHz( Megahertz ), in other words , It only allows every 1 Microseconds can be sampled at most once .StartCPUProfile Function is set by calling this function CPU The sampling frequency of the profile .
On the other hand , After a lot of experiments ,Go The language team found 100Hz Is a more appropriate setting . Because you can get enough 、 Enough useful summary information , Without stalling the operation of the program . in addition , The processing ability of the operating system for high-frequency sampling is also limited , In general , exceed 500Hz You may not get a timely response .
stay StartCPUProfile After function execution , A newly enabled goroutine Will be responsible for implementation CPU Collection and output of profile information , until runtime/pprof In bag StopCPUProfile The function was called successfully .
StopCPUProfile The function also calls runtime.SetCPUProfileRate function , And put the parameter value ( That is, the sampling frequency ) Set to 0. This will allow for CPU The sampling of the profile stopped .
meanwhile , It will also be responsible for collecting CPU The code of the profile is a “ The signal ”, To inform that the collection work also needs to be stopped .
After receiving such “ The signal ” after , That part of the program will take all the data collected during this period CPU General information , Write all to what we are calling StartCPUProfile Function in the specified writer . Only after all the above operations are completed ,StopCPUProfile Function will return .
Okay , After this explanation , You should have been right CPU Have a certain understanding of the sampling of profile information . You can go and have a look at demo96.go Code in file , And run it several times to try . This will help you deepen your understanding of the problem .
package main
import (
"errors"
"fmt"
"os"
"puzzlers/article37/common"
"puzzlers/article37/common/op"
"runtime/pprof"
)
var (
profileName = "cpuprofile.out"
)
func main() {
f, err := common.CreateFile("", profileName)
if err != nil {
fmt.Printf("CPU profile creation error: %v\n", err)
return
}
defer f.Close()
if err := startCPUProfile(f); err != nil {
fmt.Printf("CPU profile start error: %v\n", err)
return
}
if err = common.Execute(op.CPUProfile, 10); err != nil {
fmt.Printf("execute error: %v\n", err)
return
}
stopCPUProfile()
}
func startCPUProfile(f *os.File) error {
if f == nil {
return errors.New("nil file")
}
return pprof.StartCPUProfile(f)
}
func stopCPUProfile() {
pprof.StopCPUProfile()
}summary
Our two articles are about Go Program performance analysis , These contents are some necessary knowledge and skills for you to engage in this task .
First , We need to know , Related to program performance analysis API Mainly in runtime、runtime/pprof and net/http/pprof In these code packages . Their performance summary can help us collect the corresponding information , And output this information to our designated place .
Go The runtime system of the language will sample the relevant indicators of the program many times according to the requirements , And organize and sort out the sampling results , Finally, a complete performance analysis report is formed . This report is a summary of the summary information we have been talking about .
In general , We will output the profile information to the file . Depending on the profile , There are three main types of profiles , Namely :CPU Profile (CPU Profile)、 Memory profile (Mem Profile) And blocking profiles (Block Profile).
Note source code
边栏推荐
- Common commands for cleaning up kubernetes cluster resources
- MySQL de duplication query only keeps one latest record
- The "Star" industry in the small town is escorted by wechat cloud hosting
- Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip
- Nanny level anti crawling teaching, JS reverse implementation of font anti crawling
- 《scikit-learn机器学习实战》简介
- What is the reason for the error when calling API prompt 401 after easycvr replaces the version?
- Introduction to scikit learn machine learning practice
- What is the gold content of PMP certificate
- [js] generate random array
猜你喜欢

The latest research progress of domain generalization from CVPR 2022
Performance optimization of database 5- database, table and data migration

Introduction to scikit learn machine learning practice

个税怎么算?你知道吗

北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)

Find my information | Apple may launch the second generation airtag. Try the Lenz technology find my solution

Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip

数据可视化之:没有西瓜的夏天不叫夏天

Code implementation of CAD drawing online web measurement tool (measuring distance, area, angle, etc.)

University of North China, Berkeley University of California, etc. | Domain Adaptive Text Classification with structural Knowledge from unlabeled data
随机推荐
Take you through the distributed file system
Question: how to understand the network protocol and why the OSI reference model is divided into seven layers
How to build an API gateway and how to maintain an API gateway?
How does the hybrid cloud realize the IP sec VPN cloud networking dedicated line to realize the interworking between the active and standby intranet?
After CVM is configured with IPv6, it cannot be accessed as IPv6 or cannot access IPv6 sites
Ffmpeg for audio and video commands
[js] generate random array
Open source C # WPF control library --newbeecoder UI usage guide (III)
How to dynamically insert a picture into a QR code
Code implementation of CAD drawing online web measurement tool (measuring distance, area, angle, etc.)
How API gateway finds the role of microserver gateway in microservices
Flink practical tutorial: advanced 4-window top n
Prometheus primary body test
Warpspeed 2021 DFINITY × IAF hacker song demo day ends, and 10 teams win awards
How to deal with high memory in API gateway how to maintain API gateway
Go language core 36 lectures (go language practice and application 27) -- learning notes
How to calculate individual income tax? You know what?
Take you to understand the working process of the browser
What is the gold content of PMP certificate
Surprise! Edge computing will replace cloud computing??