golang 之前只看过一些资料,近期也开始重点写golang了,需要我看的书的可以去这里免费下载pdf
最近在loggie中开发了filestream 模块,一个滚动日志采集模块;由于一些为止原因不能很准确定位到性能瓶颈问题,做php和c++方面的工作,第一反应是看metrics指标、第二反应是看性能图
c++ php 都有 hprof 和 perf 等工具可以很轻松的查找到瓶颈点。
瓶颈分析一般要求遵循摩尔定律,找主要矛盾,解决主要问题。
golang初步调研,需要弄清楚,golang如何做性能调优,有没有类似的工具,如何生产火焰图,解决是什么和怎么办的问题。
golang 是有pprof 这一类工具的,默认就支持
pprof.go中有详细注释
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package pprof serves via its HTTP server runtime profiling data
// in the format expected by the pprof visualization tool.
//
// The package is typically only imported for the side effect of
// registering its HTTP handlers.
// The handled paths all begin with /debug/pprof/.
//
// To use pprof, link this package into your program:
// import _ "net/http/pprof"
//
// If your application is not already running an http server, you
// need to start one. Add "net/http" and "log" to your imports and
// the following code to your main function:
//
// go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
// }()
//
// If you are not using DefaultServeMux, you will have to register handlers
// with the mux you are using.
//
// Then use the pprof tool to look at the heap profile:
//
// go tool pprof http://localhost:6060/debug/pprof/heap
//
// Or to look at a 30-second CPU profile:
//
// go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
//
// Or to look at the goroutine blocking profile, after calling
// runtime.SetBlockProfileRate in your program:
//
// go tool pprof http://localhost:6060/debug/pprof/block
//
// Or to look at the holders of contended mutexes, after calling
// runtime.SetMutexProfileFraction in your program:
//
// go tool pprof http://localhost:6060/debug/pprof/mutex
//
// The package also exports a handler that serves execution trace data
// for the "go tool trace" command. To collect a 5-second execution trace:
//
// curl -o trace.out http://localhost:6060/debug/pprof/trace?seconds=5
// go tool trace trace.out
//
// To view all available profiles, open http://localhost:6060/debug/pprof/
// in your browser.
//
// For a study of the facility in action, visit
//
// https://blog.golang.org/2011/06/profiling-go-programs.html
什么是 Profile?
在计算机性能调试领域里,profile 就是对应用的画像,这里画像就是应用使用 CPU 和内存等情况,也就是说应用使用了多少 CPU 资源、都是哪些部分在使用、每个函数使用的比例是多少、有哪些函数在等待 CPU 资源等等。知道了这些,我们就能对应用进行规划,也能快速定位性能瓶颈。
Golang 是一个对性能特别看重的语言,因此语言中自带了 profile 的库,这篇文章就要讲解怎么在 golang 中做 profile。
在 Golang 中,主要关注的应用运行情况主要包括以下几种:
CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据
Memory profile(Heap profile):报告程序的内存使用情况
Block profile:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈
Goroutine profile:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的
使用pprof
/usr/local/go/bin/go tool pprof http://localhost:9196/debug/pprof/block
使用web生成调用图
Fetching profile over HTTP from http://localhost:9196/debug/pprof/block
Saved profile in /home/zhanglei/pprof/pprof.___go_build_github_com_loggie_io_loggie_cmd_loggie.contentions.delay.001.pb.gz
File: ___go_build_github_com_loggie_io_loggie_cmd_loggie
Build ID: b489ffcb025421cb9a68d3fa14445ecc2072fbe2
Type: delay
Time: Jul 6, 2022 at 5:53am (PDT)
No samples were found with the default sample value type.
Try "sample_index" command to analyze different sample values.
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) web
failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH
安装graphviz
sudo apt-get install graphviz
使用web命令生成调用栈
Fetching profile over HTTP from http://localhost:9196/debug/pprof/profile
Saved profile in /home/zhanglei/pprof/pprof.___go_build_github_com_loggie_io_loggie_cmd_loggie.samples.cpu.001.pb.gz
File: ___go_build_github_com_loggie_io_loggie_cmd_loggie
Build ID: b489ffcb025421cb9a68d3fa14445ecc2072fbe2
Type: cpu
Time: Jul 6, 2022 at 5:56am (PDT)
Duration: 30.12s, Total samples = 11.86s (39.37%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) web
(pprof)
(pprof)
三、总结
性能调优一定要遵循摩尔定律,首先解决主要瓶颈点,一步步深入让性能达到要求,而不是过度纠结细节