dongzhang1864 2017-11-23 09:54
浏览 70
已采纳

带有julienschmidtrouter和基准的pprof配置文件未分析处理程序

I am trying to profile my web server I wrote, but my pprof does not contain any data about the handler func.
I am using the httprouter package by julienschmidt, and want to simply benchmark one of my handlers and see the pprof profile for that. For the benchmarking, I am using go-wrk

I set up my web server and pprof like this:

// 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)
}

The router is initialized like this:

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

And my handler looks like this:

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
}

This handler is just a test, where I dynamically append a lot of elements to a slice. The reason for that is, I wanted to test whether these dynamic allocations are represented in the heap profile of pprof.

Now, what I did was:

The request works and my benchmark also reports everything correctly. However, when I type png in the pprof terminal, I get this graphenter image description here.

The graph does not contain any information about my handler and the costly heap allocations I did in my handler. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dougengqiu8031 2017-12-02 10:28
    关注

    So, I finally found the problem!

    If you are using a custom mux for handling your requests, you need to register the pprof handler to your mux, in order to get the profiling right:

    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"))
    

    Now I got my desired result!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探