Skip to content

Performance Profiling

Debug performance issues using Go pprof.

Alita Robot supports Go’s pprof profiling tool for diagnosing performance bottlenecks. This guide covers how to enable and use profiling endpoints.

Terminal window
# Enable pprof endpoints (development only!)
ENABLE_PPROF=true

When enabled, the following endpoints become available:

EndpointDescription
/debug/pprof/Index of available profiles
/debug/pprof/heapHeap memory profile
/debug/pprof/goroutineGoroutine stack trace
/debug/pprof/threadcreateThread creation profile
/debug/pprof/blockBlock (goroutine blocking) profile
/debug/pprof/mutexMutex contention profile

CPU profiling requires a separate request:

Terminal window
# Collect 30 seconds of CPU profile
curl -o cpu.pprof http://localhost:8080/debug/pprof/profile?seconds=30

Start the pprof interactive console:

Terminal window
go tool pprof http://localhost:8080/debug/pprof/heap

Common commands in pprof:

CommandDescription
topShow top functions by resource usage
webOpen visual graph in browser
list funcnameShow source for specific function
tracesPrint all sample traces
Terminal window
# Open web UI at http://localhost:8081
go tool pprof -http=:8081 http://localhost:8080/debug/pprof/heap

Use the web interface to explore the profile visually.

Terminal window
# Drop into interactive console
go tool pprof http://localhost:8080/debug/pprof/heap
# Then run commands like:
(pprof) top
(pprof) web
(pprof) list functionname
Terminal window
# Get goroutine dump in console mode
go tool pprof http://localhost:8080/debug/pprof/goroutine
# Check for goroutine leaks
(pprof) top
Terminal window
# Collect 30 seconds of CPU profile
# Note: The server has a 10s WriteTimeout - use shorter duration or profile externally
go tool pprof -seconds=30 http://localhost:8080/debug/pprof/profile
## Flame Graphs
Flame graphs provide a visual representation of CPU or memory usage.
### Option 1: go tool pprof (Recommended)
The simplest way to generate flame graphs:
```bash
# Generate SVG flame graph from heap profile
go tool pprof -svg -output=heap-flamegraph.svg http://localhost:8080/debug/pprof/heap
# Generate SVG flame graph from CPU profile (30 seconds)
go tool pprof -svg -output=cpu-flamegraph.svg http://localhost:8080/debug/pprof/profile?seconds=30
# Or open in browser directly
go tool pprof -http=:8081 http://localhost:8080/debug/pprof/heap

For more control, use Brendan Gregg’s FlameGraph tools:

Terminal window
# Clone the FlameGraph repository
git clone https://github.com/brendangregg/FlameGraph.git
cd FlameGraph
# Generate heap flame graph from pprof
# First, get the profile as raw protobuf
curl -s http://localhost:8080/debug/pprof/heap > heap.pb
# Convert to SVG using go tool pprof to export folded stacks
go tool pprof -proto -output=heap.folded ./your-binary heap.pb
# Generate flame graph
./flamegraph.pl heap.folded > heap-flamegraph.svg
  1. Collect heap profile during peak usage
  2. Look for objects that shouldn’t be retained
  3. Check for unbounded caches or slices
  1. Compare goroutine profiles over time
  2. Look for goroutines waiting on channels
  3. Check for missing context cancellations
  1. Collect CPU profile during spike
  2. Identify hot code paths
  3. Look for busy loops or excessive locking

For production monitoring without pprof:

  • Use Prometheus metrics for observability
  • Enable ENABLE_PERFORMANCE_MONITORING for auto-remediation
  • Monitor /metrics endpoint for custom metrics
  • Use external APM tools (Datadog, New Relic)
  • Ensure traffic is hitting the bot during collection
  • CPU profiles require active processing
  • Verify ENABLE_PPROF=true is set
  • Check bot is running and port is correct
  • Ensure firewall allows access to pprof port