-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcoin_benchmark_test.go
142 lines (123 loc) · 3.93 KB
/
coin_benchmark_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
package types
import (
"fmt"
"testing"
"cosmossdk.io/math"
)
func coinName(suffix int) string {
return fmt.Sprintf("coinz%04d", suffix)
}
func BenchmarkCoinsAdditionIntersect(b *testing.B) {
b.ReportAllocs()
benchmarkingFunc := func(numCoinsA, numCoinsB int) func(b *testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
coinsA := Coins(make([]Coin, numCoinsA))
coinsB := Coins(make([]Coin, numCoinsB))
for i := 0; i < numCoinsA; i++ {
coinsA[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
}
for i := 0; i < numCoinsB; i++ {
coinsB[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
coinsA.Add(coinsB...)
}
}
}
benchmarkSizes := [][]int{{1, 1}, {5, 5}, {5, 20}, {1, 1000}, {2, 1000}}
for i := 0; i < len(benchmarkSizes); i++ {
sizeA := benchmarkSizes[i][0]
sizeB := benchmarkSizes[i][1]
b.Run(fmt.Sprintf("sizes: A_%d, B_%d", sizeA, sizeB), benchmarkingFunc(sizeA, sizeB))
}
}
func BenchmarkCoinsAdditionNoIntersect(b *testing.B) {
b.ReportAllocs()
benchmarkingFunc := func(numCoinsA, numCoinsB int) func(b *testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
coinsA := Coins(make([]Coin, numCoinsA))
coinsB := Coins(make([]Coin, numCoinsB))
for i := 0; i < numCoinsA; i++ {
coinsA[i] = NewCoin(coinName(numCoinsB+i), math.NewInt(int64(i)))
}
for i := 0; i < numCoinsB; i++ {
coinsB[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
coinsA.Add(coinsB...)
}
}
}
benchmarkSizes := [][]int{{1, 1}, {5, 5}, {5, 20}, {1, 1000}, {2, 1000}, {1000, 2}}
for i := 0; i < len(benchmarkSizes); i++ {
sizeA := benchmarkSizes[i][0]
sizeB := benchmarkSizes[i][1]
b.Run(fmt.Sprintf("sizes: A_%d, B_%d", sizeA, sizeB), benchmarkingFunc(sizeA, sizeB))
}
}
func BenchmarkSumOfCoinAdds(b *testing.B) {
// This benchmark tests the performance of adding a large number of coins
// into a single coin set.
// it does numAdds additions, each addition has (numIntersectingCoins) that contain denoms
// already in the sum, and (coinsPerAdd - numIntersectingCoins) that are new denoms.
benchmarkingFunc := func(numAdds, coinsPerAdd, numIntersectingCoins int, sumFn func([]Coins) Coins) func(b *testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
addCoins := make([]Coins, numAdds)
nonIntersectingCoins := coinsPerAdd - numIntersectingCoins
for i := 0; i < numAdds; i++ {
intersectCoins := make([]Coin, numIntersectingCoins)
num := math.NewInt(int64(i))
for j := 0; j < numIntersectingCoins; j++ {
intersectCoins[j] = NewCoin(coinName(j+1_000_000_000), num)
}
addCoins[i] = intersectCoins
for j := 0; j < nonIntersectingCoins; j++ {
addCoins[i] = addCoins[i].Add(NewCoin(coinName(i*nonIntersectingCoins+j), num))
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sumFn(addCoins)
}
}
}
MapCoinsSumFn := func(coins []Coins) Coins {
sum := MapCoins{}
for _, coin := range coins {
sum.Add(coin...)
}
return sum.ToCoins()
}
CoinsSumFn := func(coins []Coins) Coins {
sum := Coins{}
for _, coin := range coins {
sum = sum.Add(coin...)
}
return sum
}
// larger benchmarks with non-overlapping coins won't terminate in reasonable timeframes with sdk.Coins
// they work fine with MapCoins
benchmarkSizes := [][]int{{5, 2, 1000}, {10, 10, 10000}}
sumFns := []struct {
name string
fn func([]Coins) Coins
}{
{"MapCoins", MapCoinsSumFn}, {"Coins", CoinsSumFn},
}
for i := 0; i < len(benchmarkSizes); i++ {
for j := 0; j < 2; j++ {
coinsPerAdd := benchmarkSizes[i][0]
intersectingCoinsPerAdd := benchmarkSizes[i][1]
numAdds := benchmarkSizes[i][2]
sumFn := sumFns[j]
b.Run(fmt.Sprintf("Fn: %s, num adds: %d, coinsPerAdd: %d, intersecting: %d",
sumFn.name, numAdds, coinsPerAdd, intersectingCoinsPerAdd),
benchmarkingFunc(numAdds, coinsPerAdd, intersectingCoinsPerAdd, sumFn.fn))
}
}
}