-
Notifications
You must be signed in to change notification settings - Fork 46
/
bench_test.go
141 lines (116 loc) · 2.59 KB
/
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
package currency_test
import (
"fmt"
"math/big"
"testing"
"github.com/bojanz/currency"
)
var result currency.Amount
var cmpResult int
func BenchmarkNewAmount(b *testing.B) {
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = currency.NewAmount("99.99", "EUR")
}
result = z
}
func BenchmarkNewAmountFromBigInt(b *testing.B) {
x := big.NewInt(9999)
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = currency.NewAmountFromBigInt(x, "EUR")
}
result = z
}
func BenchmarkNewAmountFromInt64(b *testing.B) {
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = currency.NewAmountFromInt64(9999, "EUR")
}
result = z
}
func BenchmarkAmount_Add(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Add(y)
}
result = z
}
func BenchmarkAmount_Sub(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Sub(y)
}
result = z
}
func BenchmarkAmount_Mul(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Mul("2")
}
result = z
}
func BenchmarkAmount_MulDec(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Mul("2.5")
}
result = z
}
func BenchmarkAmount_Div(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Div("2")
}
result = z
}
func BenchmarkAmount_DivDec(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Div("2.5")
}
result = z
}
func BenchmarkAmount_Round(b *testing.B) {
x, _ := currency.NewAmount("34.9876", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z = x.Round()
}
result = z
}
func BenchmarkAmount_RoundTo(b *testing.B) {
x, _ := currency.NewAmount("34.9876", "USD")
roundingModes := []currency.RoundingMode{
currency.RoundHalfUp,
currency.RoundHalfDown,
currency.RoundUp,
currency.RoundDown,
}
for _, roundingMode := range roundingModes {
b.Run(fmt.Sprintf("rounding_mode_%d", roundingMode), func(b *testing.B) {
var z currency.Amount
for n := 0; n < b.N; n++ {
z = x.RoundTo(2, roundingMode)
}
result = z
})
}
}
func BenchmarkAmount_Cmp(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z int
for n := 0; n < b.N; n++ {
z, _ = x.Cmp(y)
}
cmpResult = z
}