| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package testutil |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // TB represents union of test and benchmark. |
| 11 | // This allows the same test suite to be run by both benchmark and test, helping to reuse more code. |
| 12 | // The reason is that usually benchmarks are not being run on CI, especially for short tests, so you need to recreate |
| 13 | // usually similar tests for `Test<Name>(t *testing.T)` methods. Example of usage is presented here: |
| 14 | // |
| 15 | // func TestTestOrBench(t *testing.T) { |
| 16 | // tb := NewTB(t) |
| 17 | // tb.Run("1", func(tb TB) { testorbenchComplexTest(tb) }) |
| 18 | // tb.Run("2", func(tb TB) { testorbenchComplexTest(tb) }) |
| 19 | // } |
| 20 | // |
| 21 | // func BenchmarkTestOrBench(b *testing.B) { |
| 22 | // tb := NewTB(t) |
| 23 | // tb.Run("1", func(tb TB) { testorbenchComplexTest(tb) }) |
| 24 | // tb.Run("2", func(tb TB) { testorbenchComplexTest(tb) }) |
| 25 | // } |
| 26 | type TB interface { |
| 27 | testing.TB |
| 28 | IsBenchmark() bool |
| 29 | Run(name string, f func(t TB)) bool |
| 30 | |
| 31 | SetBytes(n int64) |
| 32 | N() int |
| 33 | |
| 34 | ResetTimer() |
| 35 | StartTimer() |
| 36 | StopTimer() |
| 37 | |
| 38 | ReportAllocs() |
| 39 | ReportMetric(n float64, unit string) |
| 40 | } |
| 41 | |
| 42 | // tb implements TB as well as testing.TB interfaces. |
| 43 | type tb struct { |
| 44 | testing.TB |
| 45 | } |
| 46 | |
| 47 | // NewTB creates tb from testing.TB. |
| 48 | func NewTB(t testing.TB) TB { return &tb{TB: t} } |
| 49 | |
| 50 | // Run benchmarks/tests f as a subbenchmark/subtest with the given name. It reports |
| 51 | // whether there were any failures. |
| 52 | // |
| 53 | // A subbenchmark/subtest is like any other benchmark/test. |
| 54 | func (t *tb) Run(name string, f func(t TB)) bool { |
| 55 | if b, ok := t.TB.(*testing.B); ok { |
| 56 | return b.Run(name, func(nested *testing.B) { f(&tb{TB: nested}) }) |
| 57 | } |
| 58 | if t, ok := t.TB.(*testing.T); ok { |
| 59 | return t.Run(name, func(nested *testing.T) { f(&tb{TB: nested}) }) |
| 60 | } |
| 61 | panic("not a benchmark and not a test") |
| 62 | } |
| 63 | |
| 64 | // N returns number of iterations to do for benchmark, 1 in case of test. |
| 65 | func (t *tb) N() int { |
| 66 | if b, ok := t.TB.(*testing.B); ok { |
| 67 | return b.N |
| 68 | } |
| 69 | return 1 |
| 70 | } |
| 71 | |
| 72 | // SetBytes records the number of bytes processed in a single operation for benchmark, noop otherwise. |
| 73 | // If this is called, the benchmark will report ns/op and MB/s. |
| 74 | func (t *tb) SetBytes(n int64) { |
| 75 | if b, ok := t.TB.(*testing.B); ok { |
| 76 | b.SetBytes(n) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // ResetTimer resets a timer, if it's a benchmark, noop otherwise. |
| 81 | func (t *tb) ResetTimer() { |
| 82 | if b, ok := t.TB.(*testing.B); ok { |
| 83 | b.ResetTimer() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // StartTimer starts a timer, if it's a benchmark, noop otherwise. |
| 88 | func (t *tb) StartTimer() { |
| 89 | if b, ok := t.TB.(*testing.B); ok { |
| 90 | b.StartTimer() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // StopTimer stops a timer, if it's a benchmark, noop otherwise. |
| 95 | func (t *tb) StopTimer() { |
| 96 | if b, ok := t.TB.(*testing.B); ok { |
| 97 | b.StopTimer() |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // IsBenchmark returns true if it's a benchmark. |
| 102 | func (t *tb) IsBenchmark() bool { |
| 103 | _, ok := t.TB.(*testing.B) |
| 104 | return ok |
| 105 | } |
| 106 | |
| 107 | // ReportAllocs reports allocs if it's a benchmark, noop otherwise. |
| 108 | func (t *tb) ReportAllocs() { |
| 109 | if b, ok := t.TB.(*testing.B); ok { |
| 110 | b.ReportAllocs() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // ReportMetric reports metrics if it's a benchmark, noop otherwise. |
| 115 | func (t *tb) ReportMetric(n float64, unit string) { |
| 116 | if b, ok := t.TB.(*testing.B); ok { |
| 117 | b.ReportMetric(n, unit) |
| 118 | } |
| 119 | } |