golang graphviz与pprof

前端之家收集整理的这篇文章主要介绍了golang graphviz与pprof前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sudo port install graphviz

fuyundeMacBook-Pro:~ fuyun$ sudo port install graphviz

Warning: The Xcode Command Line Tools don't appear to be installed; most ports will likely fail to build.

Warning: Install them by running `xcode-select --install'.

---> Computing dependencies for graphviz

---> Fetching archive for graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://mse.uk.packages.macports.org/sites/packages.macports.org/graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://packages.macports.org/graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://lil.fr.packages.macports.org/graphviz

---> Fetching distfiles for graphviz

---> Verifying checksums for graphviz

---> Extracting graphviz

---> Configuring graphviz

---> Building graphviz

---> Staging graphviz into destroot

---> Installing graphviz @2.38.0_0+pangocairo+x11

---> Activating graphviz @2.38.0_0+pangocairo+x11

---> Cleaning graphviz

---> Updating database of binaries

---> Scanning binaries for linking errors

---> No broken files found.


终端运行安装xcode-select至完毕

xcode-select --install


清除之前未安装成功的graphviz

sudo port uninstall graphviz


重新安装graphviz

fuyundeMacBook-Pro:~ fuyun$ sudo port install graphviz

---> Computing dependencies for graphviz

---> Fetching archive for graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://mse.uk.packages.macports.org/sites/packages.macports.org/graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://packages.macports.org/graphviz

---> Attempting to fetch graphviz-2.38.0_0+pangocairo+x11.darwin_14.x86_64.tbz2 from http://lil.fr.packages.macports.org/graphviz

---> Fetching distfiles for graphviz

---> Verifying checksums for graphviz

---> Extracting graphviz

---> Configuring graphviz

---> Building graphviz

---> Staging graphviz into destroot

---> Installing graphviz @2.38.0_0+pangocairo+x11

---> Activating graphviz @2.38.0_0+pangocairo+x11

---> Cleaning graphviz

---> Updating database of binaries

---> Scanning binaries for linking errors

---> No broken files found.


运行 go tool pprof 30秒等待工具

fuyundeMacBook-Pro:~ fuyun$ go tool pprof http://localhost:8080/debug/pprof/profile

Fetching profile from http://localhost:8080/debug/pprof/profile

Please wait... (30s)

Saved profile in /Users/fuyun/pprof/pprof.localhost:8080.samples.cpu.002.pb.gz

Entering interactive mode (type "help" for commands)


在这30秒中新的终端进行测试并发

ab -n 10000 -c 200 http://localhost:8080/storemanager/goods?telphone=18200566251


This is ApacheBench,Version 2.3 <$Revision: 1554214 $>

Copyright 1996 Adam Twiss,Zeus Technology Ltd,http://www.zeustech.net/

Licensed to The Apache Software Foundation,http://www.apache.org/


Benchmarking localhost (be patient)

Completed 1000 requests

Completed 2000 requests

Completed 3000 requests

Completed 4000 requests

Completed 5000 requests

Completed 6000 requests

Completed 7000 requests

Completed 8000 requests

Completed 9000 requests

Completed 10000 requests

Finished 10000 requests

Server Software:

Server Hostname: localhost

Server Port: 8080


Document Path: /storemanager/goods?telphone=18200566251

Document Length: 0 bytes


Concurrency Level: 200

Time taken for tests: 9.711 seconds

Complete requests: 10000

Failed requests: 9793

(Connect: 0,Receive: 0,Length: 9793,Exceptions: 0)

Total transferred: 21197939 bytes

HTML transferred: 19960000 bytes

Requests per second: 1029.74 [#/sec] (mean)

Time per request: 194.223 [ms] (mean)

Time per request: 0.971 [ms] (mean,across all concurrent requests)

Transfer rate: 2131.68 [Kbytes/sec] received


Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 0 0.8 0 9

Processing: 90 192 24.7 191 264

Waiting: 2 192 24.7 191 264

Total: 99 192 24.6 191 268


Percentage of the requests served within a certain time (ms)

50% 191

66% 203

75% 210

80% 214

90% 226

95% 233

98% 246

99% 255

100% 268 (longest request)


获取样本后在pprof命令提示符下可以建入web命令,如果默认没有浏览器打开,选择SVG文件COMMAND+I 选择打开方式挑一个浏览器,单击全部更改。

(pprof) web

(pprof)


注意该方式在WEB中,如果你使用的标准servermux则只需要包含import _ "net/http/pprof" 就行,如果自定义的路由需要使用pprof相应的函数输出并且附加debug的参数,这样浏览器显示的就不是字节方式了。

debug参数用于开启附加的输出。如果debug=0,只会打印pprof所需要的十六进制地址;如果debug=1,会将地址翻译为函数名和行号并添加注释,以便让程序员无需工具阅读分析报告。

预定义Profile为其它debug值赋予了含义。例如,当打印“Go程”的分析报告时,debug=2意为:由于不可恢复的恐慌而濒临崩溃时,使用与Go程序相同的格式打印Go程的堆栈信息。


Then use the pprof tool to look at the heap profile:

go tool pprof http://localhost:8080/debug/pprof/heap

Or to look at a 30-second cpu profile:

http://localhost:8080/debug/pprof/profile

Or to look at the goroutine blocking profile:

http://localhost:8080/debug/pprof/block

具体可参考:http://blog.golang.org/profiling-go-programs
	   https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md
 
以下为转载为 http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html 

应用程序

如果你的go程序只是一个应用程序,比如计算fabonacci数列,那么你就不能使用net/http/pprof包了,你就需要使用到runtime/pprof。具体做法就是用到pprof.StartcpuProfile和pprof.StopcpuProfile。比如下面的例子:

var cpuprofile = flag.String("cpuprofile","","write cpu profile to file")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f,err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartcpuProfile(f)
        defer pprof.StopcpuProfile()
    }

运行程序的时候加一个--cpuprofile参数,比如fabonacci --cpuprofile=fabonacci.prof

这样程序运行的时候的cpu信息就会记录到XXX.prof中了。

下一步就可以使用这个prof信息做出性能分析图了(需要安装graphviz)。

使用go tool pprof (应用程序) (应用程序的prof文件

进入到pprof,使用web命令就会在/tmp下生成svg文件,svg文件是可以在浏览器下看的。像这个样子:

如果你的程序非常简单,比如只有println一个语句,你用pprof.StartcpuProfile是打印不出任何东西的。

猜你在找的Go相关文章