-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.go
88 lines (69 loc) · 1.73 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gophermark
import (
"testing"
)
type GoconveyAssertFunc func(actual interface{}, expected ...interface{}) string
type benchScope struct {
B *testing.B
Setup func()
Run func()
SanityCheck func()
}
var curBench *benchScope
func Benchmark(b *testing.B, main func()) {
// stop the timer and initialize scope
b.StopTimer()
if curBench != nil {
b.Fatalf("GopherMark: calls to Benchmark() cannot be nested.")
}
curBench = &benchScope{B: b}
// run main, which should set scope vars
main()
// sanity checks
if curBench.Run == nil {
b.Fatalf("GopherMark: no benchmarks to run! Please set Run() " +
"definitions within this benchmark.")
}
// TODO both set
// regular Run
if curBench.Run != nil {
for i := 0; i < b.N; i++ {
if curBench.Setup != nil {
curBench.Setup()
}
b.StartTimer()
curBench.Run()
b.StopTimer()
}
if curBench.SanityCheck != nil {
curBench.SanityCheck()
}
}
curBench = nil
}
func Setup(setupFunc func()) {
if curBench == nil {
panic("GopherMark: can only call Setup() inside of Benchmark()")
}
curBench.Setup = setupFunc
}
func Run(benchFunc func()) {
if curBench == nil {
panic("GopherMark: can only call Run() inside of Benchmark()")
}
curBench.Run = benchFunc
}
func SanityCheck(sanityFunc func()) {
if curBench == nil {
panic("GopherMark: can only call SanityCheck() inside of Benchmark()")
}
curBench.SanityCheck = sanityFunc
}
// for more, see
// https://github.com/smartystreets/goconvey/blob/master/convey/assertions/
func Verify(actual interface{}, assert GoconveyAssertFunc, expected ...interface{}) {
result := assert(actual, expected...)
if result != "" {
curBench.B.Fatalf("GopherMark Verify Failure: \n%s", result)
}
}