kaisawind's blog
  • 关于
  • 所有帖子

golang pprof简单使用 - Tue, Jul 14, 2020

golang pprof性能分析

1. 概述

pprof是go语言自带的性能监控和分析工具。

2. 使用方法

2.1 默认使用方法

package main

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

func main() {
	go func() {
		_ = http.ListenAndServe(":6060", nil)
	}()

	var timer *time.Timer
	for {
		select {
		case <-time.Tick(1 * time.Second):
			if timer != nil {
				timer.Stop()
				timer = nil
			}
			timer = time.AfterFunc(2*time.Second, timeout)
		}
	}
}

func timeout() {
	fmt.Println("timeout")
}

2.2 自定义mux方法

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "math"

    "net/http"
)
import "net/http/pprof"

func AttachProfiler(router *mux.Router) {
    router.HandleFunc("/debug/pprof/", pprof.Index)
    router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
    router.HandleFunc("/debug/pprof/profile", pprof.Profile)
    router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
}

func SayHello(w http.ResponseWriter, r *http.Request) {
    for i := 0; i < 1000000; i++ {
        math.Pow(36, 89)
    }
    fmt.Fprint(w, "Hello!")
}

func main() {
    r := mux.NewRouter()
    AttachProfiler(r)
    r.HandleFunc("/hello", SayHello)
    http.ListenAndServe(":6060", r)
}

3. URL说明

参数PATHENCN
allocs/debug/pprof/allocs?debug=1A sampling of all past memory allocations过去内存分配情况的采样信息
block/debug/pprof/block?debug=1Stack traces that led to blocking on synchronization primitives阻塞操作情况的采样信息
cmdline/debug/pprof/cmdlineThe command line invocation of the current program显示程序启动命令及参数
goroutine/debug/pprof/goroutine?debug=1Stack traces of all current goroutines当前所有协程的堆栈信息
heap/debug/pprof/heap?debug=1A sampling of memory allocations of live objects.活动对象的内存分配的样本
mutex/debug/pprof/mutex?debug=1Stack traces of holders of contended mutexes互斥锁情况的采样信息
profile/debug/pprof/profileCPU profile.CPU 占用情况的采样信息
threadcreate/debug/pprof/threadcreate?debug=1Stack traces that led to the creation of new OS threads系统线程创建情况的采样信息
trace/debug/pprof/traceA trace of execution of the current program.程序运行跟踪信息

4. 截图


辽ICP备2021007608号 | © 2025 | kaisawind

Facebook Twitter GitHub