-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoney_bench_test.go
184 lines (143 loc) · 3.61 KB
/
money_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
package money
import "testing"
func BenchmarkNewFromString(b *testing.B) {
// Reset the benchmark timer
b.ResetTimer()
// Create Money instances from a string repeatedly
for i := 0; i < b.N; i++ {
_, err := NewFromString("10.00")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkMustFromString(b *testing.B) {
// Reset the benchmark timer
b.ResetTimer()
// Create Money instances from a string repeatedly
for i := 0; i < b.N; i++ {
MustFromString("10.00")
}
}
func BenchmarkAdd(b *testing.B) {
// Create two Money instances to add together
m1 := MustFromString("10.00")
m2 := MustFromString("5.00")
// Reset the benchmark timer
b.ResetTimer()
// Add the two Money instances together repeatedly
for i := 0; i < b.N; i++ {
m1.Add(m2)
}
}
func BenchmarkAddCentsInt(b *testing.B) {
// Create a Money instance to add to
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Add a cents int repeatedly
for i := 0; i < b.N; i++ {
m.AddCentsInt(500)
}
}
func BenchmarkAddDollarInt(b *testing.B) {
// Create a Money instance to add to
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Add a dollar int repeatedly
for i := 0; i < b.N; i++ {
m.AddDollarInt(1)
}
}
func BenchmarkSubStr(b *testing.B) {
// Create a Money instance to subtract from
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Subtract a string amount repeatedly
for i := 0; i < b.N; i++ {
m.SubStr("5.00")
}
}
func BenchmarkSub(b *testing.B) {
// Create two Money instances to subtract
m1 := MustFromString("10.00")
m2 := MustFromString("5.00")
// Reset the benchmark timer
b.ResetTimer()
// Subtract one Money instance from the other repeatedly
for i := 0; i < b.N; i++ {
m1.Sub(m2)
}
}
func BenchmarkSubCentsInt(b *testing.B) {
// Create a Money instance to subtract from
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Subtract a cents int repeatedly
for i := 0; i < b.N; i++ {
m.SubCentsInt(500)
}
}
func BenchmarkSubDollarInt(b *testing.B) {
// Create a Money instance to subtract from
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Subtract a dollar int repeatedly
for i := 0; i < b.N; i++ {
m.SubDollarInt(1)
}
}
func BenchmarkToString(b *testing.B) {
// Create a Money instance to convert to a string
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Convert the Money instance to a string repeatedly
for i := 0; i < b.N; i++ {
m.ToString()
}
}
func BenchmarkToStringCurrency(b *testing.B) {
// Create a Money instance to convert to a string with currency
m := MustFromString("10.00", EUR)
// Reset the benchmark timer
b.ResetTimer()
// Convert the Money instance to a string with currency repeatedly
for i := 0; i < b.N; i++ {
m.ToStringCurrency()
}
}
func BenchmarkToCentsInt(b *testing.B) {
// Create a Money instance to get the amountCents from
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Get the amountCents from the Money instance repeatedly
for i := 0; i < b.N; i++ {
m.ToCentsInt()
}
}
func BenchmarkMul(b *testing.B) {
// Create a Money instance to multiply
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Multiply the Money instance by a number repeatedly
for i := 0; i < b.N; i++ {
m.Mul(2)
}
}
func BenchmarkDiv(b *testing.B) {
// Create a Money instance to divide
m := MustFromString("10.00")
// Reset the benchmark timer
b.ResetTimer()
// Divide the Money instance by a number repeatedly
for i := 0; i < b.N; i++ {
m.Div(2)
}
}