@@ -3,6 +3,7 @@ package benchmark
3
3
import (
4
4
"fmt"
5
5
"math/rand"
6
+ "sort"
6
7
"testing"
7
8
8
9
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -15,8 +16,41 @@ import (
15
16
oracletypes "github.com/bandprotocol/chain/v2/x/oracle/types"
16
17
)
17
18
18
- // benchmark test for delivering MsgSubmitPrices
19
- func BenchmarkSubmitPricesDeliver (b * testing.B ) {
19
+ var (
20
+ ValidValidator = sdk .ValAddress ("1000000001" )
21
+ ValidValidator2 = sdk .ValAddress ("1000000002" )
22
+ )
23
+
24
+ func BenchmarkSortMap (b * testing.B ) {
25
+ b .ResetTimer ()
26
+ b .StopTimer ()
27
+ ba := InitializeBenchmarkApp (b , - 1 )
28
+ expValPrices := generateValidatorPrices (300 , ValidValidator .String (), ba .Ctx .BlockTime ().Unix ())
29
+ valPricesMap := make (map [string ]types.ValidatorPrice )
30
+ for _ , valPrice := range expValPrices {
31
+ valPricesMap [valPrice .SignalID ] = valPrice
32
+ }
33
+ for i := 0 ; i < b .N ; i ++ {
34
+ for j := 0 ; j < 2000 ; j ++ {
35
+ b .StartTimer ()
36
+ keys := make ([]string , len (valPricesMap ))
37
+ k := int (0 )
38
+ for key := range valPricesMap {
39
+ keys [k ] = key
40
+ k ++
41
+ }
42
+ sort .Strings (keys )
43
+ valPrices := make ([]types.ValidatorPrice , len (valPricesMap ))
44
+ for idx , key := range keys {
45
+ valPrices [idx ] = valPricesMap [key ]
46
+ }
47
+ b .StopTimer ()
48
+ }
49
+ }
50
+ }
51
+
52
+ // benchmark test for delivering MsgSubmitSignalPrices
53
+ func BenchmarkSubmitSignalPricesDeliver (b * testing.B ) {
20
54
b .ResetTimer ()
21
55
b .StopTimer ()
22
56
@@ -31,15 +65,18 @@ func BenchmarkSubmitPricesDeliver(b *testing.B) {
31
65
err = setupFeeds (ba )
32
66
require .NoError (b , err )
33
67
68
+ err = setupValidatorPriceList (ba , vals )
69
+ require .NoError (b , err )
70
+
34
71
ba .CallBeginBlock ()
35
72
36
73
txs := []sdk.Tx {}
37
74
for _ , val := range vals {
38
75
tx := GenSequenceOfTxs (
39
76
ba .TxConfig ,
40
- GenMsgSubmitPrices (
77
+ GenMsgSubmitSignalPrices (
41
78
val ,
42
- ba .FeedsKeeper .GetSupportedFeeds (ba .Ctx ).Feeds ,
79
+ ba .FeedsKeeper .GetCurrentFeeds (ba .Ctx ).Feeds ,
43
80
ba .Ctx .BlockTime ().Unix (),
44
81
),
45
82
val ,
@@ -98,42 +135,70 @@ func BenchmarkFeedsEndBlock(b *testing.B) {
98
135
}
99
136
100
137
func setupFeeds (ba * BenchmarkApp ) error {
101
- numFeeds := ba .FeedsKeeper .GetParams (ba .Ctx ).MaxSupportedFeeds
138
+ numFeeds := ba .FeedsKeeper .GetParams (ba .Ctx ).MaxCurrentFeeds
102
139
103
140
ba .CallBeginBlock ()
104
141
105
142
feeds := []types.Feed {}
106
- for i := int64 (0 ); i < numFeeds ; i ++ {
143
+ for i := uint64 (0 ); i < numFeeds ; i ++ {
107
144
feeds = append (feeds , types.Feed {
108
- SignalID : fmt .Sprintf ("signal.%d" , i ),
109
- Interval : 60 ,
110
- DeviationInThousandth : 5 ,
145
+ SignalID : fmt .Sprintf ("signal.%d" , i ),
146
+ Interval : 60 ,
111
147
})
112
148
}
113
- ba .FeedsKeeper .SetSupportedFeeds (ba .Ctx , feeds )
149
+ ba .FeedsKeeper .SetCurrentFeeds (ba .Ctx , feeds )
150
+
151
+ ba .CallEndBlock ()
152
+ ba .Commit ()
153
+
154
+ return nil
155
+ }
156
+
157
+ func setupValidatorPriceList (ba * BenchmarkApp , vals []* Account ) error {
158
+ sfs := ba .FeedsKeeper .GetCurrentFeeds (ba .Ctx )
114
159
160
+ ba .CallBeginBlock ()
161
+ for valIdx , val := range vals {
162
+ valPrices := []types.ValidatorPrice {}
163
+ for _ , feed := range sfs .Feeds {
164
+ valPrices = append (valPrices , types.ValidatorPrice {
165
+ PriceStatus : types .PriceStatusAvailable ,
166
+ Validator : val .ValAddress .String (),
167
+ SignalID : feed .SignalID ,
168
+ Price : (10000 + uint64 (valIdx )) * 10e9 ,
169
+ Timestamp : ba .Ctx .BlockTime ().Unix () - 40 ,
170
+ })
171
+ }
172
+ err := ba .FeedsKeeper .SetValidatorPriceList (ba .Ctx , val .ValAddress , valPrices )
173
+ if err != nil {
174
+ return err
175
+ }
176
+ }
115
177
ba .CallEndBlock ()
116
178
ba .Commit ()
117
179
118
180
return nil
119
181
}
120
182
121
183
func setupValidatorPrices (ba * BenchmarkApp , vals []* Account ) error {
122
- sfs := ba .FeedsKeeper .GetSupportedFeeds (ba .Ctx )
184
+ sfs := ba .FeedsKeeper .GetCurrentFeeds (ba .Ctx )
123
185
124
186
ba .CallBeginBlock ()
125
- for _ , feed := range sfs .Feeds {
126
- for valIdx , val := range vals {
127
- err := ba .FeedsKeeper .SetValidatorPrice (ba .Ctx , types.ValidatorPrice {
187
+ for valIdx , val := range vals {
188
+ valPrices := []types.ValidatorPrice {}
189
+ for _ , feed := range sfs .Feeds {
190
+ valPrices = append (valPrices , types.ValidatorPrice {
128
191
PriceStatus : types .PriceStatusAvailable ,
129
192
Validator : val .ValAddress .String (),
130
193
SignalID : feed .SignalID ,
131
194
Price : (10000 + uint64 (valIdx )) * 10e9 ,
132
195
Timestamp : ba .Ctx .BlockTime ().Unix (),
133
196
})
134
- if err != nil {
135
- return err
136
- }
197
+ }
198
+
199
+ err := ba .FeedsKeeper .SetValidatorPriceList (ba .Ctx , val .ValAddress , valPrices )
200
+ if err != nil {
201
+ return err
137
202
}
138
203
}
139
204
ba .CallEndBlock ()
@@ -213,3 +278,18 @@ func generateValidators(ba *BenchmarkApp, num int) ([]*Account, error) {
213
278
214
279
return accs , nil
215
280
}
281
+
282
+ // generateValidatorPrices generates a slice of ValidatorPrice with the specified number of elements.
283
+ func generateValidatorPrices (numElements int , validatorAddress string , timestamp int64 ) []types.ValidatorPrice {
284
+ prices := make ([]types.ValidatorPrice , numElements )
285
+
286
+ for i := 0 ; i < numElements ; i ++ {
287
+ prices [i ] = types.ValidatorPrice {
288
+ Validator : validatorAddress ,
289
+ SignalID : fmt .Sprintf ("CS:BAND%d-USD" , i ),
290
+ Price : 1e10 ,
291
+ Timestamp : timestamp ,
292
+ }
293
+ }
294
+ return prices
295
+ }
0 commit comments