Middle
Kuidas integreerida standardne profiiler oma Go rakendusse?
sobes.tech AI
Vastus AI-lt
Pakett net/http/pprof nodrošina HTTP apstrādātājus profilēšanas datiem.
Importējiet to:
import (
"net/http"
_ "net/http/pprof" // Svarīgi izmantot '_' lai inicializētu side effect
)
Sāciet HTTP serveri atsevišķā gorutīnā:
go func() {
// Servera palaišana lokālajā adresē un noklusējuma pprof portā
if err := http.ListenAndServe("localhost:6060", nil); err != nil {
// Servera palaišanas kļūdas apstrāde, piemēram, žurnāls
// log.Printf("pprof server failed: %v", err)
}
}()
Tagad jūs varat piekļūt profilēšanas datiem adresē http://localhost:6060/debug/pprof/.
Galvenie pieejamie endpointi:
/debug/pprof/: Galvenā lapa ar pieejamo profilu sarakstu./debug/pprof/heap: Atkritumu kolekcijas profils./debug/pprof/goroutine: Aktīvo gorutīnu profils./debug/pprof/block: Bloķēšanas profils./debug/pprof/threadcreate: OS pavedienu izveides profils./debug/pprof/cmdline: Lietojumprogrammas komandrinda./debug/pprof/profile: CPU profils (noklusējuma laikā 30 sekundes)./debug/pprof/trace: Izpildes trases profils (noklusējuma laikā 1 sekunde).
Lai analizētu CPU, atkritumu kolekcijas un bloķēšanas profilus, parasti tiek izmantota utilīta go tool pprof:
go tool pprof http://localhost:6060/debug/pprof/profile # CPU profils 30 sekundes
go tool pprof http://localhost:6060/debug/pprof/heap # Atkritumu kolekcijas profils
Lai analizētu izpildes trases, tiek izmantota utilīta go tool trace:
go tool trace http://localhost:6060/debug/pprof/trace
Svarīgi: Atcerieties par drošību un neatveriet pprof portu ārpusē bez autentifikācijas ražošanas vidē.