Skip to content

Commit 3d8a471

Browse files
RogerKSIcolmazia
andauthored
[Feeds] Add benchmark endblock (#148)
* add max signal id characters * deactivate using block height * add benchmark test for endblock of feeds module * add benchmark for MsgSubmitPrices * calculate supported feeds every n blocks * change default block update param * fix benchmark * using type Signal for signal-total-power * add check err in init genesis * fix test * handle error in test --------- Co-authored-by: colmazia <[email protected]>
1 parent 49f8f07 commit 3d8a471

File tree

4 files changed

+256
-20
lines changed

4 files changed

+256
-20
lines changed

benchmark/feeds_bench_test.go

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package benchmark
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"testing"
7+
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
10+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
11+
"github.com/stretchr/testify/require"
12+
13+
bandtesting "github.com/bandprotocol/chain/v2/testing"
14+
"github.com/bandprotocol/chain/v2/x/feeds/types"
15+
oracletypes "github.com/bandprotocol/chain/v2/x/oracle/types"
16+
)
17+
18+
// benchmark test for delivering MsgSubmitPrices
19+
func BenchmarkSubmitPricesDeliver(b *testing.B) {
20+
b.ResetTimer()
21+
b.StopTimer()
22+
23+
for i := 0; i < b.N; i++ {
24+
ba := InitializeBenchmarkApp(b, -1)
25+
26+
numVals := ba.StakingKeeper.GetParams(ba.Ctx).MaxValidators
27+
28+
vals, err := generateValidators(ba, int(numVals))
29+
require.NoError(b, err)
30+
31+
err = setupFeeds(ba)
32+
require.NoError(b, err)
33+
34+
ba.CallBeginBlock()
35+
36+
txs := []sdk.Tx{}
37+
for _, val := range vals {
38+
tx := GenSequenceOfTxs(
39+
ba.TxConfig,
40+
GenMsgSubmitPrices(
41+
val,
42+
ba.FeedsKeeper.GetSupportedFeeds(ba.Ctx).Feeds,
43+
ba.Ctx.BlockTime().Unix(),
44+
),
45+
val,
46+
1,
47+
)[0]
48+
49+
txs = append(txs, tx)
50+
}
51+
52+
for txIdx, tx := range txs {
53+
b.StartTimer()
54+
gasInfo, _, err := ba.CallDeliver(tx)
55+
b.StopTimer()
56+
if err != nil {
57+
require.NoError(b, err)
58+
}
59+
if i == 0 && txIdx == 0 {
60+
fmt.Println("\tCosmos Gas used:", gasInfo.GasUsed)
61+
}
62+
}
63+
64+
ba.CallEndBlock()
65+
ba.Commit()
66+
}
67+
}
68+
69+
// benchmark test for endblock of feeds module
70+
func BenchmarkFeedsEndBlock(b *testing.B) {
71+
ba := InitializeBenchmarkApp(b, -1)
72+
73+
numVals := ba.StakingKeeper.GetParams(ba.Ctx).MaxValidators
74+
75+
vals, err := generateValidators(ba, int(numVals))
76+
require.NoError(b, err)
77+
78+
err = setupFeeds(ba)
79+
require.NoError(b, err)
80+
81+
err = setupValidatorPrices(ba, vals)
82+
require.NoError(b, err)
83+
84+
b.ResetTimer()
85+
b.StopTimer()
86+
87+
// benchmark endblock
88+
for i := 0; i < b.N; i++ {
89+
ba.CallBeginBlock()
90+
91+
// process endblock
92+
b.StartTimer()
93+
ba.CallEndBlock()
94+
b.StopTimer()
95+
96+
ba.Commit()
97+
}
98+
}
99+
100+
func setupFeeds(ba *BenchmarkApp) error {
101+
numFeeds := ba.FeedsKeeper.GetParams(ba.Ctx).MaxSupportedFeeds
102+
103+
ba.CallBeginBlock()
104+
105+
feeds := []types.Feed{}
106+
for i := int64(0); i < numFeeds; i++ {
107+
feeds = append(feeds, types.Feed{
108+
SignalID: fmt.Sprintf("signal.%d", i),
109+
Interval: 60,
110+
DeviationInThousandth: 5,
111+
})
112+
}
113+
ba.FeedsKeeper.SetSupportedFeeds(ba.Ctx, feeds)
114+
115+
ba.CallEndBlock()
116+
ba.Commit()
117+
118+
return nil
119+
}
120+
121+
func setupValidatorPrices(ba *BenchmarkApp, vals []*Account) error {
122+
sfs := ba.FeedsKeeper.GetSupportedFeeds(ba.Ctx)
123+
124+
ba.CallBeginBlock()
125+
for _, feed := range sfs.Feeds {
126+
for valIdx, val := range vals {
127+
err := ba.FeedsKeeper.SetValidatorPrice(ba.Ctx, types.ValidatorPrice{
128+
PriceStatus: types.PriceStatusAvailable,
129+
Validator: val.ValAddress.String(),
130+
SignalID: feed.SignalID,
131+
Price: (10000 + uint64(valIdx)) * 10e9,
132+
Timestamp: ba.Ctx.BlockTime().Unix(),
133+
})
134+
if err != nil {
135+
return err
136+
}
137+
}
138+
}
139+
ba.CallEndBlock()
140+
ba.Commit()
141+
142+
return nil
143+
}
144+
145+
func generateValidators(ba *BenchmarkApp, num int) ([]*Account, error) {
146+
// transfer money
147+
ba.CallBeginBlock()
148+
149+
vals := []bandtesting.Account{}
150+
for i := 0; i < num; i++ {
151+
r := rand.New(rand.NewSource(int64(i)))
152+
acc := bandtesting.CreateArbitraryAccount(r)
153+
vals = append(vals, acc)
154+
155+
tx := GenSequenceOfTxs(
156+
ba.TxConfig,
157+
[]sdk.Msg{banktypes.NewMsgSend(ba.Sender.Address, acc.Address, sdk.NewCoins(sdk.NewCoin("uband", sdk.NewInt(200000000))))},
158+
ba.Sender,
159+
1,
160+
)[0]
161+
162+
_, _, err := ba.CallDeliver(tx)
163+
if err != nil {
164+
return nil, err
165+
}
166+
}
167+
168+
ba.CallEndBlock()
169+
ba.Commit()
170+
171+
// apply to be a validator
172+
ba.CallBeginBlock()
173+
174+
accs := []*Account{}
175+
for _, val := range vals {
176+
info := ba.AccountKeeper.GetAccount(ba.Ctx, val.Address)
177+
acc := &Account{
178+
Account: val,
179+
Num: info.GetAccountNumber(),
180+
Seq: info.GetSequence(),
181+
}
182+
accs = append(accs, acc)
183+
184+
msgCreateVal, err := stakingtypes.NewMsgCreateValidator(
185+
val.ValAddress,
186+
val.PubKey,
187+
sdk.NewCoin("uband", sdk.NewInt(150000000)),
188+
stakingtypes.NewDescription(val.Address.String(), val.Address.String(), "", "", ""),
189+
stakingtypes.NewCommissionRates(sdk.NewDec(1), sdk.NewDec(1), sdk.NewDec(1)),
190+
sdk.NewInt(1),
191+
)
192+
if err != nil {
193+
return nil, err
194+
}
195+
196+
msgActivate := oracletypes.NewMsgActivate(val.ValAddress)
197+
198+
tx := GenSequenceOfTxs(
199+
ba.TxConfig,
200+
[]sdk.Msg{msgCreateVal, msgActivate},
201+
acc,
202+
1,
203+
)[0]
204+
205+
_, _, err = ba.CallDeliver(tx)
206+
if err != nil {
207+
return nil, err
208+
}
209+
}
210+
211+
ba.CallEndBlock()
212+
ba.Commit()
213+
214+
return accs, nil
215+
}

benchmark/helper_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/bandprotocol/chain/v2/pkg/obi"
2121
bandtesting "github.com/bandprotocol/chain/v2/testing"
22+
feedstypes "github.com/bandprotocol/chain/v2/x/feeds/types"
2223
oracletypes "github.com/bandprotocol/chain/v2/x/oracle/types"
2324
)
2425

@@ -70,6 +71,25 @@ func GenMsgRequestData(
7071
return []sdk.Msg{&msg}
7172
}
7273

74+
func GenMsgSubmitPrices(
75+
sender *Account,
76+
feeds []feedstypes.Feed,
77+
timestamp int64,
78+
) []sdk.Msg {
79+
prices := []feedstypes.SubmitPrice{}
80+
for _, feed := range feeds {
81+
prices = append(prices, feedstypes.SubmitPrice{
82+
PriceStatus: feedstypes.PriceStatusAvailable,
83+
SignalID: feed.SignalID,
84+
Price: 60000,
85+
})
86+
}
87+
88+
msg := feedstypes.NewMsgSubmitPrices(sender.ValAddress.String(), timestamp, prices)
89+
90+
return []sdk.Msg{msg}
91+
}
92+
7393
func GenMsgSend(
7494
sender *Account,
7595
receiver *Account,

testing/test_helpers.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ var (
6969
)
7070

7171
var (
72-
EmptyCoins = sdk.Coins(nil)
73-
Coins1uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 1))
74-
Coins10uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 10))
75-
Coins11uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 11))
76-
Coins1000000uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 1000000))
77-
Coins99999999uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 99999999))
78-
Coins100000000uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 100000000))
79-
BadCoins = []sdk.Coin{{Denom: "uband", Amount: sdk.NewInt(-1)}}
72+
EmptyCoins = sdk.Coins(nil)
73+
Coins1uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 1))
74+
Coins10uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 10))
75+
Coins11uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 11))
76+
Coins1000000uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 1000000))
77+
Coins99999999uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 99999999))
78+
Coins100000000uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 100000000))
79+
Coins1000000000000uband = sdk.NewCoins(sdk.NewInt64Coin("uband", 1000000000000))
80+
BadCoins = []sdk.Coin{{Denom: "uband", Amount: sdk.NewInt(-1)}}
8081
)
8182

8283
const (
@@ -139,14 +140,14 @@ func (app *TestingApp) GetTxConfig() client.TxConfig {
139140
func init() {
140141
bandapp.SetBech32AddressPrefixesAndBip44CoinTypeAndSeal(sdk.GetConfig())
141142
r := rand.New(rand.NewSource(time.Now().Unix()))
142-
Owner = createArbitraryAccount(r)
143-
Treasury = createArbitraryAccount(r)
144-
FeePayer = createArbitraryAccount(r)
145-
Alice = createArbitraryAccount(r)
146-
Bob = createArbitraryAccount(r)
147-
Carol = createArbitraryAccount(r)
143+
Owner = CreateArbitraryAccount(r)
144+
Treasury = CreateArbitraryAccount(r)
145+
FeePayer = CreateArbitraryAccount(r)
146+
Alice = CreateArbitraryAccount(r)
147+
Bob = CreateArbitraryAccount(r)
148+
Carol = CreateArbitraryAccount(r)
148149
for i := 0; i < 3; i++ {
149-
Validators = append(Validators, createArbitraryAccount(r))
150+
Validators = append(Validators, CreateArbitraryAccount(r))
150151
}
151152

152153
// Sorted list of validators is needed for ibctest when signing a commit block
@@ -180,7 +181,7 @@ func CreateTestApp(t *testing.T, autoActivate bool) (*TestingApp, sdk.Context) {
180181
balances := []banktypes.Balance{
181182
{
182183
Address: Owner.Address.String(),
183-
Coins: Coins1000000uband,
184+
Coins: Coins1000000000000uband,
184185
},
185186
{Address: FeePayer.Address.String(), Coins: Coins100000000uband},
186187
{Address: Alice.Address.String(), Coins: Coins1000000uband},
@@ -399,8 +400,8 @@ func SetupWithGenesisValSet(
399400
return &TestingApp{app}
400401
}
401402

402-
// createArbitraryAccount generates a random Account using a provided random number generator.
403-
func createArbitraryAccount(r *rand.Rand) Account {
403+
// CreateArbitraryAccount generates a random Account using a provided random number generator.
404+
func CreateArbitraryAccount(r *rand.Rand) Account {
404405
privkeySeed := make([]byte, 12)
405406
r.Read(privkeySeed)
406407
privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed)

x/feeds/keeper/keeper_feed_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func (suite *KeeperTestSuite) TestGetSetSupportedFeeds() {
1010
{
1111
SignalID: "crypto_price.bandusd",
1212
Interval: 60,
13-
DeviationInThousandth: 1000,
13+
DeviationInThousandth: 5,
1414
},
1515
{
1616
SignalID: "crypto_price.atomusd",
1717
Interval: 60,
18-
DeviationInThousandth: 1000,
18+
DeviationInThousandth: 5,
1919
},
2020
}
2121
suite.feedsKeeper.SetSupportedFeeds(ctx, expFeed)

0 commit comments

Comments
 (0)