|
| 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 | +} |
0 commit comments