Benchmarks are first-class feature of the language and similar to tests.

func BenchmarkRepeat(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Repeat("a")
	}
}

When the benchmark code is executed, it runs b.N times and measures how long it takes. Framework will determine this value b.N by itself, so you don’t need to bother with it.

To run execute the command go test -bench=.

Example output:

➜ go test -bench=.
goos: linux
goarch: amd64
pkg: example.com/iteration
cpu: AMD Ryzen 9 5950X 16-Core Processor            
BenchmarkRepeat-32      13506831               113.8 ns/op
PASS
ok      example.com/iteration   1.632s

113.8 ns/op here means that the function under test took on average 113.8 nanoseconds to execute on our machine.

By default benchmarks are run sequentially.