The difference between a test and a benchmark
The last concept we will be looking at in this chapter is benchmarks. The testing package gives us access to benchmarks using the testing.B
type. They have a signature very similar to tests:
func BenchmarkName(b *testing.B) { // implementation }
The signature highlights the following requirements for Go benchmarks:
- Benchmarks are exported functions whose name begins with
Benchmark
. - Benchmark names can have an additional suffix that specifies what the test is covering. The suffix must also begin with a capital letter, as we can see with
Name
. - Benchmarks must take in a single parameter of the
*testing.B
type. As we’ve explained so far, this will be how the test interacts with the test runner. You can name the testing parameter however you want, but Go developers typically useb
to denote it. - Benchmarks must not have a return type.
Benchmarks are an important Go profiling tool
Tests verify...