Golang的pprof的使用心得(CPU,Heap)
包_包 · · 3598 次点击 · · 开始浏览参照的是https://github.com/caibirdme/hand-to-hand-optimize-go 这个文章
写一段代码测试
首先自己写一段demo
里面负责2件事
doSomeThingOne
genSomeBytes
运行这个程序go run main.go
安装wrk
To install thewrk,you need only:
git clonehttps://github.com/wg/wrk.git
cd wrk
make
wrk relies on the openssl and luajit, learn more from its github page
Our demo is listening on the port9876,so let's generate some requests for that.
./wrk -c400 -t8 -d5m http://localhost:9876/test
-c400means we have 400 connections to keep open
-t8means we use 8 threads to build requests
-d5mmeans the duration of the test will last for 5 minutes
用这段命令来压服务器
观看结果
Our server is very busy now and we can see some information via browser. Inputlocalhost:9876/debug/pprofyou will see:
然后用命令进入
在这里能看见各种方法的运行时间
但是很不直观对不对
所以我们安装Graphviz 在mac下
brew install graphviz
之后再这个(pprof)里面输入web
会生产一个svg文件
用浏览器打开我们就会看到
很显然gensomebytes里面的math方法最消耗时间。这个就是我们优化的对象
内存怎么看呢?
其实也很方便在
localhost:9876/debug/pprof/profile改成
localhost:9876/debug/pprof/heap
后面的结果一样。。和cpu一样可以看到那个heap占用了大量的内存到时候优化吧
https://studygolang.com/articles/1720 这个文章里面的第一个方法就可以做测试内存占用的.
有空试试把
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
参照的是https://github.com/caibirdme/hand-to-hand-optimize-go 这个文章
写一段代码测试
首先自己写一段demo
里面负责2件事
doSomeThingOne
genSomeBytes
运行这个程序go run main.go
安装wrk
To install thewrk,you need only:
git clonehttps://github.com/wg/wrk.git
cd wrk
make
wrk relies on the openssl and luajit, learn more from its github page
Our demo is listening on the port9876,so let's generate some requests for that.
./wrk -c400 -t8 -d5m http://localhost:9876/test
-c400means we have 400 connections to keep open
-t8means we use 8 threads to build requests
-d5mmeans the duration of the test will last for 5 minutes
用这段命令来压服务器
观看结果
Our server is very busy now and we can see some information via browser. Inputlocalhost:9876/debug/pprofyou will see:
然后用命令进入
在这里能看见各种方法的运行时间
但是很不直观对不对
所以我们安装Graphviz 在mac下
brew install graphviz
之后再这个(pprof)里面输入web
会生产一个svg文件
用浏览器打开我们就会看到
很显然gensomebytes里面的math方法最消耗时间。这个就是我们优化的对象
内存怎么看呢?
其实也很方便在
localhost:9876/debug/pprof/profile改成
localhost:9876/debug/pprof/heap
后面的结果一样。。和cpu一样可以看到那个heap占用了大量的内存到时候优化吧
https://studygolang.com/articles/1720 这个文章里面的第一个方法就可以做测试内存占用的.
有空试试把