Read mutex and block profiles
Read mutex and block profiles
Profiles aggregate sampled waiting costs by stack. A block profile highlights time blocked on synchronization primitives; a mutex profile attributes contention to stacks associated with releasing locks. Neither is a chronological trace or a count of bugs. Start with go test -blockprofile=block.out -mutexprofile=mutex.out -count=1 in a disposable module, then inspect with go tool pprof -top block.out and go tool pprof -top mutex.out.
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 ( "bytes" "runtime" "runtime/pprof" "strings" "sync" "testing")
func TestLesson(t *testing.T) { runtime.SetBlockProfileRate(1) defer runtime.SetBlockProfileRate(0) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() }() wg.Wait() var b bytes.Buffer p := pprof.Lookup("block") if p == nil { t.Fatal("no profile") } if err := p.WriteTo(&b, 1); err != nil { t.Fatal(err) } if !strings.Contains(b.String(), "contention") { t.Fatal("unexpected header") }}Rate 1 requests all blocking events; zero disables block profiling. The text header is checked, not a particular sample count. Scheduler and sampling affect the observations. Inspect the stacks contributing the most delay, then investigate why waiting is expensive. This setting is process-wide; use isolated tests, not parallel tests that change it.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonCapture mutex profile safely
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "bytes" "runtime" "runtime/pprof" "sync" "testing")
func TestLesson(t *testing.T) { old := runtime.SetMutexProfileFraction(1) defer runtime.SetMutexProfileFraction(old) var mu sync.Mutex var wg sync.WaitGroup total := 0 for i := 0; i < 2; i++ { wg.Add(1) go func() { defer wg.Done() for j := 0; j < 100; j++ { mu.Lock() total++ mu.Unlock() } }() } wg.Wait() if total != 200 { t.Fatal(total) } var b bytes.Buffer p := pprof.Lookup("mutex") if p == nil { t.Fatal("no profile") } if err := p.WriteTo(&b, 0); err != nil { t.Fatal(err) } if b.Len() == 0 { t.Fatal("empty encoding") }}Fraction 1 requests every mutex contention event; restore the previous fraction afterward. Debug 0 writes the binary profile. The encoding can exist with no useful samples, so a nonempty buffer is not evidence of contention or a performance improvement. Compare profiles from comparable workloads and measure overhead; profiling can perturb scheduling.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Write the block profile in binary format to a bytes.Buffer and check that writing succeeded. Do not assert specific durations or sample counts. 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
Concurrency testing 05: Read mutex and block profiles
Check the lesson objective before moving on.
What does a large mutex delay entry suggest investigating?
Why avoid asserting exact profile sample counts?