Skip to content

Commit

Permalink
Merge pull request #639 from CosmWasm/635-better-app-benchmarks
Browse files Browse the repository at this point in the history
More realistic benchmarks
  • Loading branch information
ethanfrey authored Oct 11, 2021
2 parents e18e679 + dee6b69 commit 6a471a4
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 171 deletions.
4 changes: 2 additions & 2 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
// WasmApp testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 2000000,
MaxGas: 20000000,
MaxBytes: 8000000,
MaxGas: 80000000,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
Expand Down
161 changes: 154 additions & 7 deletions benchmarks/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ package benchmarks

import (
"encoding/json"
"io/ioutil"
"testing"
"time"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

func setup(withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*app.WasmApp, app.GenesisState) {
db := dbm.NewMemDB()
func setup(db dbm.DB, withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*app.WasmApp, app.GenesisState) {
wasmApp := app.NewWasmApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, app.DefaultNodeHome, invCheckPeriod, wasm.EnableAllProposals, app.EmptyBaseAppOptions{}, opts)
if withGenesis {
return wasmApp, app.NewDefaultGenesisState()
Expand All @@ -26,8 +36,8 @@ func setup(withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*app.Was

// SetupWithGenesisAccounts initializes a new WasmApp with the provided genesis
// accounts and possible balances.
func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.WasmApp {
wasmApp, genesisState := setup(true, 0)
func SetupWithGenesisAccounts(db dbm.DB, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.WasmApp {
wasmApp, genesisState := setup(db, true, 0)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
encodingConfig := app.MakeEncodingConfig()
appCodec := encodingConfig.Marshaler
Expand Down Expand Up @@ -60,3 +70,140 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba

return wasmApp
}

type AppInfo struct {
App *app.WasmApp
MinterKey *secp256k1.PrivKey
MinterAddr sdk.AccAddress
ContractAddr string
Denom string
AccNum uint64
SeqNum uint64
TxConfig client.TxConfig
}

func InitializeWasmApp(b testing.TB, db dbm.DB, numAccounts int) AppInfo {
// constants
minter := secp256k1.GenPrivKey()
addr := sdk.AccAddress(minter.PubKey().Address())
denom := "uatom"

// genesis setup (with a bunch of random accounts)
genAccs := make([]authtypes.GenesisAccount, numAccounts+1)
bals := make([]banktypes.Balance, numAccounts+1)
genAccs[0] = &authtypes.BaseAccount{
Address: addr.String(),
}
bals[0] = banktypes.Balance{
Address: addr.String(),
Coins: sdk.NewCoins(sdk.NewInt64Coin(denom, 100000000000)),
}
for i := 0; i <= numAccounts; i++ {
acct := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
if i == 0 {
acct = addr.String()
}
genAccs[i] = &authtypes.BaseAccount{
Address: acct,
}
bals[i] = banktypes.Balance{
Address: acct,
Coins: sdk.NewCoins(sdk.NewInt64Coin(denom, 100000000000)),
}
}
wasmApp := SetupWithGenesisAccounts(db, genAccs, bals...)

// add wasm contract
height := int64(2)
txGen := simappparams.MakeTestEncodingConfig().TxConfig
wasmApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: height, Time: time.Now()}})

// upload the code
cw20Code, err := ioutil.ReadFile("./testdata/cw20_base.wasm")
require.NoError(b, err)
storeMsg := wasmtypes.MsgStoreCode{
Sender: addr.String(),
WASMByteCode: cw20Code,
}
storeTx, err := helpers.GenTx(txGen, []sdk.Msg{&storeMsg}, nil, 55123123, "", []uint64{0}, []uint64{0}, minter)
require.NoError(b, err)
_, res, err := wasmApp.Deliver(txGen.TxEncoder(), storeTx)
require.NoError(b, err)
codeID := uint64(1)

// instantiate the contract
initialBalances := make([]balance, numAccounts+1)
for i := 0; i <= numAccounts; i++ {
acct := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
if i == 0 {
acct = addr.String()
}
initialBalances[i] = balance{
Address: acct,
Amount: 1000000000,
}
}
init := cw20InitMsg{
Name: "Cash Money",
Symbol: "CASH",
Decimals: 2,
InitialBalances: initialBalances,
}
initBz, err := json.Marshal(init)
require.NoError(b, err)
initMsg := wasmtypes.MsgInstantiateContract{
Sender: addr.String(),
Admin: addr.String(),
CodeID: codeID,
Label: "Demo contract",
Msg: initBz,
}
gasWanted := 500000 + 10000*uint64(numAccounts)
initTx, err := helpers.GenTx(txGen, []sdk.Msg{&initMsg}, nil, gasWanted, "", []uint64{0}, []uint64{1}, minter)
require.NoError(b, err)
_, res, err = wasmApp.Deliver(txGen.TxEncoder(), initTx)
require.NoError(b, err)

// TODO: parse contract address better
evt := res.Events[len(res.Events)-1]
attr := evt.Attributes[0]
contractAddr := string(attr.Value)

wasmApp.EndBlock(abci.RequestEndBlock{Height: height})
wasmApp.Commit()

return AppInfo{
App: wasmApp,
MinterKey: minter,
MinterAddr: addr,
ContractAddr: contractAddr,
Denom: denom,
AccNum: 0,
SeqNum: 2,
TxConfig: simappparams.MakeTestEncodingConfig().TxConfig,
}
}

func GenSequenceOfTxs(b testing.TB, info *AppInfo, msgGen func(*AppInfo) ([]sdk.Msg, error), numToGenerate int) []sdk.Tx {
fees := sdk.Coins{sdk.NewInt64Coin(info.Denom, 0)}
txs := make([]sdk.Tx, numToGenerate)

for i := 0; i < numToGenerate; i++ {
msgs, err := msgGen(info)
require.NoError(b, err)
txs[i], err = helpers.GenTx(
info.TxConfig,
msgs,
fees,
1234567,
"",
[]uint64{info.AccNum},
[]uint64{info.SeqNum},
info.MinterKey,
)
require.NoError(b, err)
info.SeqNum += 1
}

return txs
}
Loading

0 comments on commit 6a471a4

Please sign in to comment.