亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 go lang 中分析 http 處理程序

在 go lang 中分析 http 處理程序

Go
米脂 2021-12-13 11:05:15
我正在嘗試分析用 go 編寫的 http 處理程序。在每個 http 請求上從 S3 下載一個圖像,調整它的大小/裁剪它并寫入它作為響應。我已經點擊了這個鏈接,并嘗試使用簡單的方法和困難的方法來分析我提到的代碼?,F在,當我使用代碼中提到的以下行時。defer profile.Start(profile.CPUProfile).Stop() 它不會在/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()    ...}此外,當我使用defer profile.Start(profile.CPUProfile).Stop()里面的行時serveHTTP:func serveHTTP(w http.ResponseWriter, r *http.Request) {    defer profile.Start(profile.CPUProfile).Stop()    ......}它在/tmp/profile[some number]文件夾中創建多個文件。所以,第一個問題是為什么它沒有寫入文件,其次不應該放在里面,serveHTTP method因為服務器只會啟動一次。因此main()將被調用一次 wheresserveHTTP將在每個請求上被調用。
查看完整描述

2 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

要在運行時分析 http 服務器,您可以使用該net/http/pprof包。

只需添加

import _ "net/http/pprof"

導入并http://localhost:8081/debug/pprof/在瀏覽器中打開。


查看完整回答
反對 回復 2021-12-13
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

首先使用import "net/http/pprof" NOT import _ "net/http/pprof。后來pprof在下面的路由中沒有識別出來。


我使用的是默認的 serveMux/多路復用器。但是后來我創建了自己的,因為人們建議它具有性能影響。


myMux := http.NewServeMux()

然后添加請求的路由


myMux.HandleFunc("/", serveHTTP)

此外,我還添加了用于完成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)

所以,最終的代碼是:


導入“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)

    }       


}


查看完整回答
反對 回復 2021-12-13
  • 2 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號