Skip to content

Commit 0ff8a7d

Browse files
apd: avoid rapid b.StartTimer and b.StopTimer in benchmarks
They cause the benchmarks to run for a very long time. See golang/go#27217. Adjust the benchmarks to have an explicit setup phase and run phase, separated by `b.ResetTimer`.
1 parent d020e15 commit 0ff8a7d

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

Diff for: gda_test.go

+34-23
Original file line numberDiff line numberDiff line change
@@ -296,32 +296,43 @@ func (tc TestCase) Run(c *Context, done chan error, d, x, y *Decimal) (res Condi
296296
func BenchmarkGDA(b *testing.B) {
297297
for _, fname := range GDAfiles {
298298
b.Run(fname, func(b *testing.B) {
299-
b.StopTimer()
299+
type benchCase struct {
300+
tc TestCase
301+
ctx *Context
302+
res []Decimal
303+
ops [2]*Decimal
304+
}
300305
_, tcs := readGDA(b, fname)
301-
res := new(Decimal)
302-
for i := 0; i < b.N; i++ {
303-
Loop:
304-
for _, tc := range tcs {
305-
if GDAignore[tc.ID] || tc.Result == "?" || tc.HasNull() {
306-
continue
307-
}
308-
switch tc.Operation {
309-
case "apply", "toeng":
310-
continue
311-
}
312-
operands := make([]*Decimal, 2)
313-
for i, o := range tc.Operands {
314-
d, _, err := NewFromString(o)
315-
if err != nil {
316-
continue Loop
317-
}
318-
operands[i] = d
306+
bcs := make([]benchCase, 0, len(tcs))
307+
Loop:
308+
for _, tc := range tcs {
309+
if GDAignore[tc.ID] || tc.Result == "?" || tc.HasNull() {
310+
continue
311+
}
312+
switch tc.Operation {
313+
case "apply", "toeng":
314+
continue
315+
}
316+
bc := benchCase{
317+
tc: tc,
318+
ctx: tc.Context(b),
319+
res: make([]Decimal, b.N),
320+
}
321+
for i, o := range tc.Operands {
322+
d, _, err := NewFromString(o)
323+
if err != nil {
324+
continue Loop
319325
}
320-
c := tc.Context(b)
321-
b.StartTimer()
326+
bc.ops[i] = d
327+
}
328+
bcs = append(bcs, bc)
329+
}
330+
331+
b.ResetTimer()
332+
for i := 0; i < b.N; i++ {
333+
for _, bc := range bcs {
322334
// Ignore errors here because the full tests catch them.
323-
_, _ = tc.Run(c, nil, res, operands[0], operands[1])
324-
b.StopTimer()
335+
_, _ = bc.tc.Run(bc.ctx, nil, &bc.res[i], bc.ops[0], bc.ops[1])
325336
}
326337
}
327338
})

Diff for: table_test.go

+26-20
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,50 @@ import (
2323
)
2424

2525
func BenchmarkNumDigitsLookup(b *testing.B) {
26-
b.StopTimer()
27-
runTest := func(start string, c byte) {
26+
prep := func(start string, c byte) []*Decimal {
27+
var ds []*Decimal
2828
buf := bytes.NewBufferString(start)
2929
for i := 1; i < digitsTableSize; i++ {
3030
buf.WriteByte(c)
3131
d, _, _ := NewFromString(buf.String())
32-
33-
b.StartTimer()
34-
d.NumDigits()
35-
b.StopTimer()
32+
ds = append(ds, d)
3633
}
34+
return ds
3735
}
36+
var ds []*Decimal
37+
ds = append(ds, prep("", '9')...)
38+
ds = append(ds, prep("1", '0')...)
39+
ds = append(ds, prep("-", '9')...)
40+
ds = append(ds, prep("-1", '0')...)
41+
b.ResetTimer()
3842
for i := 0; i < b.N; i++ {
39-
runTest("", '9')
40-
runTest("1", '0')
41-
runTest("-", '9')
42-
runTest("-1", '0')
43+
for _, d := range ds {
44+
d.NumDigits()
45+
}
4346
}
4447
}
4548

4649
func BenchmarkNumDigitsFull(b *testing.B) {
47-
b.StopTimer()
48-
runTest := func(start string, c byte) {
50+
prep := func(start string, c byte) []*Decimal {
51+
var ds []*Decimal
4952
buf := bytes.NewBufferString(start)
5053
for i := 1; i < 1000; i++ {
5154
buf.WriteByte(c)
5255
d, _, _ := NewFromString(buf.String())
53-
54-
b.StartTimer()
55-
d.NumDigits()
56-
b.StopTimer()
56+
ds = append(ds, d)
5757
}
58+
return ds
5859
}
60+
var ds []*Decimal
61+
ds = append(ds, prep("", '9')...)
62+
ds = append(ds, prep("1", '0')...)
63+
ds = append(ds, prep("-", '9')...)
64+
ds = append(ds, prep("-1", '0')...)
65+
b.ResetTimer()
5966
for i := 0; i < b.N; i++ {
60-
runTest("", '9')
61-
runTest("1", '0')
62-
runTest("-", '9')
63-
runTest("-1", '0')
67+
for _, d := range ds {
68+
d.NumDigits()
69+
}
6470
}
6571
}
6672

0 commit comments

Comments
 (0)