pprof发现go程序性能

修改代码加入pprof

1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import (
_ "net/http/pprof"
)
func main() {
// we need a webserver to get the pprof webserver
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// your code
}

生成性能报表

  1. 运行程序

  2. 生成内存性能报表

    1
    go tool pprof ./apply_idem http://localhost:6060/debug/pprof/heap
  3. 生成CPU性能报表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    go tool pprof ./apply_idem http://localhost:6060/debug/pprof/profile
    Fetching profile over HTTP from http://localhost:6060/debug/pprof/profile
    Saved profile in /Users/loushuai/pprof/pprof.apply_idem.samples.cpu.001.pb.gz
    File: apply_idem
    Type: cpu
    Time: Apr 10, 2018 at 3:33pm (CST)
    Duration: 30.02s, Total samples = 2.55s ( 8.49%)
    Entering interactive mode (type "help" for commands, "o" for options)
    (pprof) list exec_on_source_for_one_row
    Total: 2.55s
    ROUTINE ======================== main.exec_on_source_for_one_row in /Users/loushuai/dbscale/apply_script_go/apply_idem.go
    0 350ms (flat, cum) 13.73% of Total
    . . 225: return time_region_sql_snippet, time_region_sql_snippet_with_t
    . . 226:}
    . . 227:
    . . 228:func exec_on_source_for_one_row(query_sql string, column_count int) []string {
    . . 229: row := make([]string, column_count)
    . 320ms 230: rows := exec_on_source(query_sql)
    . . 231: defer rows.Close()
    . . 232: rawResult_onerow := make([][]byte, column_count)
    . . 233: dest_onerow := make([]interface{}, column_count)
    . . 234: for i, _ := range rawResult_onerow {
    . . 235: dest_onerow[i] = &rawResult_onerow[i]
    . . 236: }
    . . 237: for rows.Next() {
    . . 238: err := rows.Scan(dest_onerow...)
    . . 239: if err != nil {
    . . 240: fatal_error(err.Error())
    . . 241: }
    . . 242: for i, raw := range rawResult_onerow {
    . . 243: if raw == nil {
    . . 244: row[i] = "\\N"
    . . 245: } else {
    . 10ms 246: row[i] = string(raw)
    . . 247: }
    . . 248: }
    . . 249: break
    . . 250: }
    . 20ms 251: return row
    . . 252:}
    . . 253:
    . . 254:func exec_on_target(query_sql string) {
    . . 255: retry_count := 10
    . . 256: if Print_msg {
  4. 生成flumegraph

1
go-torch -u http://127.0.0.1:6060/ --seconds 30

参考文章

  1. 使用 pprof 和火焰图调试 golang 应用
  2. net/http/pprof/
  3. runtime/pprof/

本文标题:pprof发现go程序性能

文章作者:Louis

发布时间:2018年04月10日 - 08:04

最后更新:2018年04月10日 - 16:04

原始链接:/2018/04/10/pprof/

许可协议: Louis-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。