Read pprof and runtime metrics
Read pprof and runtime metrics
A CPU profile samples executing stacks; a heap profile samples allocations and retained objects. The runtime/metrics package exposes named process metrics, including counters, gauges and histograms. A profile identifies likely hot paths; a metric describes a measured quantity over time. Neither replaces a representative workload.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "runtime/metrics" "testing")
func TestLesson(t *testing.T) { s := []metrics.Sample{{Name: "/gc/heap/allocs:bytes"}} metrics.Read(s) if s[0].Value.Kind() != metrics.KindUint64 { t.Fatal("unexpected metric kind") } first := s[0].Value.Uint64() metrics.Read(s) if s[0].Value.Uint64() < first { t.Fatal("counter decreased") } t.Log("allocation counter readable")}The allocation counter is cumulative, so compare deltas over equal observation intervals; do not interpret it as current heap size. Check the metric kind before reading a value. Discover available metrics with metrics.All. Re-reading samples can reuse histogram storage; copy data before retaining it across reads.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCapture a CPU and heap profile
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "testing")
var result uint64
func work(n uint64) uint64 { var sum uint64 for i := uint64(0); i < n; i++ { sum += i * i } return sum}func TestLesson(t *testing.T) { if work(4) != 14 { t.Fatal("work") }}func BenchmarkWork(b *testing.B) { for i := 0; i < b.N; i++ { result = work(10000) }}Run go test -run=^$ -bench=BenchmarkWork -benchtime=1s -cpuprofile=cpu.prof -memprofile=heap.prof then go tool pprof -top cpu.prof and go tool pprof -top -alloc_space heap.prof. In pprof, flat is the cost attributed directly to a function and cum includes descendants. Heap inuse_space concerns retained sampled bytes; alloc_space concerns allocation traffic. Samples, especially in tiny workloads, can be sparse; do not infer a hot spot from an empty profile. Keep profiles private because they can expose implementation details.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Read /sched/goroutines:goroutines, check its kind and positive count; explain why it is a gauge rather than a cumulative count. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Performance engineering 02: Read pprof and runtime metrics
Check the lesson objective before moving on.
Which profile view shows allocation traffic?
What does cumulative CPU cost include?