Nar*_*esh 2 profiling go pprof
我正在尝试分析用 go 编写的 http 处理程序。每个 http 请求都会从 S3 下载图像,调整其大小/裁剪它并将其写入响应中。
我已点击此链接并尝试使用简单方法和困难方法来分析我的代码。现在,当我使用代码中提到的以下行时。
defer profile.Start(profile.CPUProfile).Stop()
Run Code Online (Sandbox Code Playgroud)
它不会在/tmp/profie[some number]/cpu.pprof文件中写入任何内容
func main() {
defer profile.Start(profile.CPUProfile).Stop()
if err := http.ListenAndServe(":8081", http.HandlerFunc(serveHTTP)); err != nil {
logFatal("Error when starting or running http server: %v", err)
}
}
func serveHTTP(w http.ResponseWriter, r *http.Request) {
keyName := r.URL.Path[1:]
s3Client := s3.New(session.New(), &aws.Config{Region: aws.String(region)})
params := &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
}
mw := imagick.NewMagickWand()
defer mw.Destroy()
...
}
Run Code Online (Sandbox Code Playgroud)
此外,当我使用类似的defer profile.Start(profile.CPUProfile).Stop()行时serveHTTP:
func serveHTTP(w http.ResponseWriter, r *http.Request) {
defer profile.Start(profile.CPUProfile).Stop()
......
}
Run Code Online (Sandbox Code Playgroud)
它在文件夹中创建多个文件/tmp/profile[some number]。所以,第一个问题是为什么它不写入文件,其次它不应该放在文件中,serveHTTP method因为服务器只会启动一次。因此main()将被调用一次,serveHTTP每个请求都会被调用。
第1部分
. 124: s3Client := s3.New(session.New(), &aws.Config{Region: aws.String(region)})
. . 125: params := &s3.GetObjectInput{
. . 126: Bucket: aws.String(masterBucketName),
. . 127: Key: aws.String(keyName),
. 32.01kB 128: }
. . 129:
. . 130: mw := imagick.NewMagickWand()
. . 131: defer mw.Destroy()
. . 132:
. . 133: out, err := s3Client.GetObject(params)
. . 134:
. . 135: if strings.EqualFold(keyName[strings.LastIndex(keyName,".")+1:len(keyName)], "gif") {
. . 136:
. 40.11kB 137: blobGiff, err := ioutil.ReadAll(out.Body)
. . 138: w.Header().Set("Content-Type", "image/gif")
. . 139: w.Header().Set("Cache-Control", "max-age: 604800, public")
. . 140: w.Header().Set("Last-Modified", time.Now().Format(http.TimeFormat))
. . 141: w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(http.TimeFormat))
. . 142:
Run Code Online (Sandbox Code Playgroud)
第2部分 :
else {
. . 167: img, err := ioutil.ReadAll(out.Body)
. . 168: if err != nil {
. . 169:
. . 170: w.WriteHeader(http.StatusNotFound)
. 1.56MB 171: return
. . 172: }
Run Code Online (Sandbox Code Playgroud)
另外,上面两部分中的第 128、137 和 171 行存在内存泄漏,对吗?另外,我没有找到任何关闭/销毁s3Clientand blobGiff(byte []) 的选项。
要在 http 服务器运行时对其进行分析,您可以使用该net/http/pprof包。
只需添加
import _ "net/http/pprof"
Run Code Online (Sandbox Code Playgroud)
到您的导入并http://localhost:8081/debug/pprof/在浏览器中打开。
首先使用import "net/http/pprof" NOT import _ "net/http/pprof。后来就无法识别pprof下面的路由了。
我使用默认的serveMux/多路复用器。但后来我创建了自己的,因为人们建议它对性能有影响。
myMux := http.NewServeMux()
Run Code Online (Sandbox Code Playgroud)
然后添加请求的路由
myMux.HandleFunc("/", serveHTTP)
Run Code Online (Sandbox Code Playgroud)
此外,我还添加了完成http://localhost:8081/debug/pprof/工作的路线
myMux.HandleFunc("/debug/pprof/", pprof.Index)
myMux.HandleFunc("/debug/pprof/{action}", pprof.Index)
myMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
Run Code Online (Sandbox Code Playgroud)
所以,最终代码是:
导入“net/http/pprof
func main() {
myMux := http.NewServeMux()
myMux.HandleFunc("/", serveHTTP)
myMux.HandleFunc("/debug/pprof/", pprof.Index)
myMux.HandleFunc("/debug/pprof/{action}", pprof.Index)
myMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
if err := http.ListenAndServe(":8081", myMux); err != nil {
logFatal("Error when starting or running http server: %v", err)
}
}
Run Code Online (Sandbox Code Playgroud)