Benchmarking the code
The previous recipe walks through the testing part of the testing package, and in this recipe, the basics of the benchmarking will be covered.
How to do it...
- Open the console and create the folder
chapter11/recipe03
. - Navigate to the directory.
- Create the file
sample_test.go
with the following content:
package main import ( "log" "testing" ) func BenchmarkSampleOne(b *testing.B) { logger := log.New(devNull{}, "test", log.Llongfile) b.ResetTimer() b.StartTimer() for i := 0; i < b.N; i++ { logger.Println("This si awesome") } b.StopTimer() } type devNull struct{} func (d devNull) Write(b []byte) (int, error) { return 0, nil }
- Execute the benchmark by
go test -bench=
. - See the output in the Terminal:

How it works...
Besides the pure test support, the testing package also provides the mechanisms for measuring...