go - pprof 配置文件与 julienschmidtrouter 和基准测试不分析处理程序

标签 go webserver benchmarking pprof

我正在尝试分析我编写的 Web 服务器,但我的 pprof 不包含有关处理程序函数的任何数据。
我正在使用 httprouter package由 julienschmidt 编写,并想简单地对我的一个处理程序进行基准测试,然后查看 pprof 配置文件。对于基准测试,我使用 go-wrk

我像这样设置我的网络服务器和 pprof:

// Configure the server
server := &http.Server{
    Addr:    ":4000",
    Handler: router,
}

go func() {
    log.Println(http.ListenAndServe(":6060", nil))
}()

// Start the server
err = server.ListenAndServe()
if err != nil {
    panic(err)
}

路由器是这样初始化的:

// Create the httprouter
router := httprouter.New()
// Register all handlers
router.GET("/entities/:type/map", h.UseHandler(&h.ApiGetEntitiesMapRequest{}, p))

我的处理程序看起来像这样:

func (req ApiGetEntitiesMapRequest) Handle(r *http.Request, hrp httprouter.Params, p Params) (interface{}, error) {
    test := make([]string, 0)
    for i := 0; i < 1000; i++ {
        test = append(test, "1")
        test = append(test, "2")
        // Ensure pprof has some time to collect its data
        time.Sleep(10)
    }
    return test, nil
}

这个处理程序只是一个测试,我动态地将很多元素附加到一个 slice 中。这样做的原因是,我想测试这些动态分配是否在 pprof 的堆配置文件中表示。

现在,我所做的是:

请求有效,我的基准测试也正确报告了所有内容。但是,当我在 pprof 终端中键入 png 时,我得到了这张图 enter image description here .

该图不包含有关我的处理程序和我在我的处理程序中进行的代价高昂的堆分配的任何信息。我做错了什么?

最佳答案

所以,我终于找到问题了!

如果您使用自定义多路复用器来处理您的请求,则需要将 pprof 处理程序注册到您的多路复用器,以便正确进行分析:

router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
router.Handler(http.MethodGet,"/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handler(http.MethodGet,"/debug/pprof/block", pprof.Handler("block"))

现在我得到了我想要的结果!

关于go - pprof 配置文件与 julienschmidtrouter 和基准测试不分析处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452471/

相关文章:

go - 用于检测 CGO 构建的预处理器标志?

testing - 测试 '-timeout 0' 未反射(reflect)在执行中

php - 离线更新数据库

performance - 提高二郎牛仔表现

node.js - 开源程序中的加密?

c - TLB 对 Cortex-A9 影响的测量

戈朗 : Unzip files in Go gets char encoding problems in the files names when file has been zipped in windows

multithreading - 戈朗 : why using goroutines to parallelize calls ends up being slower?

php - SQL 基准 : PHP ActiveRecord ORM vs. MySQL 与 CodeIgniter Active Record 与标准 PHP

c++ - 以纳秒为单位获取本地时间