golang graphviz与pprof

x_focus · · 3383 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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:

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

Or to look at the goroutine blocking profile:

go tool pprof 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文件是可以在浏览器下看的。像这个样子:

Image

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




有疑问加站长微信联系

本文来自:CSDN博客

感谢作者:x_focus

查看原文:golang graphviz与pprof

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:812540095

3383 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传