go性能调优1
来源:互联网 发布:java 字符串和日期比较 编辑:程序博客网 时间:2024/06/07 04:54
Introduction
The Go runtime has built in support for several types of profiling that can be used to inspect the performance of your programs. A common way to leverage this support is via the testing
package, but if you want to profile a full application it is sometimes complicated to configure the various profiling mechanisms correctly.
I wrote profile to scratch my own itch and create a simple way to profile an existing Go program without having to restructure it as a benchmark.
Installation
profile is go get
able so installation is a simple as
go get github.com/davecheney/profile
Usage
Enabling profiling in your application is as simple as one line at the top of your main
function
import "github.com/davecheney/profile"func main() { defer profile.Start(profile.CPUProfile).Stop() ...}
Options
What to profile is controlled by the *profile.Config
value passed to profile.Start
. A nil
*profile.Config
is the same as choosing all the defaults. By default no profiles are enabled.
In this more complicated example a *profile.Config
is constructed by hand which enables memory profiling, but disables the shutdown hook.
import "github.com/davecheney/profile"func main() { cfg := profile.Config { MemProfile: true, NoShutdownHook: true, // do not hook SIGINT } // p.Stop() must be called before the program exits to // ensure profiling information is written to disk. p := profile.Start(&cfg) ...}
Several convenience variables are provided for cpu, memory, and block (contention) profiling.
For more complex options, consult the documentation on the profile.Config
type. Enabling more than one profile may cause your results to be less reliable as profiling itself is not without overhead.
Example
To show profile in action, I modified cmd/godoc
following the instructions in the first example.
% godoc -http=:80802013/07/07 15:29:11 profile: cpu profiling enabled, /tmp/profile002803/cpu.pprof
In another window I visited http://localhost:8080
a few times to have some profiling data to record, then stopped godoc
.
^C2013/07/07 15:29:33 profile: caught interrupt, stopping profiles% go tool pprof $(which godoc) /tmp/profile002803/cpu.pprofWelcome to pprof! For help, type 'help'.(pprof) top10Total: 15 samples 2 13.3% 13.3% 2 13.3% go/scanner.(*Scanner).next 2 13.3% 26.7% 2 13.3% path.Clean 1 6.7% 33.3% 3 20.0% go/scanner.(*Scanner).Scan 1 6.7% 40.0% 1 6.7% main.hasPathPrefix 1 6.7% 46.7% 3 20.0% main.mountedFS.translate 1 6.7% 53.3% 1 6.7% path.Dir 1 6.7% 60.0% 1 6.7% path/filepath.(*lazybuf).append 1 6.7% 66.7% 1 6.7% runtime.findfunc 1 6.7% 73.3% 2 13.3% runtime.makeslice 1 6.7% 80.0% 2 13.3% runtime.mallocgc
Note In the example above we’re passing the godoc
binary and the profile produced by running that binary to go tool pprof
. When profiling your own code, you must pass your binary, and its profile to go tool pprof
otherwise the profile will not make sense.
go tool pprof server /var/folders/jr/hvhybrvn0j54rrn6mygq2ldh0000gn/T/profile121190984/cpu.pprof
- go性能调优1
- go性能调优2
- Golang性能调优(go-torch, go tool pprof)
- Go语言HTTP测试及程序性能调优
- Go性能优化技巧 1/10
- [Go语言]我的性能我做主(1)
- Go程序性能分析pprof
- Go高性能编程技巧
- Go高性能编程技巧
- 【golang】Go 性能优化技巧
- Go程序性能分析pprof
- GO,NODEJS,.NetCore性能对比
- 性能调优1
- Go和Python Web服务器性能对比
- 对Go+Nginx 进行性能测试
- Go语言 常见数据结构性能测试
- Go语言 类型断言性能测试
- GO性能测试:未进行任何优化
- 【每天学一点】JAVASE01 (1)
- Jenkins构建时提示:Bulid is already in progress 的解决办法
- 解决 recvfrom的 阻塞
- zTree获得选择节点的值以及反显选中
- oracle分区表详解
- go性能调优1
- 使用MySQLWorkBench绘制ER图
- mongo db 数据导出
- Linux下怎么安装JDK
- document.ready和window.onload的区别
- iOS9之UIStackView体验,无需任何约束,这才是真正的自动布局,快到不能呼吸
- linphone 编译
- Weka-Fliter类-选择属性变量[4]
- PHPStorm显示代码行号设置