Benchmarks and profiling

Contents
Write a BenchmarkX
go test -bench
pprof basics
Watch allocations

Write a BenchmarkX

A benchmark lives in a test file and its name starts with Benchmark. The testing framework calls it with a B value, and the loop over b.N repeats the measured code until timing stabilizes.

Good benchmarks compare two approaches to one task. The file below joins words with plain concatenation and with a strings.Builder, so one run shows which method wins.

package main import ( "strings" "testing" ) func JoinPlus(parts []string) string { out := "" for _, p := range parts { out += p } return out } func BenchmarkJoinPlus(b *testing.B) { parts := []string{"go", "is", "fast"} for i := 0; i < b.N; i++ { JoinPlus(parts) } } func BenchmarkJoinBuilder(b *testing.B) { parts := []string{"go", "is", "fast"} for i := 0; i < b.N; i++ { var sb strings.Builder for _, p := range parts { sb.WriteString(p) } _ = sb.String() } }

go test -bench

The go test tool runs benchmarks when the bench flag matches their names. A dot matches every benchmark, run with NONE skips unit tests, and benchtime controls the workload per benchmark.

Each result line shows the name, the iteration count, and the average time per operation. Smaller time per operation wins, so the Builder line below beats plain concatenation.

go test -bench=. -benchtime=100x -run=NONE

BenchmarkJoinPlus-8 100 420 ns/op

BenchmarkJoinBuilder-8 100 310 ns/op

pprof basics

Timing tells which function is slow, while a profile tells which line inside it burns the time. The test tool writes a CPU profile to a file when the cpuprofile flag points at one.

The go tool pprof command opens that file for questions. The top command lists the hottest functions first, and the list command with a function name shows time per source line.

go test -bench=Builder -cpuprofile=cpu.out -run=NONE

BenchmarkJoinBuilder-8 100 310 ns/op

ok wordfreq 0.412s

go tool pprof -top cpu.out

Type: cpu

Time: Oct 6, 2026

main.JoinBuilder

Watch allocations

Speed is only half the story. Every heap allocation costs the garbage collector future work, so benchmarks should track bytes and allocation counts next to time.

The benchmem flag adds two columns to each result line: bytes per operation and allocations per operation. Fewer allocations often predict calmer production behavior under load.

go test -bench=. -benchmem -run=NONE

BenchmarkJoinPlus-8 100 420 ns/op 64 B/op 3 allocs/op

BenchmarkJoinBuilder-8 100 310 ns/op 24 B/op 1 allocs/op

For a focused check, AllocsPerRun measures one function in isolation. It runs the closure many times and returns the average allocation count as a float.

package main import ( "testing" ) func BenchmarkConcatAllocs(b *testing.B) { parts := []string{"go", "is", "fast"} n := testing.AllocsPerRun(100, func() { out := "" for _, p := range parts { out += p } }) b.ReportMetric(n, "allocs") }

Next: Toolchain and CI

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: