Skip to content

Commit 89a1c45

Browse files
authored
testutil: Add missing methods to testorbench (TB). (#9)
Signed-off-by: bwplotka <[email protected]>
1 parent a21078e commit 89a1c45

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

testutil/testorbench.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ type TB interface {
3030

3131
SetBytes(n int64)
3232
N() int
33+
3334
ResetTimer()
35+
StartTimer()
36+
StopTimer()
37+
38+
ReportAllocs()
39+
ReportMetric(n float64, unit string)
3440
}
3541

3642
// tb implements TB as well as testing.TB interfaces.
@@ -78,8 +84,36 @@ func (t *tb) ResetTimer() {
7884
}
7985
}
8086

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+
81101
// IsBenchmark returns true if it's a benchmark.
82102
func (t *tb) IsBenchmark() bool {
83103
_, ok := t.TB.(*testing.B)
84104
return ok
85105
}
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+
}

0 commit comments

Comments
 (0)