我有一个用围棋编写的相对较大的Web应用程序,它使用Gorilla's mux进行路由.我最近意识到我的Web应用程序相当慢,我想分析一下Web应用程序.

看过之后,似乎net/http/pprof美元才是我需要的.但我不能让它在mux下运行;即使是最琐碎的Web应用程序也不行.

有人知道怎么做吗?

下面是一个不起作用的琐碎代码的示例(即在/debug没有任何服务).

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "math"
    "net/http"
)
import _ "net/http/pprof"

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()
    r.HandleFunc("/hello", SayHello)
    http.ListenAndServe(":6060", r)
}

推荐答案

抱歉问这个问题.答案在pprof的init()函数中.只需将pprof中的4个功能添加到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)
}

Go相关问答推荐

golang 的条件储存库

使用ciph.AEAD.Seal()查看内存使用情况

如何在链接中写入链接

这是go语言gin提供的关于TypeEngine和RouterGroup的问题

在 Windows 11 上运行 go mod tidy 时的 gitlab 权限问题

同一文件上的多个 Arrow CSV 阅读器返回 null

将 firestoreinteger_value转换为整数

gopacket:IP-in-IP 数据包上的解码层

使用 LINQ 对内部数组进行排序

Golang crypto/rand 线程安全吗?

golang yaml 马歇尔网址

如何仅提取时间作为持续时间

通过 golang 中的 gremlin-go 库嵌入 gremlin 服务器

使用 package`regexp` 查找 Golang 中的所有 mactch 子字符串,但得到意外结果

在删除级联时无法在 Gorm 中按预期工作

Grafana/Prometheus 将多个 ip 可视化为查询

Golang - 使用正则表达式提取链接

Go 泛型是否与 LINQ to Objects 等效?

是否可以动态加载 Go 代码?

Go http标准库中的内存泄漏?