Golang的pprof的使用心得(CPU,Heap)

参照的是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

Generating requests

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 这个文章里面的第一个方法就可以做测试内存占用的.

有空试试把

PS:最近玩了一个分布式的小程序.打算对这个程序进行性能的监控和报警..最后大概搞成了一个这样的方案

分布式程序 A B C D 4个进程在服务器. 监控程序E 打包程序F

写一个监控程序定时监控这4个进程的CPU 内存(搞成配置文件)

达到性能瓶颈(例如 90%CPU 内存剩下10%)  E用shell触发打包程序F把pprof等信息打包.并发送邮件

给配置者.

先就这么多了.这也算是对上面的篇章的一个总结吧。加油!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容