Lab 02: Profiling with pprof
Overview
Step 1: CPU Profiling with runtime/pprof
runtime/pprof// cpu_profile.go
package main
import (
"os"
"runtime/pprof"
"math"
"fmt"
)
func expensiveComputation(n int) float64 {
result := 0.0
for i := 0; i < n; i++ {
result += math.Sqrt(float64(i)) * math.Sin(float64(i))
}
return result
}
func main() {
// Start CPU profile
f, _ := os.Create("cpu.prof")
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
// Do work
result := expensiveComputation(10_000_000)
fmt.Printf("Result: %.4f\n", result)
}Step 2: Heap Profiling
Step 3: HTTP pprof Endpoint
Step 4: Benchmarks with Profiling
Step 5: Goroutine Profile
Step 6: Allocs Profile
Step 7: go tool pprof Interactive Mode
go tool pprof Interactive ModeStep 8: Capstone — Full Profiling Workflow
Summary
Tool
Command
Purpose
Last updated
