Go hat hervorragende integrierte Profiling-Werkzeuge. So verwenden Sie sie.
pprof aktivieren¶
import _ “net/http/pprof” go func() { log.Println(http.ListenAndServe(“:6060”, nil)) }()
CPU Profiling¶
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
Memory¶
go tool pprof http://localhost:6060/debug/pprof/heap
Goroutines¶
curl http://localhost:6060/debug/pprof/goroutine?debug=1
Flame Graphs¶
go tool pprof -http=:8080 cpu.prof
Benchmarks¶
func BenchmarkMyFunc(b *testing.B) { for i := 0; i < b.N; i++ { MyFunc() } } go test -bench=. -benchmem
Optimierung¶
- sync.Pool für wiederholte Allokationen
- Slices vorab allozieren
- strings.Builder
- Gepufferte Channels
Zusammenfassung¶
pprof ist integriert und leistungsstark. CPU + Heap decken 90% der Probleme ab.