1package inf
2
3import (
4	"fmt"
5	"math/big"
6	"math/rand"
7	"sync"
8	"testing"
9)
10
11const maxcap = 1024 * 1024
12const bits = 256
13const maxscale = 32
14
15var once sync.Once
16
17var decInput [][2]Dec
18var intInput [][2]big.Int
19
20var initBench = func() {
21	decInput = make([][2]Dec, maxcap)
22	intInput = make([][2]big.Int, maxcap)
23	max := new(big.Int).Lsh(big.NewInt(1), bits)
24	r := rand.New(rand.NewSource(0))
25	for i := 0; i < cap(decInput); i++ {
26		decInput[i][0].SetUnscaledBig(new(big.Int).Rand(r, max)).
27			SetScale(Scale(r.Int31n(int32(2*maxscale-1)) - int32(maxscale)))
28		decInput[i][1].SetUnscaledBig(new(big.Int).Rand(r, max)).
29			SetScale(Scale(r.Int31n(int32(2*maxscale-1)) - int32(maxscale)))
30	}
31	for i := 0; i < cap(intInput); i++ {
32		intInput[i][0].Rand(r, max)
33		intInput[i][1].Rand(r, max)
34	}
35}
36
37func doBenchmarkDec1(b *testing.B, f func(z *Dec)) {
38	once.Do(initBench)
39	b.ResetTimer()
40	b.StartTimer()
41	for i := 0; i < b.N; i++ {
42		f(&decInput[i%maxcap][0])
43	}
44}
45
46func doBenchmarkDec2(b *testing.B, f func(x, y *Dec)) {
47	once.Do(initBench)
48	b.ResetTimer()
49	b.StartTimer()
50	for i := 0; i < b.N; i++ {
51		f(&decInput[i%maxcap][0], &decInput[i%maxcap][1])
52	}
53}
54
55func doBenchmarkInt1(b *testing.B, f func(z *big.Int)) {
56	once.Do(initBench)
57	b.ResetTimer()
58	b.StartTimer()
59	for i := 0; i < b.N; i++ {
60		f(&intInput[i%maxcap][0])
61	}
62}
63
64func doBenchmarkInt2(b *testing.B, f func(x, y *big.Int)) {
65	once.Do(initBench)
66	b.ResetTimer()
67	b.StartTimer()
68	for i := 0; i < b.N; i++ {
69		f(&intInput[i%maxcap][0], &intInput[i%maxcap][1])
70	}
71}
72
73func Benchmark_Dec_String(b *testing.B) {
74	doBenchmarkDec1(b, func(x *Dec) {
75		x.String()
76	})
77}
78
79func Benchmark_Dec_StringScan(b *testing.B) {
80	doBenchmarkDec1(b, func(x *Dec) {
81		s := x.String()
82		d := new(Dec)
83		fmt.Sscan(s, d)
84	})
85}
86
87func Benchmark_Dec_GobEncode(b *testing.B) {
88	doBenchmarkDec1(b, func(x *Dec) {
89		x.GobEncode()
90	})
91}
92
93func Benchmark_Dec_GobEnDecode(b *testing.B) {
94	doBenchmarkDec1(b, func(x *Dec) {
95		g, _ := x.GobEncode()
96		new(Dec).GobDecode(g)
97	})
98}
99
100func Benchmark_Dec_Add(b *testing.B) {
101	doBenchmarkDec2(b, func(x, y *Dec) {
102		ys := y.Scale()
103		y.SetScale(x.Scale())
104		_ = new(Dec).Add(x, y)
105		y.SetScale(ys)
106	})
107}
108
109func Benchmark_Dec_AddMixed(b *testing.B) {
110	doBenchmarkDec2(b, func(x, y *Dec) {
111		_ = new(Dec).Add(x, y)
112	})
113}
114
115func Benchmark_Dec_Sub(b *testing.B) {
116	doBenchmarkDec2(b, func(x, y *Dec) {
117		ys := y.Scale()
118		y.SetScale(x.Scale())
119		_ = new(Dec).Sub(x, y)
120		y.SetScale(ys)
121	})
122}
123
124func Benchmark_Dec_SubMixed(b *testing.B) {
125	doBenchmarkDec2(b, func(x, y *Dec) {
126		_ = new(Dec).Sub(x, y)
127	})
128}
129
130func Benchmark_Dec_Mul(b *testing.B) {
131	doBenchmarkDec2(b, func(x, y *Dec) {
132		_ = new(Dec).Mul(x, y)
133	})
134}
135
136func Benchmark_Dec_Mul_QuoExact(b *testing.B) {
137	doBenchmarkDec2(b, func(x, y *Dec) {
138		v := new(Dec).Mul(x, y)
139		_ = new(Dec).QuoExact(v, y)
140	})
141}
142
143func Benchmark_Dec_QuoRound_Fixed_Down(b *testing.B) {
144	doBenchmarkDec2(b, func(x, y *Dec) {
145		_ = new(Dec).QuoRound(x, y, 0, RoundDown)
146	})
147}
148
149func Benchmark_Dec_QuoRound_Fixed_HalfUp(b *testing.B) {
150	doBenchmarkDec2(b, func(x, y *Dec) {
151		_ = new(Dec).QuoRound(x, y, 0, RoundHalfUp)
152	})
153}
154
155func Benchmark_Int_String(b *testing.B) {
156	doBenchmarkInt1(b, func(x *big.Int) {
157		x.String()
158	})
159}
160
161func Benchmark_Int_StringScan(b *testing.B) {
162	doBenchmarkInt1(b, func(x *big.Int) {
163		s := x.String()
164		d := new(big.Int)
165		fmt.Sscan(s, d)
166	})
167}
168
169func Benchmark_Int_GobEncode(b *testing.B) {
170	doBenchmarkInt1(b, func(x *big.Int) {
171		x.GobEncode()
172	})
173}
174
175func Benchmark_Int_GobEnDecode(b *testing.B) {
176	doBenchmarkInt1(b, func(x *big.Int) {
177		g, _ := x.GobEncode()
178		new(big.Int).GobDecode(g)
179	})
180}
181
182func Benchmark_Int_Add(b *testing.B) {
183	doBenchmarkInt2(b, func(x, y *big.Int) {
184		_ = new(big.Int).Add(x, y)
185	})
186}
187
188func Benchmark_Int_Sub(b *testing.B) {
189	doBenchmarkInt2(b, func(x, y *big.Int) {
190		_ = new(big.Int).Sub(x, y)
191	})
192}
193
194func Benchmark_Int_Mul(b *testing.B) {
195	doBenchmarkInt2(b, func(x, y *big.Int) {
196		_ = new(big.Int).Mul(x, y)
197	})
198}
199
200func Benchmark_Int_Quo(b *testing.B) {
201	doBenchmarkInt2(b, func(x, y *big.Int) {
202		_ = new(big.Int).Quo(x, y)
203	})
204}
205
206func Benchmark_Int_QuoRem(b *testing.B) {
207	doBenchmarkInt2(b, func(x, y *big.Int) {
208		_, _ = new(big.Int).QuoRem(x, y, new(big.Int))
209	})
210}
211