By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
This lab benchmarks Relay's report rendering and parsing, then uses the profile to decide what to change. The method matters more than the result: establish a baseline with -benchmem, take a CPU profile of the benchmark, and only then edit. Guessing which allocation matters is how you spend an afternoon making a function faster that runs once per day.
A benchmark that does not match production is worse than none. Use representative payload sizes and realistic line lengths. b.ReportAllocs and b.SetBytes make throughput visible, and b.ResetTimer after expensive setup keeps initialization out of the numbers.
-benchmem -count=5 and record the baseline.strings.Builder, or avoid a copy.package analyze_test
import (
"strings"
"testing"
"relay/internal/analyze"
)
func BenchmarkParseLine(b *testing.B) {
line := []byte(`{"service":"checkout","level":"error","status":503,"message":"upstream timeout"}`)
b.ReportAllocs()
b.SetBytes(int64(len(line)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := analyze.ParseLine(line); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkJoinServices(b *testing.B) {
services := []string{"api", "worker", "billing", "search"}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var builder strings.Builder
for index, service := range services {
if index > 0 {
builder.WriteByte(',')
}
builder.WriteString(service)
}
_ = builder.String()
}
}# Baseline: time per op and allocations per op.
go test ./internal/analyze -run '^$' -bench . -benchmem -count=5
# CPU profile of the benchmark, then the top functions.
go test ./internal/analyze -run '^$' -bench . -cpuprofile cpu.out
go tool pprof -top -nodecount=10 cpu.out
# Allocation profile; -alloc_space shows total, -alloc_objects shows count.
go test ./internal/analyze -run '^$' -bench . -memprofile mem.out
go tool pprof -top -alloc_space mem.out
# What escapes to the heap and why (compiler diagnostics).
go build -gcflags='-m' ./internal/analyze 2>&1 | grep -i escape| Observation | Likely cause | First fix to try |
|---|---|---|
High allocs/op in parsing | Per-line string copies or new maps | Reuse a decoder; preallocate maps |
| CPU dominated by JSON | Reflection per line | Decode only fields you need; consider a faster shape |
| CPU dominated by sorting | Too many small sorts | Sort once at the end over aggregated keys |
| Clock time high, CPU low | Blocking I/O inside the benchmark | Move I/O out of the measured path |
| Heap grows every run | Retaining slices per iteration | Preallocate once; stop appending per loop |
-race runs are much slower | The detector instruments every memory access | Profile a normal build; use -race for correctness, not speed |
Key takeaways
Benchmark a production-representative payload, then profile the benchmark.
Use -benchmem, b.ReportAllocs() and b.ResetTimer() to measure what matters.
Read the CPU and allocation profiles before editing code.
Make one change at a time and re-measure; keep readability as a constraint.
If the profile blames I/O, move it out of the measured path instead of optimizing around it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.