diff options
Diffstat (limited to 'src/testing')
| -rw-r--r-- | src/testing/benchmark_test.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/testing/benchmark_test.go b/src/testing/benchmark_test.go index 7e456f9a40..2987170827 100644 --- a/src/testing/benchmark_test.go +++ b/src/testing/benchmark_test.go @@ -181,3 +181,33 @@ func ExampleB_ReportMetric() { b.ReportMetric(float64(compares)/float64(b.Elapsed().Nanoseconds()), "compares/ns") }) } + +func ExampleB_ReportMetric_parallel() { + // This reports a custom benchmark metric relevant to a + // specific algorithm (in this case, sorting) in parallel. + testing.Benchmark(func(b *testing.B) { + var compares atomic.Int64 + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + s := []int{5, 4, 3, 2, 1} + sort.Slice(s, func(i, j int) bool { + // Because RunParallel runs the function many + // times in parallel, we must increment the + // counter atomically to avoid racing writes. + compares.Add(1) + return s[i] < s[j] + }) + } + }) + + // NOTE: Report each metric once, after all of the parallel + // calls have completed. + + // This metric is per-operation, so divide by b.N and + // report it as a "/op" unit. + b.ReportMetric(float64(compares.Load())/float64(b.N), "compares/op") + // This metric is per-time, so divide by b.Elapsed and + // report it as a "/ns" unit. + b.ReportMetric(float64(compares.Load())/float64(b.Elapsed().Nanoseconds()), "compares/ns") + }) +} |
