前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >go 性能调优

go 性能调优

原创
作者头像
良人王
修改2020-03-18 10:30:23
4220
修改2020-03-18 10:30:23
举报
文章被收录于专栏:技术运维

1 概要

go 自带profile工具,包括对内存,cpu等监控,可以根据需要对程序进行调优。 如果需要生成直观图表,则配合安装graphviz工具,在centos下,直接yum install graphviz即可

2 生成profile

生成profile,有两种方法,分别介绍如下

2.1 net/http/pprof

如果应用是web应用,则直接引入包,这种方法比较简单,在网页上可以直接查看对应的profile,代码如下

代码语言:javascript
复制
package main

import (
    "net/http"
    _ "net/http/pprof"
)

func main() {
    log.Fatal(http.ListenAndServe("0.0.0.0:9999", nil))
}

则在网页上http://127.0.0.1:9999/debug/pprof 查看整体信息

2.2 runtime/pprof

如果应用和web无关,则需要将对应的监控手动写到代码里,以CPU调优为例,代码如下:

test.go

代码语言:javascript
复制
package main

import (
	"fmt"
	"os"
	"runtime/pprof"
	"time"
)

func main() {
	cpuf, errPro := os.Create("./cpu_profile")
	if errPro != nil {
		fmt.Println("create fail")
		return
	}

	perr := pprof.StartCPUProfile(cpuf)
	if perr != nil{
		fmt.Println("start fail")
		return
	}

	defer pprof.StopCPUProfile()


	fmt.Println("time", time.Now().Unix())
	sum := int64(0)
	i := int64(0)
	for i = 0; i < 10000000000; i= i+1 {
		sum += i
	}
	fmt.Println("time", time.Now().Unix())
	fmt.Println("sum", sum)
}

则会对cpu的性能消耗进行记录。编译代码生成可执行文件test,执行test,则会在当前目录下生成cpu_profile文件

3. 分析

在终端执行 go tool pprof test cpu_profile ,如下:

代码语言:javascript
复制
git:(master) ✗ go tool pprof test cpu_profile 
File: test
Type: cpu
Time: Mar 16, 2020 at 12:41am (CST)
Duration: 14.61s, Total samples = 14.44s (98.82%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)    

会进入到pprof交互下,这里介绍两个命令,top,list

输入top命令,则可以看到消耗cpu性能最高的函数调用,如下:

代码语言:javascript
复制
(pprof) top                           
Showing nodes accounting for 14.43s, 99.93% of 14.44s total
Dropped 1 node (cum <= 0.07s)
      flat  flat%   sum%        cum   cum%
    14.43s 99.93% 99.93%     14.43s 99.93%  main.main
         0     0% 99.93%     14.43s 99.93%  runtime.main

接着执行list 命令,后接函数名,如下:

代码语言:javascript
复制
(pprof) list main.main
Total: 14.44s
ROUTINE ======================== main.main in /data/kadinwang/earth-agent-gse/test/test.go
    14.43s     14.43s (flat, cum) 99.93% of Total
         .          .     24:
         .          .     25:
         .          .     26:   fmt.Println("time", time.Now().Unix())
         .          .     27:   sum := int64(0)
         .          .     28:   i := int64(0)
    11.63s     11.63s     29:   for i = 0; i < 40000000000; i= i+1 {
     2.80s      2.80s     30:           sum += i
         .          .     31:   }
         .          .     32:   fmt.Println("time", time.Now().Unix())
         .          .     33:   fmt.Println("sum", sum)
         .          .     34:}

可以发现 执行最耗时的地方是for语句,这个例子里已经不太需要优化了,这个例子是为了看出程序cpu的占用都用在代码的哪里,可以方便开发者来进行调优

最后一个是生成图表,可以生成函数调用关系的图,方便开发者直观看,这块需要有graphviz,如下:

代码语言:javascript
复制
➜  test git:(master) ✗ ls
README.md  cpu_profile  install.sh  test  test.go  test_kadin
➜  test git:(master) ✗ go tool pprof -svg test cpu_profile >cpu.svg
➜  test git:(master) ✗ ls
README.md  cpu.svg  cpu_profile  install.sh  test  test.go  test_kadin
➜  test git:(master) ✗ 

这样会在当前目录下生成cpu.svg,可以直接用浏览器将其打开查看,如下:

当然这个测试用例比较简单,所以生成的函数调用也确实比较简单。

4 总结

使用go 自带的profile工具,还是很方便对程序的性能进行优化。memory优化,不再赘述

在交互命令下,可以输入help,查看更多的命令支持。有兴趣可以自行研究。

没必须对程序进行过早优化

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 概要
  • 2 生成profile
    • 2.1 net/http/pprof
      • 2.2 runtime/pprof
      • 3. 分析
      • 4 总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档