-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjohnny_bench_test.go
286 lines (248 loc) · 6.96 KB
/
johnny_bench_test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package johnny
import (
"testing"
"github.com/profe-ajedrez/gyro"
)
func BenchmarkDiscount(b *testing.B) {
qty := udfs("10")
benchCases := []struct {
name string
entry gyro.Gyro
ratios []gyro.Gyro
}{
{
name: "discountable 100.123 perc discount 10 and 15",
entry: udfs("100.123"),
ratios: func() []gyro.Gyro { g := make([]gyro.Gyro, 2); g[0] = udfs("15"); g[1] = udfs("10"); return g }(),
},
{
name: "discountable 172780372728901.12323223 perc discount 10.343244, 15 and 34.5654664",
entry: udfs("172780372728901.12323223"),
ratios: func() []gyro.Gyro {
g := make([]gyro.Gyro, 3)
g[0] = udfs("10.343244")
g[1] = udfs("10")
g[2] = udfs("34.5654664")
return g
}(),
},
}
var bg FromUnitValue
for _, tc := range benchCases {
discountOverEntryValue := NewPercentualDiscount(tc.ratios[0])
discountConsideringQty := NewPercentualDiscount(tc.ratios[1])
b.ResetTimer()
b.Run(tc.name, func(b *testing.B) {
for i := 0; i <= b.N; i++ {
bg = NewFromUnitValue(tc.entry)
bg.Visit(discountOverEntryValue)
bg.Visit(WithQTY(qty))
bg.Visit(discountConsideringQty)
}
})
}
}
func BenchmarkCreateTaxe(b *testing.B) {
benchCases := []struct {
name string
entry gyro.Gyro
ratio gyro.Gyro
}{
{
name: "taxable 100.123 tax 10%",
entry: udfs("100.123"),
ratio: udfs("10"),
},
{
name: "taxable 10.123 tax 1%",
entry: udfs("10.123"),
ratio: udfs("1"),
},
{
name: "taxable 100000.123001 tax 7.566%",
entry: udfs("100000.123001"),
ratio: udfs("7.566"),
},
{
name: "taxable 111111111111111100000.123001 tax 7.11111111566%",
entry: udfs("111111111111111100000.123001"),
ratio: udfs("7.11111111566"),
},
}
b.ResetTimer()
for _, tc := range benchCases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i <= b.N; i++ {
bg := NewFromUnitValue(tc.entry)
taxOverEntryValue := NewPercTax(tc.ratio)
bg.Receive(taxOverEntryValue)
}
})
}
}
func BenchmarkTax(b *testing.B) {
entry := udfs("100.123")
ratio := udfs("10")
ratio2 := udfs("15")
qty := udfs("10")
taxOverEntryValue := NewPercTax(ratio)
taxConsideringQty := NewPercTax(ratio2)
b.ResetTimer()
for i := 0; i <= b.N; i++ {
bg := NewFromUnitValue(entry)
bg.Receive(taxOverEntryValue)
bg.Receive(WithQTY(qty))
bg.Receive(taxConsideringQty)
}
}
// BenchmarkPercentualUndiscount: Benchmarks the Visit method of the PercentualUndiscount struct with positive, negative, zero, and fractional ratios.
func BenchmarkPercentualUndiscount(b *testing.B) {
testCases := []struct {
name string
initial gyro.Gyro
ratio gyro.Gyro
}{
{
name: "Positive ratio",
initial: udfs("100"),
ratio: udfs("10"),
},
{
name: "Zero ratio",
initial: udfs("100"),
ratio: udfs("0"),
},
{
name: "Fractional ratio",
initial: udfs("100"),
ratio: udfs("5.5"),
},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
u := NewPercentualUnDiscount(tc.ratio)
bg := &DefaultJohnny{v: tc.initial}
u.Visit(bg)
}
})
}
}
// BenchmarkAmountUndiscount: Benchmarks the Visit method of the AmountUndiscount struct with positive, negative, zero, and fractional amounts.
func BenchmarkAmountUndiscount(b *testing.B) {
testCases := []struct {
name string
initial gyro.Gyro
amount gyro.Gyro
}{
{
name: "Positive amount",
initial: udfs("100"),
amount: udfs("10"),
},
{
name: "Zero amount",
initial: udfs("100"),
amount: udfs("0"),
},
{
name: "Fractional amount",
initial: udfs("100"),
amount: udfs("5.5"),
},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
bg := &DefaultJohnny{v: tc.initial}
u := NewAmountUnDiscount(tc.amount)
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.Visit(bg)
}
})
}
}
func BenchmarkTaxHandlerFromUnitValue(b *testing.B) {
entry := udfs("232.5")
qty := udfs("3")
tax := udfs("16")
th := NewTaxHandlerFromUnitValue()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
bg := NewFromUnitValue(entry)
th.WithPercentualTax(tax)
qtyEv := WithQTY(qty)
bg.Visit(qtyEv)
bg.Snapshot() // snapshot of the net value
th.Visit(bg)
bg.Snapshot() // snapshot of the brute value
}
}
func BenchmarkFromBrute(b *testing.B) {
const maxScale = 12
const scaleForNet = 6
b.ResetTimer()
for i := 0; i <= b.N; i++ {
// we define the inmstance of johnny to use
// this time we will calculate the detail values from the brute value
bg := NewFromBruteDefault().WithBrute(udfs("1619.1"))
// We define the visitors to use to calculate the values
brute := &SnapshotVisitor{}
net := &SnapshotVisitor{}
netWD := &SnapshotVisitor{}
unitValue := NewUnitValue(udfs("3"))
// using the snapshot visitor we preserve the value of the brute
bg.Receive(brute)
// We apply a percentualUntac visitor to remove the taxes from the brute,
// means we get the net value
bg.Receive(NewPercentualUnTax(udfs("16")))
// We apply a snapshot visitor to preserve the net value
bg.Receive(net)
// We apply a PercentualUnDiscount visitor to remove the discount from the net,
// means we get the net value without discount
bg.Receive(NewPercentualUnDiscount(udfs("0")))
// We apply a snapshot visitor to preserve the net value without discount
bg.Receive(netWD)
// We apply a unit value visitor to get the unit value
bg.Receive(unitValue)
// the Round visitor is used to round the values to a given scale
bg.Receive(NewRound(maxScale))
_ = net.Get().Round(scaleForNet)
unitValue.Round(maxScale)
}
}
func BenchmarkFromUnitValue(b *testing.B) {
b.ResetTimer()
for i := 0; i <= b.N; i++ {
unitValue := udfs("1044.543103448276")
qty := udfs("35157")
percDiscount := udfs("10")
amountLineDiscount := udfs("100")
percTax := udfs("16")
amountLineTax := qty.Div(gyro.NewHundred()).Round(0).Mul(udfs("0.04"))
// instance the calculator as a new FromUnitValue
calc := NewFromUnitValue(unitValue)
// define the visitors to be used in the calculations
qtyVisitor := WithQTY(qty)
percDiscVisitor := NewPercentualDiscount(percDiscount)
amountDiscVisitor := NewAmountDiscount(amountLineDiscount)
percTaxVisitor := NewUnbufferedPercTax(percTax)
amountTaxVisitor := NewUnbufferedAmountTax(amountLineTax)
// Receive the visitors to the calculator
calc.Receive(qtyVisitor)
calc.Receive(percDiscVisitor)
calc.Receive(amountDiscVisitor)
calc.Receive(percTaxVisitor)
calc.Receive(amountTaxVisitor)
// get the net value from the snapshot visitor
_ = calc.Snapshot()
calc.Add(percTaxVisitor.Amount())
calc.Add(amountTaxVisitor.Amount())
// get the brute value from the snapshot visitor
_ = calc.Snapshot()
// get the total taxes amount from the percTaxVisitor and amountTaxVisitor visitors
_ = percTaxVisitor.Amount().Add(amountTaxVisitor.Amount())
// get the total discount amount from the percDiscVisitor and amountDiscVisitor visitors
_ = percDiscVisitor.Amount().Add(amountDiscVisitor.Amount())
}
}