Writing multiple benchmarks with different input sizes
You usually want to see the behavior of your algorithms with different input sizes. The Go testing framework only provides the number of times a benchmark should run, not with what input size. Use the following pattern to exercise different input sizes.
How to do it...
- Define an unexported parameterized benchmark function that accepts input size information or inputs of different sizes. The following example gets the number of items and sort direction as arguments, and creates a randomly shuffled input slice with the given size before performing the benchmark:
func benchmarkSort(b *testing.B, nItems int, asc bool) { input := make([]time.Time, nItems) t := time.Now().UnixNano() for i := 0; i < nItems; i++ { input[i] = time.Unix(0, t-int64(i)) } rand.Shuffle...