| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package testutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/ |
| 5 | |
| 6 | import ( |
| 7 | "testingscip-go gomod github.com/golang/go/src go1.17 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 TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB# interface { |
| 27 | testingscip-go gomod github.com/golang/go/src go1.17 testing/.TBscip-go gomod github.com/golang/go/src go1.17 testing/TB# |
| 28 | IsBenchmarkscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#IsBenchmark.() bool |
| 29 | Runscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#Run.(name string, f func(t TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#)) bool |
| 30 | |
| 31 | SetBytesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#SetBytes.(n int64) |
| 32 | Nscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#N.() int |
| 33 | |
| 34 | ResetTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#ResetTimer.() |
| 35 | StartTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#StartTimer.() |
| 36 | StopTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#StopTimer.() |
| 37 | |
| 38 | ReportAllocsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#ReportAllocs.() |
| 39 | ReportMetricscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#ReportMetric.(n float64, unit string) |
| 40 | } |
| 41 | |
| 42 | // tb implements TB as well as testing.TB interfaces. |
| 43 | type tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb# struct { |
| 44 | testingscip-go gomod github.com/golang/go/src go1.17 testing/.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB. |
| 45 | } |
| 46 | |
| 47 | // NewTB creates tb from testing.TB. |
| 48 | func NewTBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/NewTB().(t testingscip-go gomod github.com/golang/go/src go1.17 testing/.TBscip-go gomod github.com/golang/go/src go1.17 testing/TB#) TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB# { return &tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#{TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/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 *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) Runscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#Run().(name string, f func(t TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/TB#)) bool { |
| 55 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 56 | return b.Runscip-go gomod github.com/golang/go/src go1.17 testing/B#Run().(name, func(nested *testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#) { f(&tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#{TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB.: nested}) }) |
| 57 | } |
| 58 | if t, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Tscip-go gomod github.com/golang/go/src go1.17 testing/T#); ok { |
| 59 | return t.Runscip-go gomod github.com/golang/go/src go1.17 testing/T#Run().(name, func(nested *testingscip-go gomod github.com/golang/go/src go1.17 testing/.Tscip-go gomod github.com/golang/go/src go1.17 testing/T#) { f(&tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#{TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/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 *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) Nscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#N().() int { |
| 66 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 67 | return b.Nscip-go gomod github.com/golang/go/src go1.17 testing/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 *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) SetBytesscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#SetBytes().(n int64) { |
| 75 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 76 | b.SetBytesscip-go gomod github.com/golang/go/src go1.17 testing/B#SetBytes().(n) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // ResetTimer resets a timer, if it's a benchmark, noop otherwise. |
| 81 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) ResetTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#ResetTimer().() { |
| 82 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 83 | b.ResetTimerscip-go gomod github.com/golang/go/src go1.17 testing/B#ResetTimer().() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // StartTimer starts a timer, if it's a benchmark, noop otherwise. |
| 88 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) StartTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#StartTimer().() { |
| 89 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 90 | b.StartTimerscip-go gomod github.com/golang/go/src go1.17 testing/B#StartTimer().() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // StopTimer stops a timer, if it's a benchmark, noop otherwise. |
| 95 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) StopTimerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#StopTimer().() { |
| 96 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 97 | b.StopTimerscip-go gomod github.com/golang/go/src go1.17 testing/B#StopTimer().() |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // IsBenchmark returns true if it's a benchmark. |
| 102 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) IsBenchmarkscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#IsBenchmark().() bool { |
| 103 | _, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#) |
| 104 | return ok |
| 105 | } |
| 106 | |
| 107 | // ReportAllocs reports allocs if it's a benchmark, noop otherwise. |
| 108 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) ReportAllocsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#ReportAllocs().() { |
| 109 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 110 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.17 testing/B#ReportAllocs().() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // ReportMetric reports metrics if it's a benchmark, noop otherwise. |
| 115 | func (t *tbscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#) ReportMetricscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#ReportMetric().(n float64, unit string) { |
| 116 | if b, ok := t.TBscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/tb#TB..(*testingscip-go gomod github.com/golang/go/src go1.17 testing/.Bscip-go gomod github.com/golang/go/src go1.17 testing/B#); ok { |
| 117 | b.ReportMetricscip-go gomod github.com/golang/go/src go1.17 testing/B#ReportMetric().(n, unit) |
| 118 | } |
| 119 | } |