go tool 学习笔记

https://www.youtube.com/watch?v=uBjoTxosSys
https://github.com/campoy/go-tooling-workshop

godoc

 godoc -http=:6063

在命令行中执行该命令后,打开浏览器输入 http://localhost:6063/

go tool 学习笔记_第1张图片
2017-09-08 13:20:30屏幕截图.png

输入http://localhost:6063/pkg/

go tool 学习笔记_第2张图片
2017-09-08 13:20:48屏幕截图.png

pprof

go test -bench . -cpuprofile=cpu.prof

在当前目录下会生成一个cpu.prof的文件和一个 $PKG.test 的二进制文件。
例如:我们有一个main.go文件和一个main_test.go的测试文件,在test目录下,那么生成就会生成
cpu.prof和test.test

go tool pprof test.test cpu.prof

进入如下界面

Entering interactive mode (type "help" for commands)
(pprof) 

我们要查看函数main.go中函数handler的执行情况

Entering interactive mode (type "help" for commands)
(pprof) list handler
Total: 1.44s
ROUTINE ======================== _/home/*********/test.handler in /home/********/test/main.go
         0      540ms (flat, cum) 37.50% of Total
         .          .     18:
         .          .     19:func handler(w http.ResponseWriter, r *http.Request) {
         .          .     20:
         .          .     21:   path := r.URL.Path
         .          .     22:   // fmt.Println(path)
         .      170ms     23:   match := re.FindStringSubmatch(path)
         .          .     24:   if match != nil {
         .      370ms     25:       fmt.Fprintf(w, "hello %s\n", match[1])
         .          .     26:   } else {
         .          .     27:       fmt.Fprintf(w, "helo2")
         .          .     28:
         .          .     29:   }
         .          .     30:   return
(pprof) 

火焰图

项目中首先引入包,直接让服务器生成profile

    _ "net/http/pprof"

获取火焰脚本

$ cd $GOPATH/src/github.com/uber/go-torch
$ git clone https://github.com/brendangregg/FlameGraph.git

注意一定要在FlameGraph下载所在目录下执行 go-torch命令,否则会报错

ctory. You can download the script at https://github.com/brendangregg/FlameGraph. These scripts should be added to your PATH or in the directory where go-torch is executed. Alternatively, you can run go-torch with the --raw flag.

-u指定要测试的服务地址

go-torch -u http://my-service:port/

同时另起一个终端执行go-wrk命令

go-wrk -d 30 http://localhost:port

go-wrk测试样例:

ubuntu@ubuntu:~$ go-wrk -d 30 http://localhost:29993/[email protected]
Running 30s test @ http://localhost:29993/[email protected]
  10 goroutine(s) running concurrently
1709639 requests in 29.241100615s, 180.98MB read
Requests/sec:       58466.99
Transfer/sec:       6.19MB
Avg Req Time:       171.036µs
Fastest Request:    28.856µs
Slowest Request:    40.816523ms
Number of Errors:   0

go-torch生成svg文件,可以拖到谷歌浏览器上打开

ubuntu@ubuntu:~/pkg/src/github.com/uber/go-torch$ go-torch  http://localhost:29993/debug/pprof/profile
INFO[14:18:49] Run pprof command: go tool pprof -raw -seconds 30 http://localhost:29993/debug/pprof/profile
INFO[14:19:19] Writing svg to torch.svg

可以看到go-torch 其实首先执行了

go tool pprof 

命令

你可能感兴趣的:(go tool 学习笔记)