Home >Backend Development >Golang >About golang cpu performance analysis

About golang cpu performance analysis

藏色散人
藏色散人forward
2020-12-25 16:04:183143browse

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

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

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 _
http.ListenAndServe("0.0.0.0:10000", nil)
程序结束

Check the CPU information and status through the browser

http://127.0.0.1:10000/debug/pprof

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 要分析的文件
top //查看当前profile文件的cpu使用率

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,浏览器会弹出一个可视化的图片。

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete