go pprof使用

package main

import (
	"log"
	"net/http"
	_ "net/http/pprof"
)

func dfs(a int) int {
	if a == 0 || a == 1 {
		return 1
	}
	return (dfs(a-1) + dfs(a-2)) % 100
}
func main() {
	go func() {
		log.Println(http.ListenAndServe(":6060", nil))
	}()
	for i := 0; i < 100000000; i++ {
		dfs(10)
	}
}

浏览器访问
http://127.0.0.1:6060/debug/pprof/

go pprof使用_第1张图片
会下载heap文件

使用命令

go tool pprof heap       

分析heap文件
常用命令 top,

yuki@ubuntu  ~/Documents  cd ..1 ↵  247016:00:47 
 yuki@ubuntu  ~  cd Downloads                           ✔  247116:00:52 
 yuki@ubuntu  ~/Downloads  go tool pprof heap           ✔  247216:00:56 
File: ___go_build_main_go
Type: inuse_space
Time: Feb 12, 2023 at 2:28pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 2048.85kB, 100% of 2048.85kB total
Showing top 10 nodes out of 23
      flat  flat%   sum%        cum   cum%
  512.50kB 25.01% 25.01%   512.50kB 25.01%  runtime.allocm
  512.20kB 25.00% 50.01%   512.20kB 25.00%  runtime.malg
  512.12kB 25.00% 75.01%  1024.15kB 49.99%  net/http.ListenAndServe (inline)
  512.03kB 24.99%   100%   512.03kB 24.99%  syscall.anyToSockaddr
         0     0%   100%   512.03kB 24.99%  internal/poll.(*FD).Accept
         0     0%   100%   512.03kB 24.99%  internal/poll.accept
         0     0%   100%  1024.15kB 49.99%  main.main.func1
         0     0%   100%   512.03kB 24.99%  net.(*TCPListener).Accept
         0     0%   100%   512.03kB 24.99%  net.(*TCPListener).accept
         0     0%   100%   512.03kB 24.99%  net.(*netFD).accept
(pprof) 

火焰图

安装 graphviz

sudo apt install graphviz

运行命令

go tool pprof -http=:8080 "http://127.0.0.1:6060/debug/pprof/heap" 

显示
go pprof使用_第2张图片
选择 flame graph
go pprof使用_第3张图片

你可能感兴趣的:(golang,开发语言,后端)