About golang cpu performance analysis

藏色散人
Release: 2020-12-25 16:04:18
forward
3083 people have browsed it

The following column golang tutorial will introduce golang cpu performance analysis to you. I hope it will be helpful to friends in need!

About golang cpu performance analysis

1. Analyze the running time of the program

(1) time command (under Linux system)

time go run a.go
Copy after login

real: Start from the program To the end, the actual time spent

user: the time the program spent in the user attitude

sys: the time the program spent in the kernel

General situation Under, real>=user sys

(2) /usr/bin/time command (under Linux system)

/usr/bin/time -v go run a.go
Copy after login

Under this command, you can see the cpu occupancy, memory usage, and process Switching situation, file system io, socket situation

2. CPU performance analysis under golang

(1) Introduce _ "net/http/pprof" into the program and enable pprof monitoring

 import _
Copy after login
http.ListenAndServe("0.0.0.0:10000", nil)
Copy after login
程序结束
Copy after login

Check the CPU information and status through the browser

http://127.0.0.1:10000/debug/pprof
Copy after login

Note, wait for the program to run for a certain period of time, and then click to generate the profile file (at least 30s)

(2) Use pprof

go tool pprof [binary] [profile] //binary二进制文件 profile 要分析的文件
Copy after login
top //查看当前profile文件的cpu使用率
Copy after login

flat The execution time of the function’s own code

flat% The execution time of the function’s own code occupies the percentage of CPU time

cum represents the execution time of all functions called by the function's own code

cum% represents the execution time of all functions called by the function's own code ⓓ, the total CPU usage percentage

sum% The flat% of each row and the sum of the flat% of all the above rows

(3) go tool pprof profile file

Start the program to be debugged and execute go tool pprof http://localhost:10000/debug/pprof/profile?seconds=60

(4) Visual structure diagram

先在终端运行 go tool pprof [binary] [profile] ,然后输入web,浏览器会弹出一个可视化的图片。
Copy after login

For more related technical articles, please visit the go language tutorial column!

The above is the detailed content of About golang cpu performance analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!