2019-06-14 07:10:37 -07:00
|
|
|
package bank_test
|
2018-06-27 15:33:56 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-05 16:26:17 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2018-06-27 15:33:56 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-06-14 07:10:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
2018-07-03 21:21:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
2019-07-31 08:39:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2018-06-27 15:33:56 -07:00
|
|
|
)
|
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
var moduleAccAddr = sdk.AccAddress([]byte("moduleAcc"))
|
|
|
|
|
|
|
|
// initialize the mock application for this module
|
|
|
|
func getMockApp(t *testing.T) *mock.App {
|
|
|
|
mapp, err := getBenchmarkMockApp()
|
|
|
|
supply.RegisterCodec(mapp.Cdc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return mapp
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite the mock init chainer
|
|
|
|
func getInitChainer(mapp *mock.App, keeper keeper.BaseKeeper) sdk.InitChainer {
|
|
|
|
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
|
|
|
mapp.InitChainer(ctx, req)
|
|
|
|
bankGenesis := bank.DefaultGenesisState()
|
|
|
|
bank.InitGenesis(ctx, keeper, bankGenesis)
|
|
|
|
|
|
|
|
return abci.ResponseInitChain{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 13:19:11 -07:00
|
|
|
// getBenchmarkMockApp initializes a mock application for this module, for purposes of benchmarking
|
|
|
|
// Any long term API support commitments do not apply to this function.
|
|
|
|
func getBenchmarkMockApp() (*mock.App, error) {
|
|
|
|
mapp := mock.NewApp()
|
2019-06-05 16:26:17 -07:00
|
|
|
types.RegisterCodec(mapp.Cdc)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
|
|
|
blacklistedAddrs := make(map[string]bool)
|
|
|
|
blacklistedAddrs[moduleAccAddr.String()] = true
|
|
|
|
|
2019-06-14 07:10:37 -07:00
|
|
|
bankKeeper := keeper.NewBaseKeeper(
|
2019-01-28 19:06:48 -08:00
|
|
|
mapp.AccountKeeper,
|
2019-06-05 16:26:17 -07:00
|
|
|
mapp.ParamsKeeper.Subspace(types.DefaultParamspace),
|
|
|
|
types.DefaultCodespace,
|
2019-07-31 08:39:02 -07:00
|
|
|
blacklistedAddrs,
|
2019-01-28 19:06:48 -08:00
|
|
|
)
|
2019-06-14 07:10:37 -07:00
|
|
|
mapp.Router().AddRoute(types.RouterKey, bank.NewHandler(bankKeeper))
|
2019-01-28 19:06:48 -08:00
|
|
|
mapp.SetInitChainer(getInitChainer(mapp, bankKeeper))
|
2018-07-06 13:19:11 -07:00
|
|
|
|
2018-09-12 21:53:55 -07:00
|
|
|
err := mapp.CompleteSetup()
|
2018-07-06 13:19:11 -07:00
|
|
|
return mapp, err
|
|
|
|
}
|
|
|
|
|
2018-06-27 15:33:56 -07:00
|
|
|
func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
|
|
|
|
benchmarkApp, _ := getBenchmarkMockApp()
|
|
|
|
|
|
|
|
// Add an account at genesis
|
|
|
|
acc := &auth.BaseAccount{
|
|
|
|
Address: addr1,
|
|
|
|
// Some value conceivably higher than the benchmarks would ever go
|
2018-07-30 17:09:50 -07:00
|
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)},
|
2018-06-27 15:33:56 -07:00
|
|
|
}
|
|
|
|
accs := []auth.Account{acc}
|
|
|
|
|
|
|
|
// Construct genesis state
|
|
|
|
mock.SetGenesis(benchmarkApp, accs)
|
|
|
|
// Precompute all txs
|
2018-11-26 03:29:21 -08:00
|
|
|
txs := mock.GenSequenceOfTxs([]sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
|
2018-06-27 15:33:56 -07:00
|
|
|
b.ResetTimer()
|
|
|
|
// Run this with a profiler, so its easy to distinguish what time comes from
|
|
|
|
// Committing, and what time comes from Check/Deliver Tx.
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkApp.BeginBlock(abci.RequestBeginBlock{})
|
|
|
|
x := benchmarkApp.Check(txs[i])
|
|
|
|
if !x.IsOK() {
|
|
|
|
panic("something is broken in checking transaction")
|
|
|
|
}
|
|
|
|
benchmarkApp.Deliver(txs[i])
|
|
|
|
benchmarkApp.EndBlock(abci.RequestEndBlock{})
|
|
|
|
benchmarkApp.Commit()
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 15:58:02 -08:00
|
|
|
|
|
|
|
func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
|
|
|
|
benchmarkApp, _ := getBenchmarkMockApp()
|
|
|
|
|
|
|
|
// Add an account at genesis
|
|
|
|
acc := &auth.BaseAccount{
|
|
|
|
Address: addr1,
|
|
|
|
// Some value conceivably higher than the benchmarks would ever go
|
|
|
|
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)},
|
|
|
|
}
|
|
|
|
accs := []auth.Account{acc}
|
|
|
|
|
|
|
|
// Construct genesis state
|
|
|
|
mock.SetGenesis(benchmarkApp, accs)
|
|
|
|
// Precompute all txs
|
|
|
|
txs := mock.GenSequenceOfTxs([]sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
|
|
|
|
b.ResetTimer()
|
|
|
|
// Run this with a profiler, so its easy to distinguish what time comes from
|
|
|
|
// Committing, and what time comes from Check/Deliver Tx.
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
benchmarkApp.BeginBlock(abci.RequestBeginBlock{})
|
|
|
|
x := benchmarkApp.Check(txs[i])
|
|
|
|
if !x.IsOK() {
|
|
|
|
panic("something is broken in checking transaction")
|
|
|
|
}
|
|
|
|
benchmarkApp.Deliver(txs[i])
|
|
|
|
benchmarkApp.EndBlock(abci.RequestEndBlock{})
|
|
|
|
benchmarkApp.Commit()
|
|
|
|
}
|
|
|
|
}
|