1
2
3
4
5 package testing
6
7 import (
8 "flag"
9 "fmt"
10 "os"
11 "runtime"
12 "time"
13 )
14
15 var matchBenchmarks = flag.String("test.bench", "", "regular expression to select benchmarks to run")
16 var benchTime = flag.Duration("test.benchtime", 1*time.Second, "approximate run time for each benchmark")
17
18
19
20 type InternalBenchmark struct {
21 Name string
22 F func(b *B)
23 }
24
25
26
27 type B struct {
28 common
29 N int
30 benchmark InternalBenchmark
31 bytes int64
32 timerOn bool
33 result BenchmarkResult
34 }
35
36
37
38
39 func (b *B) StartTimer() {
40 if !b.timerOn {
41 b.start = time.Now()
42 b.timerOn = true
43 }
44 }
45
46
47
48
49 func (b *B) StopTimer() {
50 if b.timerOn {
51 b.duration += time.Now().Sub(b.start)
52 b.timerOn = false
53 }
54 }
55
56
57
58 func (b *B) ResetTimer() {
59 if b.timerOn {
60 b.start = time.Now()
61 }
62 b.duration = 0
63 }
64
65
66
67 func (b *B) SetBytes(n int64) { b.bytes = n }
68
69 func (b *B) nsPerOp() int64 {
70 if b.N <= 0 {
71 return 0
72 }
73 return b.duration.Nanoseconds() / int64(b.N)
74 }
75
76
77 func (b *B) runN(n int) {
78
79
80 runtime.GC()
81 b.N = n
82 b.ResetTimer()
83 b.StartTimer()
84 b.benchmark.F(b)
85 b.StopTimer()
86 }
87
88 func min(x, y int) int {
89 if x > y {
90 return y
91 }
92 return x
93 }
94
95 func max(x, y int) int {
96 if x < y {
97 return y
98 }
99 return x
100 }
101
102
103 func roundDown10(n int) int {
104 var tens = 0
105
106 for n > 10 {
107 n = n / 10
108 tens++
109 }
110
111 result := 1
112 for i := 0; i < tens; i++ {
113 result *= 10
114 }
115 return result
116 }
117
118
119 func roundUp(n int) int {
120 base := roundDown10(n)
121 if n < (2 * base) {
122 return 2 * base
123 }
124 if n < (5 * base) {
125 return 5 * base
126 }
127 return 10 * base
128 }
129
130
131 func (b *B) run() BenchmarkResult {
132 go b.launch()
133 <-b.signal
134 return b.result
135 }
136
137
138
139
140
141
142 func (b *B) launch() {
143
144 n := 1
145
146
147
148 defer func() {
149 b.signal <- b
150 }()
151
152 b.runN(n)
153
154 d := *benchTime
155 for !b.failed && b.duration < d && n < 1e9 {
156 last := n
157
158 if b.nsPerOp() == 0 {
159 n = 1e9
160 } else {
161 n = int(d.Nanoseconds() / b.nsPerOp())
162 }
163
164
165
166 n = max(min(n+n/2, 100*last), last+1)
167
168 n = roundUp(n)
169 b.runN(n)
170 }
171 b.result = BenchmarkResult{b.N, b.duration, b.bytes}
172 }
173
174
175 type BenchmarkResult struct {
176 N int
177 T time.Duration
178 Bytes int64
179 }
180
181 func (r BenchmarkResult) NsPerOp() int64 {
182 if r.N <= 0 {
183 return 0
184 }
185 return r.T.Nanoseconds() / int64(r.N)
186 }
187
188 func (r BenchmarkResult) mbPerSec() float64 {
189 if r.Bytes <= 0 || r.T <= 0 || r.N <= 0 {
190 return 0
191 }
192 return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds()
193 }
194
195 func (r BenchmarkResult) String() string {
196 mbs := r.mbPerSec()
197 mb := ""
198 if mbs != 0 {
199 mb = fmt.Sprintf("\t%7.2f MB/s", mbs)
200 }
201 nsop := r.NsPerOp()
202 ns := fmt.Sprintf("%10d ns/op", nsop)
203 if r.N > 0 && nsop < 100 {
204
205
206 if nsop < 10 {
207 ns = fmt.Sprintf("%13.2f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
208 } else {
209 ns = fmt.Sprintf("%12.1f ns/op", float64(r.T.Nanoseconds())/float64(r.N))
210 }
211 }
212 return fmt.Sprintf("%8d\t%s%s", r.N, ns, mb)
213 }
214
215
216
217 func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) {
218
219 if len(*matchBenchmarks) == 0 {
220 return
221 }
222 for _, Benchmark := range benchmarks {
223 matched, err := matchString(*matchBenchmarks, Benchmark.Name)
224 if err != nil {
225 fmt.Fprintf(os.Stderr, "testing: invalid regexp for -test.bench: %s\n", err)
226 os.Exit(1)
227 }
228 if !matched {
229 continue
230 }
231 for _, procs := range cpuList {
232 runtime.GOMAXPROCS(procs)
233 b := &B{
234 common: common{
235 signal: make(chan any),
236 },
237 benchmark: Benchmark,
238 }
239 benchName := Benchmark.Name
240 if procs != 1 {
241 benchName = fmt.Sprintf("%s-%d", Benchmark.Name, procs)
242 }
243 fmt.Printf("%s\t", benchName)
244 r := b.run()
245 if b.failed {
246
247
248
249 fmt.Printf("--- FAIL: %s\n%s", benchName, b.output)
250 continue
251 }
252 fmt.Printf("%v\n", r)
253
254
255 if len(b.output) > 0 {
256 b.trimOutput()
257 fmt.Printf("--- BENCH: %s\n%s", benchName, b.output)
258 }
259 if p := runtime.GOMAXPROCS(-1); p != procs {
260 fmt.Fprintf(os.Stderr, "testing: %s left GOMAXPROCS set to %d\n", benchName, p)
261 }
262 }
263 }
264 }
265
266
267 func (b *B) trimOutput() {
268
269
270
271 const maxNewlines = 10
272 for nlCount, j := 0, 0; j < len(b.output); j++ {
273 if b.output[j] == '\n' {
274 nlCount++
275 if nlCount >= maxNewlines {
276 b.output = append(b.output[:j], "\n\t... [output truncated]\n"...)
277 break
278 }
279 }
280 }
281 }
282
283
284
285 func Benchmark(f func(b *B)) BenchmarkResult {
286 b := &B{
287 common: common{
288 signal: make(chan any),
289 },
290 benchmark: InternalBenchmark{"", f},
291 }
292 return b.run()
293 }
294
View as plain text