This is a creation in Article, where the information may have evolved or changed.
If you have high performance requirements for your code, this section should work for you. The tools in Golang have a tool to collect CPU profile information. Specific Use method:
You can refer to the RUNTIME/PPROF package in your code.
Main.go
Package Main
Import "Runtime/pprof"
int main () {
F, err: = OS. Create ("Cpu.prof")
If err! = Nil {
Can output some error messages
Return
}
Pprof. Startcpuprofile (f)
Defer pprof. Stopcpuprofile ()
Other code ...
}
Go Build Main.go
./main
After execution, the Cpu.prof file is generated in the current directory, which records the information that the code runs samples. Note that your code is not too easy to remember because the PPROF sample is 100 times per second, so if your code runs too short (for example, if you do 1ms less), you probably won't be able to sample the data (remember to use the time. Sleep lengthening The execution result is useless because I tried it. So the following steps are meaningless. Keep in mind that the code is complex enough to execute long enough to write only one print.
Top command
Now that you have the profile file, you can execute the following command to see the results:
[TC01.TC Work test] $go tool pprof main cpu.prof
Welcome to pprof! For help, type ' help '.
(PPROF)
Perform TOPN in the Pprof tool, for example:
(pprof) Top10
There will be the following display
To raise a line in the display result: total:1972 samples is the total sampled data. What is sampling, that is, when the code is running, Pprof pauses the program and then looks at the current program execution into that function by checking the stack information of each goroutine and counting all the functions in the stack.
Here's what each field means, the last field is the function name, the first field is the number of samples for the function, and the second field is the percentage of the total number of samples that the function samples. The third field is the percent accumulation field, and the fourth field is the sampled data in the function, because the program stops at the B function at the time of the sampling process, and if the A function calls the B function, then A and b two functions will be recorded. The fifth field is naturally the percentage of the fourth field.
List command
Perhaps you are a very serious person, then you must want to see carefully, the following list command lets you see, the sample specific to which line of code:
The index in the result is the function name, which is the result of the sample in the index function, the first column is the sample point of the function, the second column is the sum of the sample points at the function and the function sample points that are called by the function. For example, the first column in line 143th has no data because the sample results here are only sampled in the HASHSTR function.
Web commands
Of course, you will feel that the above is not intuitive, the following method can directly form a picture, you can view the picture in the browser, but first you want to install Graphviz in your computer, this is a library, used to generate pictures. What you need to do is to execute the Web in the Pprof tool:
(pprof) Web
The resulting results are:
The result in loading Web page file:////tmp/IwzpEzlPkp.0.svg shows that the generated file is in the directory/tmp/, and the name is Iwzpezlpkp.0.svg. Of course, make sure you have space on your disk. The following is the hint of can ' t because no browser can start up to view the file, so ignore it. To view this file, download it and view it, and here is the chart of my generation.
Summarize
Of course there are a lot of commands to use, but these commands are enough for you to understand the performance of the code. Other relevant commands you can study it yourself. Of course, the tools here are not for you to look at, but to provide guidance and justification for optimizing the code. For example, if you see where the code is sampled more, it means that the place is executed more often or the code is less efficient, and the code is analyzed in detail. Sometimes you find that the frequent use of maps causes most of the sample points in a function to fall on the map operation, and I think it might be better to choose slice if possible. Of course, when make is created, slice is time-consuming than map, but it is created once.